1: ridgelm <- function(xy,s) { ycol <- ncol(xy) a <- cbind(1,xy[,-ycol]) aas <- t(a) %*% a + diag(s,ycol) solve(aas) %*% t(a) %*% xy[,ycol] } 2: # arguments: # rsdf: a data frame in the standard (userID, itemID, rating, covs) format # idCol: column number in rsdf of the grouping ID, either user or item ID # dummyCol: dummy variable on which average rating is to be found # return value: # copy of rsdf, with an exra column: for a given row (u,i,r,covs), the # value of the new column will be the mean rating given by user u among # all rows for which the given dummy is 1 makeAvgCov <- function(rsdf,idCol,dummyCol) { dm <- as.numeric(rsdf[,dummyCol]) rsdf.sub <- rsdf[dm == 1,] id <- as.character(rsdf[,idCol]) id.sub <- as.character(rsdf.sub[,idCol]) meanNoNA <- function(x) mean(x,na.rm=T) tmp <- tapply(rsdf.sub[,3],id.sub,meanNoNA) newCol <- tmp[id] cbind(rsdf,newCol) }