R 使用黄土平滑器绘制纵向数据

R 使用黄土平滑器绘制纵向数据,r,plot,R,Plot,我需要在纵向(个人周期)数据集中为个人绘制一组平滑的轨迹。我可以使用OLS回归绘制几天内的单个轨迹,但我想知道如何使用非参数平滑器绘制轨迹 下面是示例数据。同样的结果变量在11岁、12岁、13岁、14岁和15岁时对每个人进行五次测量。暴露是一个预测变量,但我们对此练习不感兴趣 id <- c(9, 45, 268, 314, 442, 514, 569, 624, 723, 918, 949, 978, 1105, 1542, 1552, 1653) eleven <- c(2.23

我需要在纵向(个人周期)数据集中为个人绘制一组平滑的轨迹。我可以使用OLS回归绘制几天内的单个轨迹,但我想知道如何使用非参数平滑器绘制轨迹

下面是示例数据。同样的结果变量在11岁、12岁、13岁、14岁和15岁时对每个人进行五次测量。暴露是一个预测变量,但我们对此练习不感兴趣

id <- c(9, 45, 268, 314, 442, 514, 569, 624, 723, 918, 949, 978, 1105, 1542, 1552, 1653)
eleven <- c(2.23, 1.12, 1.45, 1.22, 1.45, 1.34, 1.79, 1.12, 1.22, 1.00, 1.99, 1.22, 1.34, 1.22, 1.00, 1.11)
twelve <- c(2.23, 1.12, 1.45, 1.22, 1.45, 1.34, 1.79, 1.12, 1.22, 1.00, 1.99, 1.22, 1.34, 1.22, 1.00, 1.11)
thirteen <- c(1.90, 1.45, 1.99, 1.55, 1.45, 2.23, 1.90, 1.22, 1.12, 1.22, 1.12, 2.12, 1.99, 1.99, 2.23, 1.34)
fourteen <- c(2.12, 1.45, 1.79, 1.12, 1.67, 2.12, 1.99, 1.12, 1.00, 1.99, 1.45, 3.46, 1.90, 1.79, 1.55, 1.55)
fifteen <- c(2.66, 1.99, 1.34, 1.12, 1.90, 2.44, 1.99, 1.22, 1.12, 1.22, 1.55, 3.32, 2.12, 2.12, 1.55, 2.12)
exposure <- c(1.54, 1.16, 0.90, 0.81, 1.13, 0.90, 1.99, 0.98, 0.81, 1.21, 0.93, 1.59, 1.38, 1.44, 1.04, 1.25)

df <- data.frame(id, eleven, twelve, thirteen, fourteen, fifteen, exposure)
但我们真正想要的是最适合每个人的产品线

fit <- by(dfPP, dfPP$id, function (bydata) fitted.values(lm(score ~ time, data = bydata)))
fit <- unlist(fit)


interaction.plot(dfPP$age, dfPP$id, fit, xlab = "age", ylab = "score")
现在这一切都很好,但是除了使用非参数平滑器之外,有人知道如何获得类似的结果吗?还与平均轨迹线叠加。也许使用某种黄土函数

interaction.plot(dfPP$age, dfPP$id, dfPP$score)
fit <- by(dfPP, dfPP$id, function (bydata) fitted.values(lm(score ~ time, data = bydata)))
fit <- unlist(fit)


interaction.plot(dfPP$age, dfPP$id, fit, xlab = "age", ylab = "score")
ints <- by(dfPP, df$id, function (data) coefficients(lm(score ~ time, data = data))[[1]]) 
ints1 <- unlist(ints)


slopes <- by(dfPP, dfPP$id, function (data) coefficients(lm(score ~ time, data = data))[[2]])
slopes1 <- unlist(slopes)
abline(a = mean(ints1), b = mean(slopes1), lwd = 2, col = "red")