Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
model.frame.default中的R错误:使用lm时对象不是矩阵_R_Loops_Matrix_Formula_Lm - Fatal编程技术网

model.frame.default中的R错误:使用lm时对象不是矩阵

model.frame.default中的R错误:使用lm时对象不是矩阵,r,loops,matrix,formula,lm,R,Loops,Matrix,Formula,Lm,对于可复制的示例: test点仅适用于公式的RHS。原因可能是,如果数据集中有两列以上(与您的特殊情况相反),则会出现歧义。您可以使用重新格式化,这有助于创建用于lm的公式 for (j in colnames(test)[index]) { for (i in 1:2) { tmp <- test[test$IDcount == i, c("Otminus1", j)] if(any(colSums(!is.na(tmp)) == 0))

对于可复制的示例:


test点
仅适用于公式的RHS。原因可能是,如果数据集中有两列以上(与您的特殊情况相反),则会出现歧义。您可以使用
重新格式化
,这有助于创建用于
lm
的公式

for (j in colnames(test)[index]) {
  for (i in 1:2) {
    tmp <- test[test$IDcount == i, c("Otminus1", j)]
    if(any(colSums(!is.na(tmp)) == 0)) 
      betas[i, ] <- NA
    else {
      fo <- reformulate(names(tmp)[1], names(tmp)[2], intercept=FALSE)
      betas[i,] <- coef(lm(fo, tmp))
    }
  }
  betas <- data.frame(betas)
  results[[j]] <- betas$beta
}

results
#   IDcount         N.1        N.2       N.3       N.4  N.5
# 1       1 -0.03167421 0.07420163 0.1065183 -0.173913   NA
# 2       2  0.64615385 1.10714286 1.1000000 -2.500000 -6.5
给出这个矩阵

combs
#         [,1]    [,2]   
# [1,] "N.1,1" "N.1,2"
# [2,] "N.2,1" "N.2,2"
# [3,] "N.3,1" "N.3,2"
# [4,] "N.4,1" "N.4,2"
# [5,] "N.5,1" "N.5,2"
现在我们通过
sapply
循环
combs
,在
处做一个
strsplit
,使用第一个值作为IV,第二个值作为
测试数据的子集。不可能的迭代将产生错误(IDcount==1的
N.5
情况),因此我们使用
tryCatch
,并让代码在这种情况下抛出
NA

res <- sapply(combs, function(v) {
  x <- el(strsplit(v, ","))
  tmp <- test[test$IDcount == x[2], ]
  tryCatch(lm(as.formula(paste0(x[1], "~ Otminus1 - 1")), tmp)$coe,
           error=function(e) NA)
})

@ConnorUhl好像我把x和y互换了,你现在能试试吗?现在它工作得很好!!非常感谢@ConnorUhl只是将该值设置为
NA
,而不是使用
next
,请参阅editet版本。再次感谢@欢迎使用ConnorUhl,另请参见不带
的替代解决方案,了解
循环。
res <- sapply(combs, function(v) {
  x <- el(strsplit(v, ","))
  tmp <- test[test$IDcount == x[2], ]
  tryCatch(lm(as.formula(paste0(x[1], "~ Otminus1 - 1")), tmp)$coe,
           error=function(e) NA)
})
matrix(res, 2, byrow=TRUE, dimnames=list(n.IDcount, i.vars))
#           N.1        N.2       N.3       N.4  N.5
# 1 -0.03167421 0.07420163 0.1065183 -0.173913   NA
# 2  0.64615385 1.10714286 1.1000000 -2.500000 -6.5