2017-04-13 75 views
0

我試圖將沒有成功的nlme對象的結果可視化。當我使用lmer對象時,會創建正確的繪圖。我的目標是使用nlme並通過ggplot2可視化每個人的擬合增長曲線。 predict()函數似乎與nlmelmer對象的工作方式不同。使用nlme/ggplot2 vs lme4/ggplot2可視化多級增長模型

型號:

#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

+0

通過「可視化模型估計的隨機效應」是否意味着繪製每個人的擬合增長曲線?我想你可以改變'geom_line(aes(y = fitted(autoregressive),group = id))'或者開始加入'data $ fit <-fitted(autoregressive)' – Niek

+0

謝謝你回覆@Niek。我嘗試使用'fitted()',但是它爲每個人返回相同的擬合值。我更新了我的問題。謝謝! –

+1

你有一個可重複的例子嗎?沒有你的數據我不能看一看。嘗試用隨機生成的數據或公共數據集重現問題。 –

回答

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成功地適合。在模型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

###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的原因可逆係數矩陣的錯誤,它具有更簡單的組內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"做,爲什麼它的工作原理。