R中的logitmfx错误以计算健壮的群集标准错误

问题描述

我希望进行logit回归,以预测平均家庭规模和年龄的边际效应,以及二元指标(无论个人是移民,有健康保险还是吸烟)对预期概率的影响发展中的高血压。

此数据来自聚类调查,我希望在输出中包括可靠的聚类标准误差。

但是,当我添加代码以包含健壮的集群SE时,我收到一个错误,即不再找到回归变量,并且不确定为什么。任何建议都很好!谢谢。

AGE       IMMIGRANT     FAMSIZE     HLTH_INS    HYPERTEN   SMOKE    PSU
<int>       <dbl>         <int>       <dbl>       <dbl>     <dbl>  <int>
40           0              2          1            0         0      2
23           0              2          1            0         0      1
24           0              2          1            0         0      2
18           0              3          1            1         0      2
30           0              2          1            0         0      2
33           1              6          0            0         0      1

#or if this is an easier output to reproduce:
structure(list(AGE = c(40L,23L,24L,18L,30L,33L,32L,63L,22L,24L),IMMIGRANT = c(0,1,1),FAMSIZE = c(2L,2L,3L,6L,1L,1L),HLTH_INS = c(1,0),HYPERTEN = c(0,SMOKE = c(0,PSU = c(2L,2L)),row.names = c(NA,-10L),class = "data.frame")


#The regression works without adjusting for clustered SE 
logit<-logitmfx(HYPERTEN~scale(AGE)+IMMIGRANT+scale(FAMSIZE)+HLTH_INS+
                 SMOKE,data=sample,atmean=TRUE,robust=T)


#However,when I add in the code to cluster SE I receive the error: "Error in scale(AGE) : object 'AGE' not found" 
logit<-logitmfx(HYPERTEN~scale(AGE)+IMMIGRANT+scale(FAMSIZE)+HLTH_INS+
                 SMOKE,robust=T,clustervar1="PSU",clustervar2=NULL,!is.null("PSU")) 

解决方法

尝试使用源代码复制功能的步骤,Steffen Moritz的解决方案确实应该起作用。由于第一个logitmfx立即调用了另一个函数logitmfxest,因此出现了问题。

此函数具有相同的参数,但也具有以下代码:

if(!is.null(clustervar1)){
    if(is.null(clustervar2)){
      if(!(clustervar1 %in% names(data))){
        stop("clustervar1 not in data.frame object")
      }    
      data = data.frame(model.frame(formula,data,na.action=NULL),data[,clustervar1])
      names(data)[dim(data)[2]] = clustervar1
      data=na.omit(data)
    }
    if(!is.null(clustervar2)){
      if(!(clustervar1 %in% names(data))){
        stop("clustervar1 not in data.frame object")
      }    
      if(!(clustervar2 %in% names(data))){
        stop("clustervar2 not in data.frame object")
      }    
      data = data.frame(model.frame(formula,c(clustervar1,clustervar2)])
      names(data)[c(dim(data)[2]-1):dim(data)[2]] = c(clustervar1,clustervar2)
      data=na.omit(data)
    }
  }

根据您的情况,以下代码将被激活:

if(!is.null(clustervar1)){
    if(is.null(clustervar2)){  
      data = data.frame(model.frame(formula,clustervar1])
      names(data)[dim(data)[2]] = clustervar1
      data=na.omit(data)
    }
  }

这将“数据”重新定义为在model.frame上构建的data.frame。但是模型框架使用公式中的名称,因此第2列突然称为 scale.AGE。。第3列称为 scale.FAMSIZE。

这是一个大问题,因为该函数随后调用了广义线性模型:

fit = glm(formula,data=data,family = binomial(link = "logit"),x=T,start = start,control = control) 

它使用包含scale(AGE)和scale(FAMSIZE)的原始公式,但使用具有重命名列的新数据框。

因此在输入之前进行缩放应该有效。确实,正如史蒂芬(Steffen)所述,其他任何函数也会导致相同的错误,因为调用model.frame时,它们将产生类似的列重命名。

,

奇怪,以某种方式无法再识别formula中的功能。 您可以尝试执行此操作,如果删除scale,则可以正常运行。同样,没有其他功能像log()起作用。

您可以尝试先计算比例尺(AGE),然后不必将其放入公式中。

可能看起来像这样:

sample$AGE<-scale(sample$AGE)
sample$FAMSIZE<-scale(sample$FAMSIZE)

form <- as.formula(HYPERTEN~AGE+IMMIGRANT+FAMSIZE+HLTH_INS+SMOKE)
#However,when I add in the code to cluster SE I receive the error: "Error in scale(AGE) : object 'AGE' not found"
logit<-logitmfx(form,data=sample,atmean=TRUE,robust=T,clustervar1="PSU",clustervar2=NULL)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...