Fitting a quadratic curve for each data set that has different lengths

我的梦境 提交于 2019-12-13 03:45:33

问题


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]]))

回答1:


Use fitted:

lapply(fitted_models$model, fitted)


来源:https://stackoverflow.com/questions/48687685/fitting-a-quadratic-curve-for-each-data-set-that-has-different-lengths

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!