
> rats <- read.table('u.data')
> head(rats)
   V1  V2 V3        V4
1 196 242  3 881250949
2 186 302  3 891717742
3  22 377  1 878887116
4 244  51  2 880606923
5 166 346  1 886397596
6 298 474  4 884182806
> class(rats$V1)
[1] "integer"
> rats$V1 <- as.factor(rats$V1)
> rats$V2 <- as.factor(rats$V2)
> lmout <- lm(V3 ~ V1+V2,data=rats)  # runs about 10 mins
> coefs <- lmout$coefficients
> str(coefs)
 Named num [1:2624] 3.913 0.041 -0.529 0.88 -0.457 ...
 - attr(*, "names")= chr [1:2624] "(Intercept)" "V12" "V13" "V14" ...
# let's try predicting something
> newx <- rats[5,1:2]
> newx
   V1  V2
5 166 346
# how would user 166 like movie 8?
> newx$V2 <- '8'  # character, due to factor
# R factors are essentially character vectors with named levels
> newx
   V1 V2
5 166  8
> predict(lmout,newx)
       5 
4.399462 


