简体   繁体   English

使用nlme / ggplot2与lme4 / ggplot2可视化多级增长模型

[英]Visualize Multilevel Growth Model with nlme/ggplot2 vs lme4/ggplot2

I am trying to visualize the results of an nlme object without success. 我试图将nlme对象的结果可视化,但没有成功。 When I do so with an lmer object, the correct plot is created. 当我使用lmer对象执行此操作时,将创建正确的绘图。 My goal is to use nlme and visualize a fitted growth curve for each individual with ggplot2 . 我的目标是使用nlme并使用ggplot2可视化每个人的拟合增长曲线。 The predict() function seems to work differently with nlme and lmer objects. 对于nlmelmer对象, predict()函数的工作方式似乎有所不同。

model: 模型:

#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 visualization attempt: 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) 

不正确的可视化

when I use: 当我使用时:

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

it returns the same fitted values for each individual and so ggplot produces the same growth curve for each. 它为每个个体返回相同的拟合值,因此ggplot为每个个体生成相同的增长曲线。 Running test <-data.frame(ranef(autoregressive, level=1)) returns varying intercepts and slopes by patient id. 运行test <-data.frame(ranef(autoregressive, level=1))将根据患者ID返回不同的截距和斜率。 Interestingly, when I fit the model with lmer and run the below code it returns the correct plot. 有趣的是,当我使用lmer拟合模型并运行以下代码时,它将返回正确的图。 Why does predict() work differently with nlme and lmer objects? 为什么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))) 

正确的情节

In creating a reproducible example, I found that the error was not occurring in predict() nor in ggplot() but instead in the lme model. 在创建可再现例如,我发现错误未发生在predict()也不在ggplot()而是在lme模型。

Data: 数据:

###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)

Model 1 with no NA values fits successfully. 没有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))

Introducing NA's causes invertible coefficient matrix error in model 1. 引入NA会导致模型1中的可逆系数矩阵误差。

###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))

But not in model 2, which has a more simplistic within-group AR(1) correlation structure. 但是在模型2中则没有,模型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))

However, changing opt="optim" to opt="nlminb" fits model 1 successfully. 但是,将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))

The code below visualizes model 3 (formerly model 1) successfully. 下面的代码成功地可视化了模型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) 

Note that I am not exactly sure what changing the optimizer from "optim" to "nlminb" does and why it works. 请注意,我不确定如何将优化器从"optim"更改为"nlminb" ,以及为什么会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM