当我还需要估计其他随机效应时,有人可以告诉我如何让R估计分段线性模型中的断裂点(作为固定或随机参数)吗?
我在下面提供了一个玩具示例,该示例适合曲棍球杆/折断的杆回归,其随机点的斜率变化和y轴截距的随机变化为4的断裂点。我想估算断裂点而不是指定断裂点。它可以是随机效果(最好)或固定效果。
library(lme4)
str(sleepstudy)
#Basis functions
bp = 4
b1 <- function(x, bp) ifelse(x < bp, bp - x, 0)
b2 <- function(x, bp) ifelse(x < bp, 0, x - bp)
#Mixed effects model with break point = 4
(mod <- lmer(Reaction ~ b1(Days, bp) + b2(Days, bp) + (b1(Days, bp) + b2(Days, bp) | Subject), data = sleepstudy))
#Plot with break point = 4
xyplot(
Reaction ~ Days | Subject, sleepstudy, aspect = "xy",
layout = c(6,3), type = c("g", "p", "r"),
xlab = "Days of sleep deprivation",
ylab = "Average reaction time (ms)",
panel = function(x,y) {
panel.points(x,y)
panel.lmline(x,y)
pred <- predict(lm(y ~ b1(x, bp) + b2(x, bp)), newdata = data.frame(x = 0:9))
panel.lines(0:9, pred, lwd=1, lty=2, col="red")
}
)
输出:
Linear mixed model fit by REML
Formula: Reaction ~ b1(Days, bp) + b2(Days, bp) + (b1(Days, bp) + b2(Days, bp) | Subject)
Data: sleepstudy
AIC BIC logLik deviance REMLdev
1751 1783 -865.6 1744 1731
Random effects:
Groups Name Variance Std.Dev. Corr
Subject (Intercept) 1709.489 41.3460
b1(Days, bp) 90.238 9.4994 -0.797
b2(Days, bp) 59.348 7.7038 0.118 -0.008
Residual 563.030 23.7283
Number of obs: 180, groups: Subject, 18
Fixed effects:
Estimate Std. Error t value
(Intercept) 289.725 10.350 27.994
b1(Days, bp) -8.781 2.721 -3.227
b2(Days, bp) 11.710 2.184 5.362
Correlation of Fixed Effects:
(Intr) b1(D,b
b1(Days,bp) -0.761
b2(Days,bp) -0.054 0.181