# [R] simple lm question

David Winsemius dwinsemius at comcast.net
Fri Dec 2 05:42:58 CET 2011

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

> cannot find my way around the documentation properly
>
> Given the code...
>
> M <- matrix(runif(5*20), nrow=20)
> colnames(M) <- c('a', 'b', 'c', 'd', 'e')
> ind <- c(1,2,3,4)
> dep <- 5
>
> I can then do...
> l2 <- lm(M[,dep]~M[,ind]) ## Clearly not useful!
> summary(l2)
>
> I am not sure what my regression formula is.
>
> The results are (edited for brevity)
>
>> summary(l2)\$coefficients
>
> Coefficients:
>            Estimate Std. Error t value Pr(>|t|)
> (Intercept)  0.63842    0.16036   3.981  0.00120 **
> M[, ind]a   -0.02912    0.17566  -0.166  0.87054
> M[, ind]b   -0.21172    0.19665  -1.077  0.29865
> M[, ind]c    0.00752    0.18551   0.041  0.96820
> M[, ind]d    0.06357    0.18337   0.347  0.73366
>
> Is there some way I can do this so the coefficients have better
> names ('a'
> through 'd')?
>

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

--
David.

> As for what I am doing it is not...
>
> l3 <- lm(M[,1]+M[,2]+M[,3]+M[,4]~M[,5])
>
> or
>
> l4 <- lm(M[,1]*M[,2]*M[,3]*M[,4]~M[,5])
>
> cheers
> W
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help