2016-12-20 13 views
0

나는 다중 레벨 모델을 배우기 위해 nlme 패키지를 사용하고, 일어 났을 때 "Discovering Statistics Using R"교과서의 예제를 사용하고있다. 오류 nlme 사용; 기형 인자

Mixed Models Code

데이터 세트

또한 그들의 동반자 웹 사이트에서 다운로드 할 수있는, 허니문 Period.dat입니다. 나는 "타이머"모델을 업데이트하려고 할 때
require(nlme) 
require(reshape2) 
satisfactionData = read.delim("Honeymoon Period.dat", header = TRUE) 

restructuredData<-melt(satisfactionData, id = c("Person", "Gender"), measured = c("Satisfaction_Base", "Satisfaction_6_Months", "Satisfaction_12_Months", "Satisfaction_18_Months")) 
names(restructuredData)<-c("Person", "Gender", "Time", "Life_Satisfaction") 


#print(restructuredData) 
#restructuredData.sorted<-restructuredData[order(Person),] 

intercept <-gls(Life_Satisfaction~1, data = restructuredData, method = "ML", na.action = na.exclude) 
randomIntercept <-lme(Life_Satisfaction ~1, data = restructuredData, random = ~1|Person, method = "ML", na.action = na.exclude, control = list(opt="optim")) 
anova(intercept, randomIntercept) 

timeRI<-update(randomIntercept, .~. + Time) 
timeRS<-update(timeRI, random = ~Time|Person) 
ARModel<-update(timeRS, correlation = corAR1(0, form = ~Time|Person)) 

오류

Data Set - Multilevel Models

,이 순간에 발생했습니다. 오류는 다음과 같습니다.

Error in as.character.factor(X[[i]], ...) : malformed factor 

이 모든 것을 아는 사람/프로그래머는 누구입니까?

답변

3

나는이 책을 보았다. 코딩이 잘못되었습니다. 시간 변수가 하나의 요소이므로 숫자로 입력해야하기 때문에 오류가 발생합니다. 책의 저자의 첫 번째 그림에서 그는 시간을 숫자 (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) 

는 ANOVA 이제 정확히 같은 책에 표시됩니다 모델 비교를 읽어.

+1

좋은 조언! 'restructuredData [, "Time"] <- as.numeric (restructuredData [, "Time"]) - 1'이것은 첫 번째 모델 정의 전에 실행될 수 있습니다 :'intercept <-gls (Life_Satisfaction ~ 1 , data = restructuredData, method = "ML", na.action = na.exclude)'나머지 코드는 변경하지 않아도됩니다. –