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)