Aligning Data frame with missing values

后端 未结 4 1417
盖世英雄少女心
盖世英雄少女心 2020-12-04 00:31

I\'m using a data frame with many NA values. While I\'m able to create a linear model, I am subsequently unable to line the fitted values of the model up with

4条回答
  •  不知归路
    2020-12-04 00:54

    My answer is an extension to @ithomps solution:

    for(i in 1:nrow(data)){
      data$fitted.values.men[i]<- ifelse(data$sex == 1, 
        fit.males$fitted.values[paste(i)], "NA")
      data$fitted.values.women[i]<- ifelse(data$sex == 0, 
        fit.females$fitted.values[paste(i)], "NA")
      data$fitted.values.combined[i]<- fit.combo$fitted.values[paste(i)]
    }
    

    Because in my case I ran three models: 1 for males, 1 for females, and 1 for the combined. And to make things "more" convenient: males and females are randomly distributed in my data. Also, I'll have missing data as input for lm(), so I did fit <- lm(y~x, data = data, na.action = na.exclude) to get NAs in my model-object (fit).

    Hope this helps others.

    (I found it pretty hard to formulate my issue/question, glad I found this post!)

提交回复
热议问题