2

I want to compute a linear regression on all column (or to a selected column) of a specific dataset. The first column respresent a X axis of the regression, the other each subject response. The second step is to extract for each specific subject the coefficients parameters of regression (linear or logistic). Actually I do it manually for each column using lm (or glm) and extracting the coefficients to a specific variable and dataset.

Example using lm:

dataset <- as.data.frame(matrix(c(1,1,
3,7,2,1,4,5,3,2,4,6,4,2,5,8,5,5,9,9,6,4,
12,10,7,6,15,11,8,6,15,15,9,8,16,10,10,9,18,9,11,12,
20,12,12,15,21,16,13,18,22,15,14,22,21,10,15,29,24,12)
,nrow=15, ncol=4,byrow=TRUE))
colnames(dataset) <- c("X","Sj1","Sj2","Sj3")

Output:

dataset
    X Sj1 Sj2 Sj3
1   1   1   3   7
2   2   1   4   5
3   3   2   4   6
4   4   2   5   8
5   5   5   9   9
6   6   4  12  10
7   7   6  15  11
8   8   6  15  15
9   9   8  16  10
10 10   9  18   9
11 11  12  20  12
12 12  15  21  16
13 13  18  22  15
14 14  22  21  10
15 15  29  24  12

Regressions:

attach (dataset)
mod1 <- lm(Sj1~X)
mod2 <- lm(Sj2~X)
mod3 <- lm(Sj3~X)

Intercept <- 0
Intercept[1] <- mod1$coefficients[[1]]
Intercept[2] <- mod2$coefficients[[1]]
Intercept[3] <- mod3$coefficients[[1]]

Slope <- 0
Slope[1] <- mod1$coefficients[[2]]
Slope[2] <- mod2$coefficients[[2]]
Slope[3] <- mod3$coefficients[[2]]

data.frame(Intercept,Slope,row.names=colnames(dataset)[-1])

and the final output is

    Intercept     Slope
Sj1 -4.580952 1.7392857
Sj2  1.104762 1.6035714
Sj3  6.104762 0.5285714

There is a code to perform it automatically, indipendently from the number of columns? I tried apply and function without results.

What is the best way to do this?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Alessio
  • 93
  • 1
  • 7

1 Answers1

3

lm accepts a matrix on the LHS. See the documentation.

f <- as.formula(paste0("cbind(", paste(names(dataset)[-1], collapse = ","), ") ~ X"))
mods <- lm(f, data = dataset)
coef(mods)
#                  Sj1      Sj2       Sj3
#(Intercept) -4.580952 1.104762 6.1047619
#X            1.739286 1.603571 0.5285714

PS: You should get out of the habit of using attach.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • Thanks Roland for your answer, it's another useful solution. I use `attach` only in the final part of data analysis to write less code (such in graph). I know its limits and problem. – Alessio Dec 04 '15 at 14:48