0

I tried to predict the t121 columns using the "lm" command below like this,

Model<-lm(t121 ~ t1 + t2 + ..... +t120, mydata)

In my data dependent variables are more than 100, So it's difficult for predicting each columns using "lm" command that's why i want to write the program for my data like this given below i written,

for(j in 120:179){
model[[j+1]]<-lm(t[j+1] ~ add1(t1:t[j]),mydata)
}

Instead of add1 place i used add.bigq,sum commands but these three commands are not correct please tell me what is the command suitable for that place?

Aarthika
  • 101
  • 11
  • Instead of `t1+t2+..` you can use `lm(t121~., mydata)` It is better to show a small reproducible example. – akrun Jan 24 '16 at 12:14
  • please tell me clearly i didn't get – Aarthika Jan 24 '16 at 12:19
  • Perhaps `lapply(121:ncol(mydata), function(i) lm(mydata[[1]]~., mydata[c(i,seq(i-1))]))` – akrun Jan 24 '16 at 12:23
  • shall i use this command in the "for loop"? and where shall i add this command? – Aarthika Jan 24 '16 at 12:28
  • If you are using a `for` loop, you need to create a `list` object to store the output. The `lapply` output is already a `list`. – akrun Jan 24 '16 at 12:31
  • Please provide a minimal, complete, and reproducible example that anyone else can simply copy and paste into their R session to run. All library statements and inputs need to be included. Cut down your data to the minimum needed to illustrate the problem and if your input is `x` then show it by displaying the output of `dput(x)` in your question. See http://stackoverflow.com/help/mcve for general advice and see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for more R oriented advice on how to ask a question on SO.. – G. Grothendieck Jan 24 '16 at 12:48

1 Answers1

1

From what I understand, you want to write a loop that allows you to use lm with different formulas. The nice thing about lm is that it can take objects of the class formula as its first argument. Lets see how that works.

# Create a data set
df <- data.frame(col1=(1:10+rnorm(10)), col2 = 1:10, col3 = rnorm(10), col4 = rnorm(10))

If we want to run lm on col1 as the dependent and col2 as the independent variable, then we can do this:

model_a <- lm(col1 ~ col2, data = df)

form_b <- as.formula("col1 ~ col2")
model_b <- lm(form_b, data = df)

all.equal(model_a,model_b)
# [1] "Component “call”: target, current do not match when deparsed"

So the only thing that differed between the two models is that the function call was different (in model_b we used form_b, not col1 ~ col2). Other than that, the models are identical.

So now you know how to use the formula class to run lm. You can easily construct formulas with paste, by setting collapse to +

ind_vars <- paste(names(df)[-1],collapse = " + ")
form_lm <- paste(names(df)[1], "~", ind_vars)
form_lm
# [1] "col1 ~ col2 + col3 + col4"

If we want three different models, we can do a couple of things, for example:

lis <- list()
for (i in 2:length(names(df))) {
    ind_vars <- paste(names(df)[2:i], collapse="+")
    form_lm <- paste(names(df)[1], "~", ind_vars)
    lis[[i-1]] <- lm(form_lm,data=df)
}
slamballais
  • 3,161
  • 3
  • 18
  • 29