【问题标题】:Visualize Multilevel Growth Model with nlme/ggplot2 vs lme4/ggplot2使用 nlme/ggplot2 与 lme4/ggplot2 可视化多级增长模型
【发布时间】:2017-04-13 01:23:09
【问题描述】:

我试图可视化 nlme 对象的结果但没有成功。当我使用lmer 对象执行此操作时,会创建正确的绘图。我的目标是使用nlme 并使用ggplot2 可视化每个人的拟合增长曲线。 predict() 函数似乎与 nlmelmer 对象的工作方式不同。

型号:

#AR1 with REML
autoregressive <- lme(NPI ~ time,
                  data = data,
                  random = ~time|patient,
                  method = "REML",
                  na.action = "na.omit",
                  control = list(maxlter=5000, opt="optim"),
                  correlation = corAR1())

nlme可视化尝试:

data <- na.omit(data)

data$patient <- factor(data$patient,
                   levels = 1:23)

ggplot(data, aes(x=time, y=NPI, colour=factor(patient))) +
    geom_point(size=1) +
    #facet_wrap(~patient) +
    geom_line(aes(y = predict(autoregressive,
                              level = 1)), size = 1) 

当我使用时:

data$fit<-fitted(autoregressive, level = 1) 
geom_line(aes(y = fitted(autoregressive), group = patient))

它为每个人返回相同的拟合值,因此 ggplot 为每个人生成相同的增长曲线。运行test &lt;-data.frame(ranef(autoregressive, level=1)) 根据患者 ID 返回不同的截距和斜率。有趣的是,当我使用lmer 拟合模型并运行以下代码时,它会返回正确的图。 为什么predict()nlmelmer 对象的工作方式不同?

timeREML <- lmer(NPI ~ time + (time | patient), 
                 data = data,
                 REML=T, na.action=na.omit)

ggplot(data, aes(x = time, y = NPI, colour = factor(patient))) +
    geom_point(size=3) +
    #facet_wrap(~patient) +
    geom_line(aes(y = predict(timeREML))) 

【问题讨论】:

  • “可视化模型估计的随机效应”是指绘制每个个体的拟合增长曲线吗?我认为您可以更改geom_line(aes(y = fitted(autoregressive), group = id)) 或从添加data$fit&lt;-fitted(autoregressive) 开始
  • 感谢您回复@Niek。我尝试使用fitted(),但它为每个人返回相同的拟合值。我在上面更新了我的问题。谢谢!
  • 你有可重现的例子吗?没有你的数据,我无法查看。尝试使用随机生成的数据或公共数据集重现问题。

标签: r ggplot2 nlme


【解决方案1】:

在创建一个可重现的示例时,我发现predict()ggplot() 中都没有发生错误,而是在lme 模型中发生了错误。

数据:

###libraries
library(nlme)
library(tidyr)
library(ggplot2)

###example data
df <- data.frame(replicate(78, sample(seq(from = 0, 
            to = 100, by = 2), size = 25, 
            replace = F)))

##add id
df$id <- 1:nrow(df)

##rearrange cols
df <- df[c(79, 1:78)]

##sort columns
df[,2:79] <- lapply(df[,2:79], sort)

##long format
df <- gather(df, time, value, 2:79)

##convert time to numeric
df$time <- factor(df$time)
df$time <- as.numeric(df$time)

##order by id, time, value
df <- df[order(df$id, df$time),]

##order value
df$value <- sort(df$value)

没有 NA 值的模型 1 成功拟合。

###model1
model1 <- lme(value ~ time,
                  data = df,
                  random = ~time|id,
                  method = "ML",
                  na.action = "na.omit",
                  control = list(maxlter=5000, opt="optim"),
                  correlation = corAR1(0, form=~time|id,
                                       fixed=F))

在模型 1 中引入 NA 会导致可逆系数矩阵误差。

###model 1 with one NA value
df[3,3] <- NA

model1 <- lme(value ~ time,
                  data = df,
                  random = ~time|id,
                  method = "ML",
                  na.action = "na.omit",
                  control = list(maxlter=2000, opt="optim"),
                  correlation = corAR1(0, form=~time|id,
                                       fixed=F))

但不是在模型 2 中,它具有更简单的组内 AR(1) 相关结构。

###but not in model2
model2 <- lme(value ~ time,
                  data = df,
                  random = ~time|id,
                  method = "ML",
                  na.action = "na.omit",
                  control = list(maxlter=2000, opt="optim"),
                  correlation = corAR1(0, form = ~1 | id))

但是,将 opt="optim" 更改为 opt="nlminb" 成功适合模型 1。

###however changing the opt to "nlminb", model 1 runs 
model3 <- lme(value ~ time,
          data = df,
          random = ~time|id,
          method = "ML",
          na.action = "na.omit",
          control = list(maxlter=2000, opt="nlminb"),
          correlation = corAR1(0, form=~time|id,
                               fixed=F))

下面的代码成功地可视化了模型 3(以前的模型 1)。

df <- na.omit(df)

ggplot(df, aes(x=time, y=value)) +
    geom_point(aes(colour = factor(id))) +
    #facet_wrap(~id) +
    geom_line(aes(y = predict(model3, level = 0)), size = 1.3, colour = "black") +
    geom_line(aes(y = predict(model3, level=1, group=id), colour = factor(id)), size = 1) 

请注意,我不确定将优化器从 "optim" 更改为 "nlminb" 的作用以及它为何起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多