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

时间:2017-04-13 01:23:10

标签: r ggplot2 nlme

我试图将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) 

incorrect visualization

当我使用时:

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

它为每个人返回相同的拟合值,因此ggplot为每个人生成相同的增长曲线。运行test <-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))) 

correct plot

1 个答案:

答案 0 :(得分:0)

在创建可重现的示例时,我发现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))

引入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))

但不是模型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"的原因及其工作原理。