Predictive margins using R's "survey" package when I don't want to adjust for anything (simple GLM)

73 views Asked by At
library(survey) # Analysis of Complex Survey Samples
library(srvyr) # 'dplyr' inspired syntactic style

# complex survey design
design <- data %>% as_survey_design(ids = HOSP_NRD, weights = DISCWT, strata = NRD_STRATUM)

# create my survey-weighted GLM
fit_cost <- svyglm(Costs1 ~ 1, 
                   design = design,
                   family = Gamma(link = "log")) 

# predictive marginal means works
svypredmeans(fit_cost, ~group)

I can't figure out how to get the marginpred command to work without adjusting for anything. I just want a simple GLM.

It gives the following error: "Error in eval(e[[2L]], where) : argument "adjustfor" is missing, with no default".

# Standardised predictions (predictive margins) for regression models
marginpred(fit_cost,
           predictat = data.frame(group = c("a", "b")))
1

There are 1 answers

0
IRTFM On

I first read the help page for ?marginpred and saw that marginpred first does some adjustments and then hands off to the predict function. In the absence of a proper example I continued down to the examples on that page and then executed the `svyglm example and attempted my comment suggestion with apparently satisfactory results:

> set.seed(42)
> df<-data.frame(x=rnorm(100))
> df$time<-rexp(100)*exp(df$x-1)
> df$status<-1
> df$group<-(df$x+rnorm(100))>0
> des<-svydesign(id=~1,data=df)
Warning message:
In svydesign.default(id = ~1, data = df) :
  No weights or probabilities supplied, assuming equal probability
> newdf<-data.frame(group=c(FALSE,TRUE), x=c(0,0))
> 
> logisticm <- svyglm(group~time, family=quasibinomial, design=des)
> newdf$time<-c(0.1,0.8)
> logisticpred <- marginpred(logisticm, adjustfor=~x, predictat=newdf)
> predict(logisticm)
         link     SE
1   -0.279206 0.2156
2   -0.719218 0.2707
3   -0.813465 0.2988
4   -0.856459 0.3127
5   -0.722535 0.2716
# elided next 95 results