# Example 1: monotonic in 1 predictor
data(nhdat2, package = 'csurvey')
#specify the design:
dstrat <- svydesign(ids = ~id, strata = ~str, data = nhdat2, weight = ~wt)
#uncomment to use parallel computing:
#options(csurvey.multicore=TRUE)
#mixture-variance-covariance matrix is simulated
set.seed(1)
ans <- csvy(chol ~ incr(age), design = dstrat, n.mix=5)
#check the constrained fit vs the unconstrained fit
summary(ans)
plot(ans, type = 'both')
if (FALSE) {
# Example 2: monotonic in 2 predictors and unconstrained in a 3rd predictor
data(nhdat2, package = 'csurvey')
#specify the design:
dstrat <- svydesign(ids = ~id, strata = ~str, data = nhdat2, weight = ~wt)
#use parallel computing:
#options(csurvey.multicore=TRUE)
#mixture-variance-covariance matrix is simulated
#Average cholestorel level increases in age, waist, and unconstrained in income
set.seed(1)
ans <- csvy(chol ~ incr(age)*incr(wcat)*icat, design = dstrat, test=FALSE, n.mix=5)
#visualize the constrained estimation with confidence bands
plot(ans, x1='icat', x2='wcat')
#create control object
ctl <- plot_csvy_control(ribbon_fill = "pink",x1lab = 'income',x2lab = 'waist')
plot(ans, x1='icat', x2='wcat', control=ctl)
}
# Example 3: example with a binomial response
library(NHANES)
library(survey)
data(NHANES)
nh <- subset(NHANES, !is.na(Education) & !is.na(BMI) & !is.na(Weight))
nh$DiabetesBin <- as.integer(nh$Diabetes == "Yes")
nh$BMIgroup <- cut(nh$BMI, breaks = c(0, 18.5, 25, 30, 35, 40, Inf), labels = FALSE)
# specify the design
dsgn <- svydesign(ids = ~1, strata = ~BMIgroup, weights = ~Weight, data = nh)
ans <- csvy(DiabetesBin ~ decr(Education) * incr(BMIgroup), design = dsgn,
family = quasibinomial(link='logit'), n.mix=5)
summary(ans)
plot(ans, x1 = 'BMIgroup', x2 = 'Education')
ctl <- plot_csvy_control(
x1size = 1.5, x2size = 2,
angle = 45, hjust = .3)
plot(ans, x1 = 'Education', x2 = 'BMIgroup', control = ctl)
Run the code above in your browser using DataLab