How to Save Predicted (or manually generated) Values into the Original Data?

Dear, I am a newbie here.

Would you kindly answer how to save predicted values (e.g., using lmer function) into the original data, which should also be saved as Stata format.

Your reply will be greatly appreciated.

Thanks,

BK28

Probably the easiest way to do this would be like this:

library(lme4)
library(broom) # for adding predictions to original data
library(haven) # for saving as Stata format

fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)

sleep_with_preds <- augment(fm1)

write_dta(sleep_with_preds, 'sleep.dta')

The function augment from the broom package will return the original data plus a whole bunch of other values including predictions (.fitted column), residuals (.resid), etc.

2 Likes

Hi, and welcome!

The haven package provides a facility to write Stata dta files from data frames and tibbles.

If you have data in the form of a data frame or tibble and model output with a vector of predictors for each observation (which means you must exclude all NA cases) you should be able to add the predictors as a column with cbind or, preferably, dplyr::mutate and then haven::write_dta(your_df, path, n) where n is Stata version 8-15.

If you get stuck along the way, please post a new topic with a reproducible example, called a reprex so peeps here can weigh in on specifics.

1 Like

Thank you so much! In fact, I created a weight based on fitted values from lmer.

num.mod <- lmer(CCW4 ~ (1|S4_ID), data =M)
den.mod <- lmer(CCW4 ~ (1|S4_ID) + STHLTHW2 + IRTRW2 + IRTMW2 + IRTSW2 + SINP_W2 + INC_W2 + SIB_W2 + BRTHWW12 + MALE + AGE_W2 + CNTCRW12 + PED1_W12 + HNOENGW2 + PEDEXPW1 + CityW2 + TownW2 + RuralW2 + EtcCCW2 , data =M)

num.p_op1 <- dnorm(M$CCW4, mean = fitted(num.mod), sd = attr(VarCorr(num.mod), "sc"))
num.p_op2 <- dnorm(M$CCW4, mean = mean(M$CCW4), sd = sd(M$CCW4))
den.p <- dnorm(M$CCW4, mean = fitted(den.mod), sd = attr(VarCorr(den.mod), "sc"))

Tps_MLM_op1 <- num.p_op1/den.p

"Tps_MLM_op1" is the weight variable that I want to merge with the original data. However, I received the following error message with "broom" Would you help me to solve this issue? Other approaches will also be appreciated. Thank you again,

library(broom) # for adding predictions to original data
M <- augment(Tps_MLM_op1)
Error: No augment method for objects of class numeric

The function augment will only work if you directly provide it the lmer model object as an argument. In your case, it probably makes most sense to just add the variable Tps_MLM_op1 to your original data set using more traditional methods like dplyr::mutate(). For example, replace the augment() code with:

M <- mutate(M, Tps_MLM_op1)
write_dta(M, 'M.dta')
3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.