# NOT RUN {
# DATA
data(healthsurvey)
# first determine the order of the levels of the dependent variable
levels(healthsurvey$health)
# the order of response levels decreases from the best health to
# the worst health; hence the hopit() parameter decreasing.levels
# is set to TRUE
# Example 1 ---------------------
# fitting the model:
model1 <- hopit(latent.formula = health ~ hypertension + high_cholesterol +
heart_attack_or_stroke + poor_mobility + very_poor_grip +
depression + respiratory_problems +
IADL_problems + obese + diabetes + other_diseases,
thresh.formula = ~ sex + ageclass + country,
decreasing.levels = TRUE,
control = list(trace = FALSE),
data = healthsurvey)
# summarize the fit:
summary(model1)
# extract parameters in the form of a list
cm1 <- coef(model1, aslist = TRUE)
# names of the returned coefficients
names(cm1)
# extract the latent health coefficients
cm1$latent.params
# check the fit
# }
# NOT RUN {
profile(model1)
# }
# NOT RUN {
# Example 2 ---------------------
# }
# NOT RUN {
# incorporate the survey design
design <- svydesign(ids = ~ country + psu, weights = healthsurvey$csw,
data = healthsurvey)
model2 <- hopit(latent.formula = health ~ hypertension + high_cholesterol +
heart_attack_or_stroke + poor_mobility +
very_poor_grip + depression + respiratory_problems +
IADL_problems + obese + diabetes + other_diseases,
thresh.formula = ~ sex + ageclass + country,
decreasing.levels = TRUE,
design = design,
control = list(trace = FALSE),
data = healthsurvey)
# compare the latent variables
cbind('No survey design' = coef(model1, aslist = TRUE)$latent.par,
'Has survey design' = coef(model2, aslist = TRUE)$latent.par)
# }
# NOT RUN {
# Example 3 ---------------------
# defining the interactions between the threshold and the latent variables
# correctly defined interactions:
model3 <- hopit(latent.formula = health ~ hypertension + high_cholesterol +
heart_attack_or_stroke + poor_mobility * very_poor_grip +
depression + respiratory_problems +
IADL_problems + obese + diabetes + other_diseases +
sex : depression + sex : diabetes + ageclass:obese,
thresh.formula = ~ sex * ageclass + country + sex : obese,
decreasing.levels = TRUE,
control = list(trace = FALSE),
data = healthsurvey)
# }
# NOT RUN {
# badly defined interactions:
# 1) lack of a main effect of "other_diseases" in any formula
# it can be solved by adding " + other_diseases" to the latent formula
model3a <- hopit(latent.formula = health ~ hypertension + high_cholesterol +
heart_attack_or_stroke + poor_mobility + very_poor_grip +
depression + respiratory_problems +
IADL_problems + obese + diabetes + other_diseases : sex,
thresh.formula = ~ sex + ageclass + country,
decreasing.levels = TRUE,
control = list(trace = FALSE),
data = healthsurvey)
# 2) the main effect of sex is present in both formulas.
# it can be solved by replacing "*" with ":" in "other_diseases * sex"
model3b <- hopit(latent.formula = health ~ hypertension + high_cholesterol +
heart_attack_or_stroke + poor_mobility + very_poor_grip +
depression + respiratory_problems +
IADL_problems + obese + diabetes + other_diseases * sex,
thresh.formula = ~ sex + ageclass + country,
decreasing.levels = TRUE,
control = list(trace = FALSE),
data = healthsurvey)
# }
# NOT RUN {
# Example 4 ---------------------
# }
# NOT RUN {
# construct a naive continuous variable:
hs <- healthsurvey
hs$cont_var <- sample(5000:5020,nrow(hs),replace=TRUE)
latent.formula = health ~ hypertension + high_cholesterol +
heart_attack_or_stroke + poor_mobility + very_poor_grip +
depression + respiratory_problems +
IADL_problems + obese + diabetes + other_diseases
# in some cases, when continuous variables are used, the hopit:::get.hopit.start() function
# do not find starting parameters (R version 3.4.4 (2018-03-15)):
model4 <- hopit(latent.formula = latent.formula,
thresh.formula = ~ sex + cont_var,
decreasing.levels = TRUE,
data = hs)
# one of the solutions is to transform one or more continuous variables:
hs$cont_var_t <- hs$cont_var-min(hs$cont_var)
model4b <- hopit(latent.formula = latent.formula,
thresh.formula = ~ sex + cont_var_t,
decreasing.levels = TRUE,
data = hs)
# this can also be done automatically using the the control parameter
model4c <- hopit(latent.formula = latent.formula,
thresh.formula = ~ sex + cont_var,
decreasing.levels = TRUE,
control = list(transform.thresh = 'min',
transform.latent = 'none'),
data = hs)
model4d <- hopit(latent.formula = latent.formula,
thresh.formula = ~ sex + cont_var,
decreasing.levels = TRUE,
control = list(transform.thresh = 'scale_01',
transform.latent = 'none'),
data = hs)
model4e <- hopit(latent.formula = latent.formula,
thresh.formula = ~ sex + cont_var,
decreasing.levels = TRUE,
control = list(transform.thresh = 'standardize',
transform.latent = 'none'),
data = hs)
model4f <- hopit(latent.formula = latent.formula,
thresh.formula = ~ sex + cont_var,
decreasing.levels = TRUE,
control = list(transform.thresh = 'standardize_trunc',
transform.latent = 'none'),
data = hs)
round(t(rbind(coef(model4b),
coef(model4c),
coef(model4d),
coef(model4e),
coef(model4f))),4)
# }
Run the code above in your browser using DataLab