R语言crossCarry包CrossGEEKron函数报错:replacement has length zero
重复测量交叉研究CrossGEEKron函数报错解决
问题背景
我正在开展一项包含2个周期、2种处理、3个时间点的重复测量交叉研究分析,运行CrossGEEKron函数时触发如下错误,且确认数据格式与演示数据Arterial一致,无法定位原因。
报错信息
Error in r_media[ii, ] <- media_temp/(n_suj) : replacement has length zero
运行过程输出:
Beginning Cgee S-function, @(#) geeformula.q 4.13 98/01/27 running glm to get initial regression estimate (Intercept) period2 treatment2 Carry_2 time 78.71847 -2.05556 -6.94444 1.88889 0.06081 Error in r_media[ii, ] <- media_temp/(n_suj) : replacement has length zero
所用代码
carrydata<- createCarry(data=rop, treatment= "treatment", period= "period", id="sno") data<- carrydata$data carry<- carrydata$carryover model<- CrossGEEKron(response= "syst", treatment = "treatment", period = "period", id= "sno", time = "time", carry= "Carry_2", data=data, correlation="ar1")
数据集示例
time babysuhidno sno syst diast hr period treatment 1 0 104944203 104944203 82 36 172 1 2 2 0 104944203 104944203 76 41 171 2 1 3 0 10583361 10583361 84 47 170 1 2 4 0 10583361 10583361 87 42 156 2 1 5 0 105855122 105855122 83 44 153 1 1 6 0 105855122 105855122 65 33 167 2 2 7 0 105855143 105855143 71 32 168 1 1 8 0 105855143 105855143 76 43 165 2 2 9 0 105904967 105904967 53 26 177 1 2 10 0 105904967 105904967 73 52 163 2 1 11 20 104944203 104944203 81 40 174 1 2 12 20 104944203 104944203 81 51 170 2 1 13 20 10583361 10583361 85 39 181 1 2 14 20 10583361 10583361 83 57 157 2 1 15 20 105855122 105855122 80 36 165 1 1 16 20 105855122 105855122 61 31 171 2 2 17 20 105855143 105855143 72 33 165 1 1 18 20 105855143 105855143 76 41 162 2 2 19 20 105904967 105904967 54 27 172 1 2 20 20 105904967 105904967 82 42 152 2 1 21 35 104944203 104944203 75 33 177 1 2 22 35 104944203 104944203 81 42 159 2 1 23 35 10583361 10583361 80 37 173 1 2 24 35 10583361 10583361 83 48 153 2 1 25 35 105855122 105855122 88 45 156 1 1 26 35 105855122 105855122 72 43 174 2 2 27 35 105855143 105855143 85 66 154 1 1 28 35 105855143 105855143 75 42 133 2 2 29 35 105904967 105904967 62 32 177 1 2 30 35 105904967 105904967 71 41 141 2 1
解决方案
这个错误通常是CrossGEEKron函数无法正确识别受试者的重复测量结构导致的,可按以下步骤排查修复:
转换time变量类型:确保
time为因子类型,函数需要时间点作为分类变量构建相关结构,执行代码:data$time <- as.factor(data$time)检查时间点完整性:确认每个受试者的所有周期、处理组合下,3个时间点记录完整,用代码排查缺失:
library(dplyr) data %>% group_by(sno, period, treatment) %>% summarise(n_time = n_distinct(time)) %>% filter(n_time != 3)若输出非空,补全或剔除不完整的受试者数据。
调整相关结构参数:尝试将
correlation从"ar1"改为"exchangeable",ar1结构对测量顺序要求更严格,换用exchangeable可绕过部分场景的错误:model<- CrossGEEKron(response= "syst", treatment = "treatment", period = "period", id= "sno", time = "time", carry= "Carry_2", data=data, correlation="exchangeable")验证carry变量有效性:检查
Carry_2无缺失值或异常水平:table(data$Carry_2, useNA = "always")若存在异常,重新运行
createCarry并核对参数。
内容的提问来源于stack exchange,提问作者Vivek Kumar
相关产品推荐
相关产品推荐

