2

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?

FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67
  • 2
    `label ~ .`. And this is the mnist handwritten digits, so you probably don't want to be using `lm` – Jake Burkhead Jan 29 '14 at 04:36
  • it's not working as it shows Error: cannot allocate vector of size 251.5 Mb In addition: Warning messages: 1: In lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : Reached total allocation of 1535Mb: see help(memory.size) – user2143940 Jan 29 '14 at 04:48
  • 1
    How much memory does your computer have? 1.5 GB? That error shows up because you are running out or memory not because of an error in the code. – Jake Burkhead Jan 29 '14 at 04:52

1 Answers1

2

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)
Prasanna Nandakumar
  • 4,295
  • 34
  • 63
  • though the command is right but there seems to be memory problem as always i am getting the error as Error: cannot allocate vector of size 251.5 Mb In addition: Warning messages: 1: In lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : – user2143940 Jan 29 '14 at 05:26
  • 1
    as Jake mentioned you are running out of memory.why don't you try `lm.fit` instead of `lm` to narrow down the problem? `lm.fit` just does more-or-less "raw" linear model fitting via the `QR decomposition` If you also get memory problems with lm.fit check out - http://stackoverflow.com/questions/1358003/tricks-to-manage-the-available-memory-in-an-r-session – Prasanna Nandakumar Jan 29 '14 at 05:31