0
我正在使用nlme軟件包來學習多層次模型,並遵循教科書「Discovering Statistics Using R」的示例。錯誤使用nlme;畸形因子
的數據集是蜜月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))
的錯誤發生在這一刻,當我試圖更新「定時器」模式。錯誤如下:
Error in as.character.factor(X[[i]], ...) : malformed factor
任何統計人員/程序員在這裏誰知道這是什麼意思?
很好的建議!我簡化了一下:'restructuredData [,'Time'] < - as.numeric(restructuredData [,'Time「]) - 1'這可以在第一個模型定義之前運行:'攔截<-gls(Life_Satisfaction〜1 ,data = restructuredData,method = 「ML」,na.action = na.exclude)'而不需要改變其餘的代碼。 –