I am running some simulation where I want to fit a linear model to subsets of my data:
library(reshape2)
library(plyr)
all <- mutate(iris, mean_width = ave(Petal.Width, Petal.Length))
str(all)
## want to minimise sum(|y*polynomial(x) - z|^2) for each id
## in the region where x != exclude
weighted_difference <- function(d, n=4, exclude = c(2.5, 3), ...){
sub <- subset(d, !(Sepal.Width > exclude[1] &
Sepal.Width < exclude[2]))
fit <- lm(mean_width ~ I(poly(Petal.Length, n, raw=TRUE)*Petal.Width) + Petal.Width - 1, data = sub)
mutate(d, predict = predict(fit, d),
difference = Petal.Width - predict )
}
results <- ddply(all, "Species", weighted_difference)
This works, but I would like to use a simpler approach where I first create a new data.frame for the fit,
exclude <- c(3, 6)
sub <- subset(all, !(x > exclude[1] & x < exclude[2]))
fit all cases,
fits <- lm(z ~ I(poly(x, n, raw=TRUE)*y) + y - 1 | id, data = sub)
(this ... | id
is invalid syntax apparently)
and use predict on the full data at once,
all <- mutate(all, predict = predict(fits, all), difference = y - predict )
Is there some trick to use lm()
like this? Or a better solution? Thanks.