# How to extract the model equation from model object in R?

To extract the model equation model object, we can use the model object name with dollar sign and call function. For example, if we have a model object name Model then the model equation can be extracted by using Model$call. This will directly present the equation that was used to create the model. ## Example1 Live Demo x1<−rnorm(20,1,0.2) x2<−rnorm(20,1,0.5) y1<−rnorm(20,5,1) Model1<−lm(y1~x1+x2) summary(Model1) ## Output 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)

## Example2

Model2<−lm(y1~x1+x2+x1*x2)
summary(Model2)

## Output

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) ## Example3 Model3<−lm(y1~x1+x2+x1*x2+x1^2+x2^2) summary(Model3) ## Output 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)

## Example4

Model4<−lm(y1~x1+x2+x1*x2+x1^2+x2^2+x1^3+x2^3)
summary(Model4)

## Output

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) ## Example5 Model5<−lm(y1~x1+x2+x1*x2+x1^2+x2^2+x1^3+x2^3+exp(x1)+exp(x2)) summary(Model5) ## Output 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))

Updated on: 07-Nov-2020

1K+ Views