0
$\begingroup$

I am not an expert and I need a help in these commands:

lm (growth ~ tannin , data = tanninData)
model <- lm (growth~tannin , data = tanninData)
class ( model )
summary ( model)

The question: Write down the regression equation using the output from summary(model)?

$\endgroup$

closed as off-topic by Scortchi Aug 13 '17 at 16:01

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Scortchi
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ This is a very simple model so you need to know enough about regression to easily write the fitted equation from the output of summary. $\endgroup$ – Frank Harrell Aug 13 '17 at 14:27
  • $\begingroup$ thanks , but I am not familiar with R in act I just begin learning it , and my wondering if there was a specific command in R to write a regression equation based on the summary output you guys are expert in this $\endgroup$ – Watan Nidhal Aug 13 '17 at 14:42
  • 1
    $\begingroup$ One option is to use the rms package ols function then run the Function function on the fit object to see the model in R notation $\endgroup$ – Frank Harrell Aug 13 '17 at 15:28
  • $\begingroup$ See ?formula & the manual, An Introduction to R, Ch11 for an explanation of the syntax. $\endgroup$ – Scortchi Aug 13 '17 at 16:00
  • 1
    $\begingroup$ You could use this function: write_eqn <- function(model){cf <- signif(coef(model), 2);eqn <- paste("y = ", cf[1], sep="");for (i in 2:length(cf)){;eqn <- paste(eqn, " + ", cf[i],"*", names(cf)[i], sep="")};eqn} and do write_eqn(model) $\endgroup$ – Flounderer Aug 13 '17 at 17:10
0
$\begingroup$

First create an object containing your equation:

data(mtcars)
eq <-  lm(mpg ~ cyl, mtcars)

You can inspect elements inside your eq object using the command:

str(eq)

that returns:

List of 12
 $ coefficients : Named num [1:2] 37.88 -2.88
  ..- attr(*, "names")= chr [1:2] "(Intercept)" "cyl"
 $ residuals    : Named num [1:32] 0.37 0.37 -3.58 0.77 3.82 ...
  ..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
 $ effects      : Named num [1:32] -113.65 -28.6 -3.7 0.71 3.82 ...
  ..- attr(*, "names")= chr [1:32] "(Intercept)" "cyl" "" "" ...
 $ rank         : int 2
 $ fitted.values: Named num [1:32] 20.6 20.6 26.4 20.6 14.9 ...
  ..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
 $ assign       : int [1:2] 0 1
 $ qr           :List of 5
  ..$ qr   : num [1:32, 1:2] -5.657 0.177 0.177 0.177 0.177 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
  .. .. ..$ : chr [1:2] "(Intercept)" "cyl"
  .. ..- attr(*, "assign")= int [1:2] 0 1
  ..$ qraux: num [1:2] 1.18 1.02
  ..$ pivot: int [1:2] 1 2
  ..$ tol  : num 1e-07
  ..$ rank : int 2
  ..- attr(*, "class")= chr "qr"
 $ df.residual  : int 30
 $ xlevels      : Named list()
 $ call         : language lm(formula = mpg ~ cyl, data = mtcars)
 $ terms        :Classes 'terms', 'formula'  language mpg ~ cyl
  .. ..- attr(*, "variables")= language list(mpg, cyl)
  .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:2] "mpg" "cyl"
  .. .. .. ..$ : chr "cyl"
  .. ..- attr(*, "term.labels")= chr "cyl"
  .. ..- attr(*, "order")= int 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(mpg, cyl)
  .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. ..- attr(*, "names")= chr [1:2] "mpg" "cyl"
 $ model        :'data.frame':  32 obs. of  2 variables:
  ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
  ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
  ..- attr(*, "terms")=Classes 'terms', 'formula'  language mpg ~ cyl
  .. .. ..- attr(*, "variables")= language list(mpg, cyl)
  .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. ..$ : chr [1:2] "mpg" "cyl"
  .. .. .. .. ..$ : chr "cyl"
  .. .. ..- attr(*, "term.labels")= chr "cyl"
  .. .. ..- attr(*, "order")= int 1
  .. .. ..- attr(*, "intercept")= int 1
  .. .. ..- attr(*, "response")= int 1
  .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. .. ..- attr(*, "predvars")= language list(mpg, cyl)
  .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. .. ..- attr(*, "names")= chr [1:2] "mpg" "cyl"
 - attr(*, "class")= chr "lm"

Now, say you want the coefficients of your regressors (the first item of str()), use the command:

eq$coefficients

More specifically, if you want the intercept:

eq$coefficients[1]
#(Intercept) 
#   37.88458 

And the coefficient for the cyl variable:

eq$coefficients[2]
#     cyl 
#-2.87579 

Etc...

I mean, the R summary function is just nice-looking and limited grouping of some elements of an equation object.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.