Skip to content

Combining several regressions

3 messages · Peter Dalgaard, Hadley Wickham

#
I've a situation where I need to perform different regressions on 
different subsets of my data and then combine the resulting fitted 
values back into the original dataset (partly this is because loess 
doesn't do grouping, but I'd also like to approximate the shrinkage 
estimators of mixed models by giving low weights to observations in the 
groups other than the one I'm regressing).

For example, I want to fit separate loess regressions for each value of 
some conditioning value.  I can do the regressions with by(data.frame, 
conditioning.variable, function(subset) { loess(y ~ x, subset) })  but 
I'm at a loss as how to get the fitted values out and match them up with 
the rows in the original dataset.

Any suggestions?

Thanks,

Hadley
#
Hadley Wickham <h.wickham at auckland.ac.nz> writes:
[Untested!]

b <- by(data.frame, conditioning.variable, 
        function(subset) { loess(y ~ x, subset) })

unsplit(lapply(b,fitted), conditioning.variable)

You might need to replace the by() by an

  lapply(split(data.frame,cond.var), FUN)

construction, but it doesn't look like it should be necessary.
#
That does the trick. Thanks!
Hadley