Feliks - 7 months ago 37

R Question

I try to perform a simple

`lm()`

`d = data.frame(replicate(6,rnorm(6)))`

colnames(d) = as.character(0:5)

However, my

`lm()`

`lm(d[1,]~colnames(d))`

#Error in model.frame.default(formula = d[1, ] ~ colnames(d), drop.unused.levels = TRUE) :

#invalid type (list) for variable 'd[1, ]'

I would very much appreciate if someone helps me to get this running. I have not used much the

`lm()`

I know that the

`lm()`

`lm(columnA ~ columnB, data = mydata)`

`cbind(d[1,],0:5)`

This, however, does not drop the dimensions of

`d`

Answer

I have to make some assumptions on what you plan to do, as you are not actively clarifying it.

I am assuming you want a different, independent regression line for each row of your data frame. In other words, you have multiple response (one per row), but a common covariate:

```
x <- 1:ncol(d) - 1
```

Thus, you can do

```
fit <- lm(t(d) ~ x)
#Call:
#lm(formula = t(d) ~ x)
#Coefficients:
# [,1] [,2] [,3] [,4] [,5] [,6]
#(Intercept) 0.23133 0.48307 0.07867 0.62308 0.71174 0.89866
#x 0.02964 -0.30077 -0.05160 0.06321 -0.17155 -0.43689
```

`fit`

is not a standard "lm" object, but "mlm" (multiple linear models). The coefficient matrix you see above, has each column associated to each response.