1. (iv), (vi) 2. gy <- function() { library(lme4) data(InstEval) ie <- InstEval # easier if IDs are 1,2,3,..., and ratings are numeric factorToNumToFactor <- function(f) as.factor(as.numeric(f)) ie$s <- factorToNumToFactor(ie$s) ie$d <- factorToNumToFactor(ie$d) ie$y <- as.numeric(ie$y) library(recosystem) r <- Reco() # training set will be all of ie, but need in recosystem form ### next line was a fill-in ie.trn <- data_memory(ie[,1],ie[,2],ie[,7],index1=TRUE) ### next line was a fill-in r$train(ie.trn,opts=list(dim=20,nmf=TRUE)) result <- r$output(out_memory(),out_memory()) ### next 3 lines were fill-ins w <- result$P h <- t(result$Q) aout <- w %*% h # for a given row in ie, find the predicted rating findPred <- function(row) { row <- as.numeric(row) userID <- row[1] instID <- row[2] ### next line was a fill-in round(aout[userID,instID]) } # get vector of predicted ratings for ie preds <- apply(ie,1,findPred) ### next line was a fill-in mean(preds == ie$y) } print(gy()) # 0.2904619 3. nmf <- function(a,w0,h0,nIters) { w <- w0; h <- h0 nch <- ncol(h) nrw <- nrow(w) for (iter in 1:nIters) { for (j in 1:nch) { h[,j] <- lm(a[,j] ~ w-1)$coef } for (i in 1:nrw) { w[i,] <- lm(a[i,] ~ t(h)-1)$coef } } list(w=w, h=h) } a <- rbind(2:5,c(4,3,5,1),c(2,2,1,4),1:4) w0 <- matrix(1:8,ncol=2) h0 <- matrix(1:8,nrow=2) print(nmf(a,w0,h0,2)) # $w # [,1] [,2] # [1,] 5.077570 10.828505 # [2,] -5.426055 -4.330852 # [3,] 5.597083 10.142626 # [4,] 4.716699 9.309399 # # $h # [,1] [,2] [,3] [,4] # [1,] -1.3990331 -1.2300503 -1.958519 -0.8837465 # [2,] 0.8750231 0.8565012 1.260554 0.8783982