在stat_smooth中添加权重和相关类型

时间:2017-01-30 19:05:35

标签: r ggplot2 statistics

有没有办法在ggplot2,stat_smooth函数中指定权重和相关类型。我试图使用以下gam模式绘制平滑函数。有没有办法在stat_smooth()

中指定参数
set.seed(1)
D = data.table(id = rep((1:100),10), value = rnorm(1000), stratification = rep(c("A","B","C","D"), 25))
setkey(D, id)
D = D[, time := 1:10, by = id]
w = 1/(0.1 + time)^2 
Model <- gam(formula = value~ s(time), weights=w, data=D, correlation=corAR1(form =~ time))

有没有办法在ggplot2中绘制这个平滑函数?

谢谢!

1 个答案:

答案 0 :(得分:1)

这应该有效:

set.seed(1)
D = data.table(id = rep((1:100),10), value = rnorm(1000), stratification = rep(c("A","B","C","D"), 25))
setkey(D, id)
D[, time := 1:10, by = id]
D$w = 1/(0.1 + D$time)^2 
Model <- gam(formula = value~ s(time), weights=w, data=D, correlation=corAR1(form =~ time))
pred <- data.frame(time=D$time, fitted=Model$fitted.values)
ggplot(D, aes(time, value)) + 
  geom_point() +
  geom_line(data=pred,aes(time, fitted), col='steelblue', lwd=1.2)

enter image description here