boxTidwell函数可以处理二进制结果变量吗?

时间:2014-12-03 18:13:03

标签: r

我最初想在我的前瞻Logistic回归模型(BinaryOutcomeVar~ContinplePredVar + ContinuousPredVar ^ 2 + ContinuousPredVar ^ 3)上运行boxTidwell()(在" car"包中找到)分析。我遇到了问题:

Error in x - xbar : non-numeric argument to binary operator
In addition: Warning message:
In mean.default(x) : argument is not numeric or logical: returning NA

因此,我创建了一个可重现的示例来演示错误:

不能工作:

boxTidwell(formula = Treatment ~ uptake, other.x = ~ poly(x = colnames(CO2)[c(1,2,4)], degree = 2), data = CO2)
  

boxTidwell(y = CO2 $治疗,x = CO2 $摄取)

使用:

boxTidwell(formula = prestige ~ income + education, other.x = ~ poly(x = women , degree = 2), data = Prestige)

我一直在讨论other.x参数,并猜测这是个问题。

问题

那么,有没有人知道1. boxTidwell()函数是否适用于二进制结果变量2. other.x背后的逻辑,因为我无法使用我的虚拟示例。

1 个答案:

答案 0 :(得分:0)

进一步搜索后,看起来像汽车::: boxTidwell不能处理公式中的二进制结果变量,但它可以手工编码:

require(MASS)
require(car)

d1<-read.csv("path for your csv file",sep=',',header=TRUE)

x<-d1$explanatory variable name
y<-d1$dependent variable name

#FIT IS DONE USING THE glm FUNCTION
m1res <- glm(y ~ x,family=binomial(link = "logit"))
coeff1<- coefficients(summary(m1res))
lnx<-x*log(x)
m2res <- glm(y ~ x+lnx ,family=binomial(link = "logit"))
coeff2<- coefficients(summary(m2res))
alpha0<-1.0
pvalue<-coeff2[3,4]
pvalue
beta1<-coeff1[2,1]
beta2<-coeff2[3,1]
iter<-0
err<-1
while (pvalue<0.1) {
alpha <-(beta2/beta1)+alpha0 
err<-abs(alpha-alpha0)
alpha0<-alpha
mx<-x^alpha
m1res <- glm(y ~ mx,family=binomial(link = "logit"))
coeff1<- coefficients(summary(m1res))
mlnx<-mx*log(x)
m2res <- glm(y ~ mx+mlnx ,family=binomial(link = "logit"))
coeff2<- coefficients(summary(m2res))
pvalue<-coeff2[3,4]
beta1<-coeff1[2,1]
beta2<-coeff2[3,1]
iter<- iter+1
}
# PRINT THE POWER TO CONSOLE
alpha

以上代码取自: https://sites.google.com/site/ayyalaprem/box-tidwelltransform