簡體   English   中英

使用nlme / ggplot2與lme4 / ggplot2可視化多級增長模型

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

我試圖將nlme對象的結果可視化,但沒有成功。 當我使用lmer對象執行此操作時,將創建正確的繪圖。 我的目標是使用nlme並使用ggplot2可視化每個人的擬合增長曲線。 對於nlmelmer對象, predict()函數的工作方式似乎有所不同。

模型:

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

正確的情節

在創建可再現例如,我發現錯誤未發生在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中則沒有,模型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" ,以及為什么會起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM