I have created this function to automate multinomial regression:
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)
}
When this function is used in a script, it works just fine. I have also added this function to a package I am writing:
##############################
#' 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)
}
When this function is used inside the corresponding package, specifically as zoombedo::multinorm
, I encounter the error message: Error in object$coefficients: $ operator is invalid for atomic vectors.
Can anyone help me to understand what is going on here?
I don't have a perfect solution but I think I can explain the problem. The error is almost certainly coming from your coef(summary(mod))[,4]
. This should be calling the coef()
method for an object of class summary.vglm
:
selectMethod("coef", "summary.vglm")
Method Definition:
function (object, ...)
object@coef3
<bytecode: 0x5617c6d9d8e8>
<environment: namespace:VGAM>
This is apparently not found in the package context. Instead, what you get is stats:::coef.default()
:
function (object, complete = TRUE, ...)
{
cf <- object$coefficients
if (complete)
cf
else cf[!is.na(cf)]
}
Since the object doesn't have an $coefficient
element, you get the error.
The hacky way to solve this would be to access the @coef3
slot directly.
Using importMethodsFrom("VGAM", "summary")
in your NAMESPACE
file(or @importMethodsFrom
if you're using roxygen) and maybe importClassesFrom("VGAM", "summary.vglm")
might solve the problem (see the R Extensions document), but I haven't tested.