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

From: Mike Dugas <>
Date: Wed, 23 Apr 2008 20:59:36 -0400

Sure, I am creating a partial dependence plot (reference Friedman's stochastic gradient paper from, I want to say, 2001). The idea is to find the relationship between one of the predictors, say x1, and y by creating the following plot: take a random sample of actual data points, hold other predictors fixed (x2-xp), vary x1 across its range, create a string of predictions for each value of x1, repeat for all observations in sample, and finally average all the predictions for each value of x1. If you think about it, this plot solves Simpson's paradox under fairly mild conditions.

The code I wrote does this using predict() which is useful for modeling approaches like GAMs.


On Wed, Apr 23, 2008 at 8:47 PM, hadley wickham <> wrote:

> On Wed, Apr 23, 2008 at 7:31 PM, Mike Dugas <> wrote:
> > Thanks for the help. That explains why my time testing showed no
> > difference. Is there any way to speed up the program? It is unbearably
> > slow if I increase the number of loops.
> Could you explain exactly what you're trying to do with your code?
> It's a little hard to understand.
> Hadley
> --

        [[alternative HTML version deleted]] mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu 24 Apr 2008 - 01:09:15 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 Thu 24 Apr 2008 - 01:30:33 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive