假设我们假设了以下线性混合模型(LMM),我们通常将其称为fit。
library(lme4)
fit <- lmer(Reaction ~ Days + (1 | Subject), data = sleepstudy)
Run Code Online (Sandbox Code Playgroud)
假设我们也有兴趣从 中提取原始公式(即固定效应和随机效应部分)fit,我们希望将其传递给名为 的第二个 LMM fit2。显然,传递terms(fit)给formula的参数lmer()是行不通的。
> fit2 <- lmer(terms(fit), data = sleepstudy)
Error: No random effects terms specified in formula
Run Code Online (Sandbox Code Playgroud)
问题
有没有办法从 中提取固定效应和随机效应部分fit,然后将其传递给lmer()?
我正在尝试计算与随时变系数的Cox PH模型获得的点估计相关的P值。我编写的函数没有提供正确的P值。我将通过使用生存包中的NCCTG肺癌数据来说明这一点。
# Setup
require(survival)
# Effect of Karnofsky score, linear
fit <- coxph(Surv(time/365.25, status == 2) ~ ph.karno + tt(ph.karno),
lung, tt=function(x, t, ...) {x*t})
Run Code Online (Sandbox Code Playgroud)
功能:
# Same function but now with a P-value in the output
calculate.timeDependentHazard.P <- function(model,time) {
index.1 <- which(names(model$coef)=="ph.karno")
index.2 <- which(names(model$coef)=="tt(ph.karno)")
coef <- model$coef[c(index.1,index.2)]
var <- rbind(c(model$var[index.1,index.1],model$var[index.1,index.2]),
c(model$var[index.2,index.1],model$var[index.2,index.2]))
var.at.time <- t(c(1,time)) %*% var %*% c(1,time)
hazard.at.time <- t(c(1,time)) %*% coef
lower.95 <- hazard.at.time - 1.96*sqrt(var.at.time)
upper.95 <- hazard.at.time + …Run Code Online (Sandbox Code Playgroud)