Skip to content

simple lm question

7 messages · Worik R, R. Michael Weylandt, David Winsemius

#
On Dec 1, 2011, at 10:50 PM, Worik R wrote:

            
Use `lm` the way it is designed to be used, with a data argument:

 > l2 <- lm(e~. , data=as.data.frame(M))
 > summary(l2)

Call:
lm(formula = e ~ ., data = as.data.frame(M))

Residuals:
     Min      1Q  Median      3Q     Max
-0.5558 -0.2396  0.1257  0.2213  0.4586

Coefficients:
             Estimate Std. Error t value Pr(>|t|)
(Intercept)  0.41110    0.25484   1.613    0.128
a           -0.03258    0.28375  -0.115    0.910
b            0.09088    0.25971   0.350    0.731
c            0.09382    0.29555   0.317    0.755
d            0.14725    0.33956   0.434    0.671

Residual standard error: 0.3317 on 15 degrees of freedom
Multiple R-squared: 0.04667,	Adjusted R-squared: -0.2076
F-statistic: 0.1836 on 4 and 15 DF,  p-value: 0.9433
#
In your code by supplying a vector M[,"e"] you are regressing "e"
against all the variables provided in the data argument, including "e"
itself -- this gives the very strange regression coefficients you
observe. R has no way to know that that's somehow related to the "e"
it sees in the data argument.

In the suggested way,

lm(formula = e ~ ., data = as.data.frame(M))

e is regressed against everything that is not e and sensible results are given.

Michael
On Fri, Dec 2, 2011 at 11:03 PM, Worik R <worikr at gmail.com> wrote:
#
On Dec 2, 2011, at 11:20 PM, Worik R wrote:

            
<Sigh>  Please note that your "df" and "M" are undoubtedly different  
objects by now:

 > M <- matrix(runif(5*20), nrow=20)
 > colnames(M) <- c('a', 'b', 'c', 'd', 'e')
 > l1 <- lm(e~., data=as.data.frame(M))
 > l1

Call:
lm(formula = e ~ ., data = as.data.frame(M))

Coefficients:
(Intercept)            a            b            c            d
     0.40139     -0.15032     -0.06242      0.13139      0.23905

 > l3 <- lm(M[,5]~M[,1]+M[,2]+M[,3]+M[,4])
 > l3

Call:
lm(formula = M[, 5] ~ M[, 1] + M[, 2] + M[, 3] + M[, 4])

Coefficients:
(Intercept)       M[, 1]       M[, 2]       M[, 3]       M[, 4]
     0.40139     -0.15032     -0.06242      0.13139      0.23905

As expected.