Tomas
Tomas

Reputation: 59525

Binomial glm in `rsq` package: error: object not found

It seems that whenever I use any of the rsq package functions (pcor for partial correlations; rsq and rsq.partial for R-squared) on a binomial glm which uses the two-column notation, I get an error - see below. The model actually is correct, fit goes perfectly, no data missing.

Is there something I can do about it?

Reproducible example:

require(rsq)
data(esoph)
model1 <- glm(cbind(ncases, ncontrols) ~ agegp + tobgp * alcgp,
              data = esoph, family = binomial)

pcor(model1)

Error in cbind(ncases, ncontrols) : object 'ncases' not found

rsq(model1)

Error in cbind(ncases, ncontrols) : object 'ncases' not found

rsq.partial(model1)

Error in cbind(ncases, ncontrols) : object 'ncases' not found

Upvotes: 3

Views: 871

Answers (2)

UseR10085
UseR10085

Reputation: 8188

You have to use attach(esoph) before applying the model. Like

data(esoph)


model1 <- glm(cbind(ncases, ncontrols) ~ agegp + tobgp * alcgp,
              data = esoph, family = binomial)

attach(esoph)
pcor(model1)
# $adjustment
#[1] FALSE
#$variable
#[1] "agegp"       "tobgp"       "alcgp"       "tobgp:alcgp"
#$partial.cor
#[1] 0.8092124 0.0000000 0.0000000 0.3815876

#Warning message:
#In (nLevels > 1) & (varcls == "factor") :
#longer object length is not a multiple of shorter object length

rsq(model1)
# [1] 0.826124

rsq.partial(model1)
#$adjustment
#[1] FALSE
#$variable
#[1] "agegp"       "tobgp"       "alcgp"       "tobgp:alcgp"
#$partial.rsq
#[1]  6.548247e-01 -6.661338e-16  0.000000e+00  1.456091e-01

detach(esoph)

Upvotes: 3

jay.sf
jay.sf

Reputation: 73252

cbinding beforehand works.

esoph$ncases.ncontrols <- with(esoph, cbind(ncases, ncontrols))
glm(ncases.ncontrols ~ agegp + tobgp * alcgp, data=esoph, family=binomial)

Comes a warning though in pcor().

Upvotes: 1

Related Questions