I am examining the relationship between diet quality (variable called “diet” in the R codes) and mortality using NHANES survey data. I aim to estimate Hazard Ratios (HR). I developed my codes. However, as you can see below, I am unable to complete the Predict() step, receiving the message “#Error in Predict()” and “predictor(s) not in model.” I have inserted my codes here, showing the error in the Predict() step.
Any assistance in resolving this problem would be greatly appreciated.
————————- R code using rms package ——————————————————
1 # dt' is data frame and includes all necessary variables
dt
2 # Create datadist object using the same data
ddist <- datadist(dt)
options(datadist = 'ddist')
3 # Define survey design
des <- svydesign(ids = ~SDMVPSU, strata = ~SDMVSTRA, weights = ~new_weight, nest=TRUE, data = dt)
4 # Fit the Cox proportional hazards model
fit1 <- svycoxph(Surv(PERMTH_EXM, MORTSTAT == 1) ~ rcs(diet, 3) +
RIDAGEYR + RIDAGEYR * RIDAGEYR + RIAGENDR + race + bmi_cat,
design = des)
5 # Generate a sequence over the range of diet
diet_seq <- seq(from = min(dt$diet, na.rm = TRUE), to = max(dt$diet, na.rm = TRUE), length.out = 100)
6 # Use Predict function with the specified predictor
pred_results <- Predict(fit1, diet = diet_seq, fun = exp)
#Error in Predict(fit1, diet = diet_seq, fun = exp) :
# predictors(s) not in model: diet
# I am not able to run step #6. As you see I get the error message that my variable "diet" is not in the model
Sam Farsi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.