0

I would like to fit a quadratic to (Time,SkinTemp) for each id in the following data.frame df. Each id has a different number of Time,SkinTemp entries so I'm stuck with 'predict'

df<-data.frame(Time=seq(65),
               SkinTemp=rnorm(65,37,0.5),
               id=rep(1:10,c(5,4,10,6,7,8,9,8,4,4)))

So far I have:

#Fit the model y=x^2+x+C
fitted_models = df %>% group_by(id) %>% do(model = lm(SkinTemp ~ Time+I(Time^2), data = .))

So far so good. Here's where I'm stuck. How do I pass the original Time data into the predict function below?

#Predict data points for each quadratic 
predQ<-sapply(unique(df$id), function(x) predict(fitted_models$model[[x]]))
HCAI
  • 2,213
  • 8
  • 33
  • 65

1 Answers1

2

Use fitted:

lapply(fitted_models$model, fitted)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Super, thanks. I think I made my question a bit short. The subsequent idea is to find the maximum curvature of the quadratics that I create by feeding into features(x,y) for each id. For this I unsuccessfully wrote: c<-sapply(unique(predQ$id), function(x) features(predQ$Time[predQ["id"]==x],predQ$SkinTemp[predQ["id"]==x],smoother="smooth.spline")) – HCAI Feb 08 '18 at 14:37
  • Do you mind adding that to the question? It's tough to see how to modify this answer to get what you're looking for in the comment. – twedl Feb 08 '18 at 14:43
  • No, do not change the question in material ways after it has already been answered. – G. Grothendieck Feb 08 '18 at 14:51