Advertisement
whiterarebit

Own impute

Jun 23rd, 2020
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.57 KB | None | 0 0
  1. own_imp <- function(dt){
  2.   for (column in 1:ncol(dt)){
  3.     corr <- sapply(c(names(dt[,-column])),
  4.                    function(x) cor(dt[,column], dt[,x],  use = "p"))
  5.    
  6.     corr_dec <- sort(corr, decreasing = TRUE)
  7.     imp.use <- names(corr_dec[1])
  8.    
  9.     lm.cf <- lm(reformulate(imp.use, colnames(dt[column])), dt)$coef
  10.    
  11.     dt[, column] <- ifelse(is.na(dt[, column]), lm.cf[1] + dt[[imp.use]] *lm.cf[2], dt[,column])
  12.   }
  13.   return(dt)
  14. }
  15.  
  16. file = "https://www.dropbox.com/s/jw8oqc9816ul8jq/GRP_outside_d20.csv?dl=1"
  17. GRP_data = read.csv(file = file)
  18.  
  19. new_data <- own_imp(GRP_data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement