要提取模型方程模型对象,可以使用带有美元符号和调用函数的模型对象名称。例如,如果我们有一个模型对象名称Model,则可以使用Model $call提取模型方程式。这将直接显示用于创建模型的方程式。
x1<−rnorm(20,1,0.2) x2<−rnorm(20,1,0.5) y1<−rnorm(20,5,1) Model1<−lm(y1~x1+x2) summary(Model1)
输出结果
Call: lm(formula = y1 ~ x1 + x2) Residuals: Min 1Q Median 3Q Max −1.4419 −0.6894 −0.2993 0.7183 1.7525 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 5.4266 0.8572 6.331 7.53e−06 *** x1 −0.1942 0.7662 −0.253 0.803 x2 −0.3554 0.3972 −0.895 0.383 −−− Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.9826 on 17 degrees of freedom Multiple R−squared: 0.05325, Adjusted R−squared: −0.05813 F−statistic: 0.4781 on 2 and 17 DF, p−value: 0.628 Model1$call lm(formula = y1 ~ x1 + x2)
Model2<−lm(y1~x1+x2+x1*x2) summary(Model2)
输出结果
Call: lm(formula = y1 ~ x1 + x2 + x1 * x2) Residuals: Min 1Q Median 3Q Max −1.39281 −0.75354 −0.04841 0.64300 1.55856 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4.5049 1.4569 3.092 0.00699 ** x1 0.7627 1.4416 0.529 0.60401 x2 1.7243 2.6722 0.645 0.52790 x1:x2 −2.0166 2.5617 −0.787 0.44267 −−− Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.9938 on 16 degrees of freedom Multiple R−squared: 0.08855, Adjusted R−squared: −0.08234 F−statistic: 0.5182 on 3 and 16 DF, p−value: 0.6757 Model2$call lm(formula = y1 ~ x1 + x2 + x1 * x2)
Model3<−lm(y1~x1+x2+x1*x2+x1^2+x2^2) summary(Model3)
输出结果
Call: lm(formula = y1 ~ x1 + x2 + x1 * x2 + x1^2 + x2^2) Residuals: Min 1Q Median 3Q Max −1.39281 −0.75354 −0.04841 0.64300 1.55856 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4.5049 1.4569 3.092 0.00699 ** x1 0.7627 1.4416 0.529 0.60401 x2 1.7243 2.6722 0.645 0.52790 x1:x2 −2.0166 2.5617 −0.787 0.44267 −−− Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.9938 on 16 degrees of freedom Multiple R−squared: 0.08855, Adjusted R−squared: −0.08234 F−statistic: 0.5182 on 3 and 16 DF, p−value: 0.6757 Model3$call lm(formula = y1 ~ x1 + x2 + x1 * x2 + x1^2 + x2^2)
Model4<−lm(y1~x1+x2+x1*x2+x1^2+x2^2+x1^3+x2^3) summary(Model4)
输出结果
Call: lm(formula = y1 ~ x1 + x2 + x1 * x2 + x1^2 + x2^2 + x1^3 + x2^3) Residuals: Min 1Q Median 3Q Max −1.39281 −0.75354 −0.04841 0.64300 1.55856 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4.5049 1.4569 3.092 0.00699 ** x1 0.7627 1.4416 0.529 0.60401 x2 1.7243 2.6722 0.645 0.52790 x1:x2 −2.0166 2.5617 −0.787 0.44267 −−− Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.9938 on 16 degrees of freedom Multiple R−squared: 0.08855, Adjusted R−squared: −0.08234 F−statistic: 0.5182 on 3 and 16 DF, p−value: 0.6757 Model4$call lm(formula = y1 ~ x1 + x2 + x1 * x2 + x1^2 + x2^2 + x1^3 + x2^3)
Model5<−lm(y1~x1+x2+x1*x2+x1^2+x2^2+x1^3+x2^3+exp(x1)+exp(x2)) summary(Model5)
输出结果
Call: lm(formula = y1 ~ x1 + x2 + x1 * x2 + x1^2 + x2^2 + x1^3 + x2^3 + exp(x1) + exp(x2)) Residuals: Min 1Q Median 3Q Max −1.4709 −0.4401 −0.0917 0.6385 1.6444 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 3.5360 1.5679 2.255 0.0407 * x1 6.4664 3.5377 1.828 0.0890 . x2 1.1568 2.7512 0.420 0.6805 exp(x1) −1.5881 0.9112 −1.743 0.1033 exp(x2) 0.2382 0.4782 0.498 0.6261 x1:x2 −2.4765 2.5267 −0.980 0.3437 −−− Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.9619 on 14 degrees of freedom Multiple R−squared: 0.2529, Adjusted R−squared: −0.01398 F−statistic: 0.9476 on 5 and 14 DF, p−value: 0.4811 Model5$call lm(formula = y1 ~ x1 + x2 + x1 * x2 + x1^2 + x2^2 + x1^3 + x2^3 + exp(x1) + exp(x2))