user2143940
user2143940

Reputation: 53

How to use lm function for large number of attributes

i have a dataset with 1 label attribute and 784 pixel attributes with 42000 rows like below

label  pixel0  pixel1  pixel2 ...........  pixel783  
0        1     0         0                   16
.  
.  
1        2      15       1                    0

Now i want to perform regression on it and so i use lm function

lm(label~pixel0+pixel1+pixel2..........+pixel784,data=df )

but to write pixel0 to pixel 784 is , according to me foolishness.
Is there a way to avoid manually writing the above exp or i have to do so?

Upvotes: 2

Views: 461

Answers (1)

Prasanna Nandakumar
Prasanna Nandakumar

Reputation: 4335

you can use . as described in the help page for formula. The . stands for "all columns not otherwise in the formula".

lm(label ~ ., data = df)

Alternatively, construct the formula manually with paste. This example is from the as.formula() help page:

xnam <- paste("x", 1:784, sep="")
(fmla <- as.formula(paste("y ~ ", paste(xnam, collapse= "+"))))

You can then insert this object into regression function:

lm(fmla, data = df)

Upvotes: 2

Related Questions