I have a question regarding the function rlm of the MASS package.
The function fits a linear model by robust regression and has the argument na.action. The user should have the option to specify the action to be taken if NAs are found. I explicitly run the function with na.exclude to return the fitted values with the same length as the input, but actually the NA values are just lost. Is na.action implemented in the rlm function because actually changing the parameter for na.action does not do anything?
I tried to set the na.action via options to na.exclude but it did not work. Actually, all I want is that the function returns a fit model with fitted values of the same length as the input. Since this na.action parameter is offered, I thought it would also work.
Here a simple example:
ref_a <- c(13.70284,11.61466,16.03751,20.70219,13.85347,22.22926,18.16462,16.39861,14.62653,13.66633)
m <- c(NA,NA,0.05286859,0.23125664,-0.55154806, 0.16420488,0.29565772 ,0.49802977, NA,NA)
fit <- MASS::rlm(m ~ ref_a, na.action = stats::na.exclude)
fit_values <- fit$fitted.values
length(fit_values) # 6
length(m) #10
length(ref_a) #10
na.action
stores the information in the model fit, and this information is used by other functions, especially predict
. The object fitted.values
is not affected. So just use fitted values calculated by predict
instead of $fitted.values
.
library(MASS)
ref_a <- c(13.70284,11.61466,16.03751,20.70219,13.85347,22.22926,18.16462,16.39861,14.62653,13.66633)
m <- c(NA,NA,0.05286859,0.23125664,-0.55154806, 0.16420488,0.29565772 ,0.49802977, NA,NA)
fit <- rlm(m ~ ref_a, na.action=stats::na.exclude)
length(fit$fitted.values)
#> [1] 6
length(predict(fit))
#> [1] 10
Created on 2023-01-13 with reprex v2.0.2