[R] Can I get rid of this for loop using apply?

From: Mike Dugas <mikedugas77_at_gmail.com>
Date: Wed, 23 Apr 2008 17:23:58 -0400


The answer to my post is yes (which I just figured out).

Solution:

#super small version of R code for pd plot using apply

a <- rbind(c(0:1111)*(max(m$x1)-min(m$x1))/1111 + min(m$x1),c(0:1111)*0-99999)

b <- matrix(rep(c(0:1111)*(max(m$x1)-min(m$x1))/1111 + min(m$x1), nrow(m)), nrow(m), 1112, byrow=T)

a[2,] <- apply(b,2,FUN=function(x)
{mean(predict(lm1,cbind(m[,-match("x1",names(m))],x1=x))) })

plot(a[1,],a[2,],xlab="x1",ylab="Response",type="l",main="Partial Dependence Plot")

Mike Dugas

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 23 Apr 2008 - 21:43:31 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 Apr 2008 - 23:30:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive