"subscript out of bounds" 我的自定义函数出错

"subscript out of bounds" error on my custom function

我决定尝试编写一个简单的自定义函数,对某些 lm() 产生的回归估计量进行 t 检验(例如,H_0:Beta_j = "some constant" 对比 H_1:Beta_j < "some constant")。

这是我第一次创建自己的代码,但我已经使用 R 几个月了,我认为我对它有一定的了解,所以我不明白为什么我总是得到 "subscript out of bounds" 在 运行 上。

我的代码:

custom_test<-function(data,coeff,alt,alternative=c("two.sided","greater","less"),clevel=.95){
  dof<-data$df.residual
  top<-data$coefficients["coeff"]-alt
  bottom=coef(summary(data))["coeff","Std. Error"]
  stat<-abs(top/bottom)
  if (alternative=="two.sided") {
    tstat<-qt(clevel/2,dof)
    pstat<-2*pt(tstat,dof)
    return(pstat)
  } else if (alternative=="greater") {
      tstat<-qt(clevel/2,dof)
      pstat<-pt(tstat,dof)
      return(pstat)
  } else if (alternative=="less") {
      tstat<-qt(clevel/2,dof)
      pstat<-pt(tstat,dof)
      return(pstat)
  } else {
      return("Error")
  }

}

然后我尝试 运行 使用标准 lm() 结果,hrsemp 是一个变量,并得到错误:

custom_test(fit9,hrsemp,0,alternative="less")
Error in coef(summary(data))["coeff", "Std. Error"] : 
  subscript out of bounds

但每次我自己手动 运行 有问题的代码时,我都会得到答案:

> coef(fit9)
(Intercept)      hrsemp  log(sales) log(employ) 
12.45837237 -0.02926893 -0.96202698  0.76147045 
> coef(summary(fit9))["hrsemp", "Std. Error"]
[1] 0.02280484

有关此错误的其他 Stack Exchange 问题似乎都略有不同,到目前为止我还无法将他们的经验教训概括为我的代码。

我哪里错了?

Frank是对的;你得到这个错误的原因与其他人一样:你试图访问一个不存在的对象的元素。更具体地说,在您的情况下,您试图访问 coef(summary(data))"coeff" 行和 "Std. Error" 列中的元素。这是一个问题,因为可能没有名为 "coeff" 的系数。您想执行以下操作:

custom_test<-function(data,coeff,alt,alternative=c("two.sided","greater","less"),clevel=.95){
    dof<-data$df.residual
    top<-data$coefficients[coeff]-alt
    bottom=coef(summary(data))[coeff,"Std. Error"]
    stat<-abs(top/bottom)
    if (alternative=="two.sided") {
        tstat<-qt(clevel/2,dof)
        pstat<-2*pt(tstat,dof)
        return(pstat)
    } else if (alternative=="greater") {
        tstat<-qt(clevel/2,dof)
        pstat<-pt(tstat,dof)
        return(pstat)
    } else if (alternative=="less") {
        tstat<-qt(clevel/2,dof)
        pstat<-pt(tstat,dof)
        return(pstat)
    } else {
        return("Error")
    }

}

并将变量名作为字符串传递:

set.seed(42)
hrsemp <- rnorm(10)
Y <- 1 + 5 * hrsemp + rnorm(10)
fit9 <- lm(Y ~ hrsemp)
custom_test(fit9, 'hrsemp', 0, alternative="less")
[1] 0.475

(请注意,您也可以将实际变量对象提供给函数并使用 deparse(substitute(coeff))——例如,请参阅 this SO question)。

但是,您可能会注意到这给了您错误的答案。那是因为你的函数写错了。你可能想要更多这样的东西:

custom_test <- function(data, coeff, alt,
                        alternative = c("two.sided", "greater", "less"),
                        clevel = .95){
    dof <- data$df.residual
    top <- data$coefficients[coeff] - alt
    bottom <- coef(summary(data))[coeff, "Std. Error"]
    stat <- abs(top/bottom)
    if ( alternative == "two.sided" ) {
        return(2 * (1 - pt(stat, dof)))
    } else if ( alternative == "greater" ) {
        return(1 - pt(stat, dof))
    } else if ( alternative == "less" ) {
        return(1 - pt(stat, dof))
    } else {
        stop("Provide a valid alternative hypothesis.", call.=FALSE)
    }
}


custom_test(fit9, 'hrsemp', 0, alternative="less")
      hrsemp 
7.858176e-05 
custom_test(fit9, 'hrsemp', 0, alternative="two.sided")
      hrsemp 
0.0001571635 
coef(summary(fit9))['hrsemp', 'Pr(>|t|)']
[1] 0.0001571635

可以找到为什么这是正确计算的许多很好的解释之一 here