[英]R, LME and Tukey Test produces error after sorting
在下面,您將找到一個簡化的代碼片段,該片段可以tp重現我得到的錯誤:
rm(list=ls(all=TRUE))
rawdata <- read.table(file = "test.csv", header=TRUE, sep=",", dec=".", stringsAsFactors = TRUE)
sets <- levels(as.factor(rawdata[,'set']))
index <- rawdata[,'set']==sets[1]
testdata <- rawdata[index,]
testdata$name <- as.factor(testdata$name)
testdata$name <- factor(testdata$name, as.character(testdata$name))
#### sorting step
sortindex <- with(testdata,order(name))
testdata <- testdata[sortindex,]
####
testdata.lme <- lme (var~name, random=~1|sample,testdata, method='REML')
testdata.lme.tuk <- summary(glht(testdata.lme, linfct=mcp(name='Tukey')))
錯誤是以下之一:
Error in glht.matrix(model = list(modelStruct = list(reStruct = list(sample = -10.3240629947066)), :
‘ncol(linfct)’ is not equal to ‘length(coef(model))’
如果省去了排序步驟,則不會產生該結果。 但是,我需要在較大的代碼中進行排序以用於其他功能和數據清理。 我也希望第一個變量代表我的控件,因此代表截距。
之前在其他一些博客中也發生過相同的問題,但要么沒有解決,要么是數據清理問題,否則就沒有。 有人有主意嗎?
下面列出了我在這里使用的數據,但是我認為只要包含一個子集和一個排序步驟,該錯誤就可以重現:
name var sample set
423 10.31 1 1
423 10.39 1 1
423 10.86 1 1
421 10.75 1 1
421 10.24 1 1
421 10.27 1 1
424 10.75 1 1
424 10.75 1 1
424 10.75 1 1
423 10.14 2 1
423 10.85 2 1
423 10.48 2 1
425 10.56 1 1
425 10.67 1 1
425 10.17 1 1
426 10.50 1 1
426 10.30 1 1
426 10.29 1 1
423 10.57 3 1
423 10.07 3 1
423 10.01 3 1
428 10.26 1 1
428 10.17 1 1
428 10.72 1 1
429 10.88 1 1
429 10.93 1 1
429 10.63 1 1
423 10.38 4 1
423 10.54 4 1
423 10.25 4 1
432 10.72 1 1
432 10.62 1 1
432 10.14 1 1
434 10.45 1 1
434 10.38 1 1
434 10.41 1 1
435 10.64 1 1
435 10.21 1 1
435 10.21 1 1
423 10.46 5 1
423 10.41 5 1
423 10.13 5 1
501 10.09 1 1
501 10.86 1 1
501 10.05 1 1
503 10.22 1 1
503 10.94 1 1
503 10.38 1 1
423 10.31 1 2
423 10.39 1 2
423 10.86 1 2
421 10.75 1 2
421 10.24 1 2
421 10.27 1 2
424 10.75 1 2
424 10.75 1 2
424 10.75 1 2
423 10.14 2 2
423 10.85 2 2
423 10.48 2 2
425 10.56 1 2
425 10.67 1 2
425 10.17 1 2
426 10.50 1 2
426 10.30 1 2
426 10.29 1 2
423 10.57 3 2
423 10.07 3 2
423 10.01 3 2
428 10.26 1 2
428 10.17 1 2
428 10.72 1 2
429 10.88 1 2
429 10.93 1 2
429 10.63 1 2
423 10.38 4 2
423 10.54 4 2
423 10.25 4 2
432 10.72 1 2
432 10.62 1 2
432 10.14 1 2
434 10.45 1 2
434 10.38 1 2
434 10.41 1 2
435 10.64 1 2
435 10.21 1 2
435 10.21 1 2
423 10.46 5 2
423 10.41 5 2
423 10.13 5 2
501 10.09 1 2
501 10.86 1 2
501 10.05 1 2
503 10.22 1 2
503 10.94 1 2
503 10.38 1 2
您要做的就是在子集或排序步驟之后再次將factor()
函數應用於變量:
testdata$name <- factor(testdata$name)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.