MatthewR
MatthewR

Reputation: 2780

getting r2 out of a summ.svyglm object

I am trying to get R² for survey-adjusted logistic models. I am using jtools, which prints the Pseudo-R² and the observations to the screen, but I can't sort out how to extract those elements into a data.frame.

library(survey)
library(jtools)
data(api)
dstrat <- svydesign(id = ~1, strata =~ stype, weights =~ pw,
data = apistrat, fpc =~ fpc)
regmodel <- svyglm(as.numeric( both =="Yes") ~ ell * meals, design = dstrat, family="binomial" )
summ(regmodel)

Upvotes: 0

Views: 467

Answers (2)

Duck
Duck

Reputation: 39623

You can try one of these approaches:

library(survey)
library(jtools)
data(api)
dstrat <- svydesign(id = ~1, strata =~ stype, weights =~ pw,
                    data = apistrat, fpc =~ fpc)
regmodel <- svyglm(as.numeric( both =="Yes") ~ ell * meals, design = dstrat, family="binomial" )

Approach 1:

#Approach 1
attr(summ(regmodel), "rsq")

Output:

[1] 0.00107412

Approach 2:

#Approach 2
x <- capture.output(summ(regmodel))
x[9]
x[10]

Output:

[1] "Pseudo-R² (Cragg-Uhler) = 0.00"

[1] "Pseudo-R² (McFadden) = 0.01"

Upvotes: 1

user2554330
user2554330

Reputation: 45027

Those are stored as attributes of the summ(regmodel) object. The "Cragg-Uhler" value is in attribute "rsq", and the "McFadden" one is in "rsqmc". You can extract them as numbers using

rsq <- attr(summ(regmodel), "rsq")
rsqmc <- attr(summ(regmodel), "rsqmc")

To find things like this, you need to look at the code that prints the results. That's often print.xxxx, where xxxx is the class of the object. Here class(summ(regmodel)) is c("summ.svyglm" "summ"), so it will be print.summ.svyglm if there is such a thing, and print.summ if not.

Typically those functions aren't exported from a package, so if print.summ.svyglm doesn't find anything, you might still want to try getAnywhere("print.summ.svyglm"). In this case that's what works. You can read through that function and figure out where it got those values from.

Upvotes: 1

Related Questions