Predicting NA values with a perfect line in R

Viewed 63

In my dataset, column A and B are strongly correlated and the scatter plot is as follows:

ggplot(df, aes(x = B, y =A)) + geom_point() + geom_smooth()

enter image description here

A is inversely proportional to B and forms a perfect nonlinear line.

Yet in column B, there are some missing values NA and I would like to replace them while keeping the perfect line instead of dropping them or replacing them by mean.

Here is my attempt to calculate the value directly but this approach doesn't work very well.

const = mean(transform(df, new = A* B)$new)
df$B[is.na(df$B)] <- const / df$A

It looks like this. enter image description here

Instead of calculating it, is there a way to do so? For example, how to replace the missing values with a predict() function ?

Thank you.

1 Answers

In the question, the last assignment is forgetting

  1. To set mean argument na.rm = TRUE;
  2. to subset column A, only the values for which B is missing should be taken into account.

In the example below I have created the data with const = pi.

f <- function(x, const = 1) const/x

const <- with(df1, mean(A*B, na.rm = TRUE))
const
#[1] 3.141593

na <- is.na(df1$B)
df1$B[na] <- f(df1$A[na], const = const)
plot(B ~ A, df1)

Test data

x <- seq(0.1, 2, by = 0.01)
y <- f(x, pi)
set.seed(2021)
is.na(y) <- sample(length(y), 0.1*length(y))
df1 <- data.frame(A = x, B = y)
Related