代码之家  ›  专栏  ›  技术社区  ›  Francisco

关于错误的问题:$operator对原子向量无效

  •  0
  • Francisco  · 技术社区  · 2 年前

    我创建了这个函数来自动进行多项回归:

    multinom<-function(form,ref,db){
      library(VGAM)
      mod<-vglm(formula(form),multinomial(refLevel = ref),data = db)
      t1<-exp(cbind(OR = coef(mod), confint(mod)))
      p_val<-coef(summary(mod))[,4]
      x<-data.frame(cbind(t1,p_val))
      x$prob<-x[,1]/(1+x[,1])
      colnames(x)<-c("OR","CI_min","CI_max","P_val","Probablity")
      return(x)
    }
    

    当这个函数在脚本中使用时,它运行得很好。我还在正在编写的程序包中添加了此功能:

    ##############################
    #' Statistical analysis: Multinomial Regression
    #'
    #'The purpose of this function is to calculate the Odds Ratio for a statistical analysis that involves a categorical dependent variable, with more than two unordered levels or classes, and different independent variables of any kind.
    #'The Odds Ratio is obtained using a multinominal logit regression. This is an extension of the binomial logistic regression to allow for a categorical dependent variable with more than two unordered categories.
    #'In multinomial regression we need to define a reference variable category. The model will determine several binomial distribution parameters with respect to the reference category.
    #' @param form model formula. For example: `dep_var~conf_var1+conf_var2+conf_var3`.
    #' @param ref reference category from the dependent variable. If its a character variable the reference level must be included in `""`.
    #' @param df D.B_Final database obtained from the data incorporation step.
    #' @return A table with the Odds ratio, its 95% CI and the associated p-value.
    #' @export
    #' @examples
    #' # Basic Usage of Multinomial Regression.
    #' multinom(cov_2~cov_3+var_con,"A",D.B_Final)
    #'
    multinom<-function(form,ref,db){
      library(VGAM)
      mod<-vglm(formula(form),multinomial(refLevel = ref),data = db)
      t1<-exp(cbind(OR = coef(mod), confint(mod)))
      p_val<-coef(summary(mod))[,4]
      x<-data.frame(cbind(t1,p_val))
      x$prob<-x[,1]/(1+x[,1])
      colnames(x)<-c("OR","CI_min","CI_max","P_val","Probablity")
      return(x)
    }
    

    当此功能在相应的包中使用时,具体为 zoombedo::multinorm ,我遇到错误消息: Error in object$coefficients: $ operator is invalid for atomic vectors.

    有人能帮我了解一下这里发生了什么吗?

    1 回复  |  直到 2 年前
        1
  •  4
  •   Ben Bolker    2 年前

    我没有一个完美的解决方案,但我想我可以解释这个问题。几乎可以肯定的是,这个错误来自于 coef(summary(mod))[,4] 应该 正在呼叫 coef() 类的对象的方法 summary.vglm :

    selectMethod("coef", "summary.vglm")
    Method Definition:
    
    function (object, ...) 
    object@coef3
    <bytecode: 0x5617c6d9d8e8>
    <environment: namespace:VGAM>
    

    这显然在包上下文中找不到。相反,你得到的是 stats:::coef.default() :

    function (object, complete = TRUE, ...) 
    {
        cf <- object$coefficients
        if (complete) 
            cf
        else cf[!is.na(cf)]
    }
    

    由于对象没有 $coefficient 元素,则会出现错误。

    解决此问题的方法是访问 @coef3 直接插入插槽。

    使用 importMethodsFrom("VGAM", "summary") 在您的 NAMESPACE 文件(或 @importMethodsFrom 如果你使用的是roxygen),也许 importClassesFrom("VGAM", "summary.vglm") 可以 解决问题(请参阅 R Extensions 文档),但我还没有测试。

    推荐文章