1

I would like my lm function to be able to use whatever column contains an NA dynamically, instead of writing it out. When I use paste(dependent), I get the following error:

Error in eval(parse(text = x, keep.source = FALSE)[[1L]]) : object 'V4' not found

Here is my code so far:

library(imputeTS)
library(dplyr)

rawVector <- c(1,3,4,6,8,11,13,NA,18,19,21,29,30,34,36,38)
testMatrix <- matrix(rawVector, nrow = 4, ncol = 4, byrow = T)

imputeMissingValuesHARD <- function(inputMatrix) {
  inputMatrix <- as.data.frame(inputMatrix)
  dimnames(inputMatrix) <- list(rownames(inputMatrix, do.NULL = FALSE, prefix = "row"), colnames(inputMatrix, do.NULL = FALSE, prefix = "col"))
  dependent <- ""
  colNumber <- -1

  for(row in 1:nrow(inputMatrix)) {
    for(col in 1:ncol(inputMatrix)) {
        if(is.na(inputMatrix[row, col]))
      {
        dependent <- colnames(inputMatrix)[col]
        colNumber <- col
      }
    }
  }

  print(dependent)
  inputMatrixCleaned <- inputMatrix %>% filter(!is.na(paste0(dependent)))
  print(inputMatrixCleaned)
  fit <- lm(V4 ~ V1, inputMatrixCleaned)
  summary(fit)
  imputedMatrix <- inputMatrix %>% 
    mutate(pred = predict(fit, .)) %>%
    mutate(V4 = ifelse(is.na(paste0(dependent)), pred, paste0(dependent)))

  return(as.data.frame(imputedMatrix))
}

imputeMissingValuesHARD(testMatrix)
camille
  • 13,812
  • 10
  • 29
  • 45
  • The answers here should help https://stackoverflow.com/questions/4951442/formula-with-dynamic-number-of-variables You can also just use `get(dependent)` instead of `paste()` – Kreuni Dec 29 '19 at 02:51
  • I'm unable to tell where your errors are being thrown because you included no code that throws an error. I'm guessing that you need to learn using `as.formula`. – IRTFM Dec 29 '19 at 05:30

0 Answers0