-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for svyglm #47
Comments
I just noticed you actually added some support for |
Great minds think alike! Can you let me know if this is now working with the dev version of prediction? It probably won't (I fear), so I'll try to work on it soon. This is definitely a top priority for me. |
Unfortunately it did not work with the dev version of data <- eval(model[["call"]][["data"]], env) With # from examples
library(survey)
data(scd)
repweights<-2*cbind(c(1,0,1,0,1,0), c(1,0,0,1,0,1), c(0,1,1,0,0,1),
c(0,1,0,1,1,0))
scdrep<-svrepdesign(data=scd, type="BRR", repweights=repweights, combined.weights=FALSE)
x <- svyglm(arrests~alive,scdrep)
x$data # gives full scd data.frame
# also works for database backed survey objects
library(RSQLite)
dbclus1<-svydesign(id=~dnum, weights=~pw, fpc=~fpc,
data="apiclus1",dbtype="SQLite",
dbname=system.file("api.db",package="survey"))
y <-svyglm(meals~api00,dbclus1)
y$data # gives data.frame with columns used including weights I was able to get things to work by making Regards |
Ooh, I like the idea of the making it generic, actually. That would likely simplify it moving forward as new model classes are supported. Send a PR? |
This appears to be working with latest dev versions of prediction and margins. |
I find that factor/character variables cause errors. MRE: library(margins)
library(survey)
data(api)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
# works without svy
margins(glm(api00~api99*sch.wide,data=apistrat))
# doesn't work with svy
margins(svyglm(api00~api99*sch.wide,design=dstrat))
# works with svy
margins(svyglm(api00~api99,design=dstrat))
# doesn't work with svy
margins(svyglm(api00~sch.wide,design=dstrat)) |
This appears to be a survey package issue: library(survey)
data(api)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
svymodel <- svyglm(api00~sch.wide,design=dstrat)
# errors
predict(svymodel, data.frame(sch.wide=rep("No",10)))
regmodel <- glm(api00~sch.wide,data=apistrat)
# works
predict(regmodel,data.frame(sch.wide=rep("No",10))) Issue is that the factor in the newdata needs to have more than one level to work (the levels don't even have to correspond to the levels used in the original data, it just needs more than one level), so this works: library(survey)
data(api)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
svymodel <- svyglm(api00~sch.wide,design=dstrat)
predict(svymodel, data.frame(sch.wide=factor(rep("No",10),
levels = c("No","random phrase")))) We could add a line here: https://github.com/leeper/prediction/blob/master/R/prediction_methods.R#L67 to check that all factors have levels with length at least 1, and if they don't just add a level, but that feels like a hack. I am going to think about this some more. |
Version 3.31-5 of survey, now uploaded to r-forge, uses the |
I can confirm everything works with the dev versions of |
Excellent! Just checked it, as well, and it seems to be working. I've added versioned dependencies accordingly. Thank you for this! |
I wonder, weather |
@tzoltak Can you open a new issue with an reproducible example (or just some more detail)? Thanks. |
Hello,
This is a wonderful package. In Stata, you can run a regression with survey data, and then run margins, but with this package it appears that you cannot currently run margins for survey data.
So in Stata you can do something like this:
Perhaps I am doing something wrong, but this won't work in R:
Issue is that
prediction::find_data
looks for data in the wrong place forsvyglm
objects.I believe it shouldn't be too difficult to get
margins
working with thesurvey
package. Would you consider a pull request?Thanks
The text was updated successfully, but these errors were encountered: