Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.
Sign upSupport for svyglm #47
Comments
This comment has been minimized.
This comment has been minimized.
I just noticed you actually added some support for |
This comment has been minimized.
This comment has been minimized.
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. |
leeper
added
the
enhancement
label
Nov 4, 2016
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
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 comment has been minimized.
This comment has been minimized.
This appears to be working with latest dev versions of prediction and margins. |
leeper
added this to the CRAN Release milestone
Nov 5, 2016
This comment has been minimized.
This comment has been minimized.
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 comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
tslumley
commented
Nov 7, 2016
Version 3.31-5 of survey, now uploaded to r-forge, uses the |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I can confirm everything works with the dev versions of |
leeper
closed this
in
f0a99f7
Nov 9, 2016
This comment has been minimized.
This comment has been minimized.
Excellent! Just checked it, as well, and it seems to be working. I've added versioned dependencies accordingly. Thank you for this! |
This comment has been minimized.
This comment has been minimized.
tzoltak
commented
Jun 17, 2019
I wonder, weather |
This comment has been minimized.
This comment has been minimized.
@tzoltak Can you open a new issue with an reproducible example (or just some more detail)? Thanks. |
carlganz commentedNov 3, 2016
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