小编Dio*_*ion的帖子

如何从 lmerMod 对象中提取原始公式(固定效应和随机效应)?

假设我们假设了以下线性混合模型(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()

r lme4 mixed-models

7
推荐指数
1
解决办法
992
查看次数

为什么这段代码会产生错误的P值?

我正在尝试计算与随时变系数的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)

r function cox-regression survival-analysis p-value

1
推荐指数
1
解决办法
67
查看次数