基于nmle包的R中多层次模型
我正在使用基于nmle包的R中多层次模型,r,statistics,nlme,R,Statistics,Nlme,我正在使用nlme软件包学习多层次模型,并遵循教科书“使用R发现统计数据”中的示例 该数据集为蜜月Period.dat,也可在其同伴网站下载 require(nlme) 要求(2) satisfactionData=read.delim(“蜜月期.dat”,标题=TRUE) 我已经看过这本书了。看来编码是错误的。您得到的错误是因为您的时间变量是一个因子,您需要它是数字。在书中作者的第一个数字中,他将时间表示为数字(0-3),但他的模型代码不正确。我已为您重新编码: ## First, Tim
nlme
软件包学习多层次模型,并遵循教科书“使用R发现统计数据”中的示例
该数据集为蜜月Period.dat
,也可在其同伴网站下载
require(nlme)
要求(2)
satisfactionData=read.delim(“蜜月期.dat”,标题=TRUE)
我已经看过这本书了。看来编码是错误的。您得到的错误是因为您的时间变量是一个因子,您需要它是数字。在书中作者的第一个数字中,他将时间表示为数字(0-3),但他的模型代码不正确。我已为您重新编码:
## First, Time needs to be recoded as a numeric
restructuredData$Time.Numeric <- with(restructuredData, ifelse(Time == "Satisfaction_Base", 0,
ifelse(Time == "Satisfaction_6_Months", 1,
ifelse(Time == "Satisfaction_12_Months", 2,
ifelse(Time == "Satisfaction_18_Months", 3, NA)))))
## Baseline Model
intercept <-gls(Life_Satisfaction~1, data = restructuredData, method = "ML", na.action = na.exclude)
summary(intercept)
## Model where intercept can vary for Individuals
randomIntercept <- lme(Life_Satisfaction ~ 1, data = restructuredData, random = ~1|Person, method = "ML", na.action = na.exclude, control = list(opt = "optim"))
summary(randomIntercept)
## Add time as a fixed effect
timeRI <- lme(Life_Satisfaction ~ Time.Numeric, data = restructuredData, random = ~1|Person, method = "ML", na.action = na.exclude, control = list(opt = "optim"))
summary(timeRI)
## Add a random slope to the model by nesting the Individual within the test time
timeRS <- lme(Life_Satisfaction ~ Time.Numeric, data = restructuredData, random = ~Time.Numeric|Person, method = "ML", na.action = na.exclude, control = list(opt = "optim"))
summary(timeRS)
## Modeling the covariance structure structure of the errors with a first-order autoregressive covariance structure
ARModel <- update(timeRS, correlation = corAR1(0, form = ~Time.Numeric|Person))
summary(ARModel)
anova(intercept, randomIntercept, timeRI, timeRS, ARModel)
##首先,时间需要重新编码为数字
我已经看过这本书了。看来编码是错误的。您得到的错误是因为您的时间变量是一个因子,您需要它是数字。在书中作者的第一个数字中,他将时间表示为数字(0-3),但他的模型代码不正确。我已为您重新编码:
## First, Time needs to be recoded as a numeric
restructuredData$Time.Numeric <- with(restructuredData, ifelse(Time == "Satisfaction_Base", 0,
ifelse(Time == "Satisfaction_6_Months", 1,
ifelse(Time == "Satisfaction_12_Months", 2,
ifelse(Time == "Satisfaction_18_Months", 3, NA)))))
## Baseline Model
intercept <-gls(Life_Satisfaction~1, data = restructuredData, method = "ML", na.action = na.exclude)
summary(intercept)
## Model where intercept can vary for Individuals
randomIntercept <- lme(Life_Satisfaction ~ 1, data = restructuredData, random = ~1|Person, method = "ML", na.action = na.exclude, control = list(opt = "optim"))
summary(randomIntercept)
## Add time as a fixed effect
timeRI <- lme(Life_Satisfaction ~ Time.Numeric, data = restructuredData, random = ~1|Person, method = "ML", na.action = na.exclude, control = list(opt = "optim"))
summary(timeRI)
## Add a random slope to the model by nesting the Individual within the test time
timeRS <- lme(Life_Satisfaction ~ Time.Numeric, data = restructuredData, random = ~Time.Numeric|Person, method = "ML", na.action = na.exclude, control = list(opt = "optim"))
summary(timeRS)
## Modeling the covariance structure structure of the errors with a first-order autoregressive covariance structure
ARModel <- update(timeRS, correlation = corAR1(0, form = ~Time.Numeric|Person))
summary(ARModel)
anova(intercept, randomIntercept, timeRI, timeRS, ARModel)
##首先,时间需要重新编码为数字
重组数据$Time.Numeric好建议!我简化了一点:restructuredData[,“Time”]好建议!我简化了一点:restructuredData[,“Time”]