I have run this code
# Load the rms package
library(rms)
# Create a small example data frame
example_data <- data.frame(
problems = factor(c(1, 0, 1, 0, 1)),
var = c(1, 2, 3, 4, 5)
)
# Fit a model with only an intercept
fit_example <- lrm(problems ~ 1, data = example_data)
and I am encountering this error message --> Error in lrm(problems~ 1, data = example_data) : object 'nact' not found.
What does the error message mean and how do I fix it? I am using R version 4.4.1
I am an R novice, have searched for this issue online and cannot find anything.
This looks like it might be a bug in the lrm()
function itself.
If you just type the name of the function without parenthesis on the end (eg. type "lrm", without quotes), you can see the function source code.
You will find a line near the end that says:
f <- c(f, list(call = call, Design = if (xpres) atr, scale.pred = c("log odds","Odds Ratio"), terms = Terms, assign = ass, na.action = nact,
fail = FALSE, interceptRef = 1, nstrata = nstrata, sformula = sformula))
The part of the line that says na.action = nact
triggers an error if the function has not created a variable called nact by the time it reaches that line.
If you look further up in the code, you'll see there's an if statement, and a value for nact is set inside of that if statement:
if (length(atl <- attr(tform, "term.labels")) && any(atl !=".")) {
X <- Design(data, formula = formula, specials = "strat")
atrx <- attributes(X)
sformula <- atrx$sformula
nact <- atrx$na.action
The corresponding "else" for that if statement does not include a line that sets nact, which is how you sometimes end up with "nact" not existing by the time you get to the line that uses it.
If you have a github account you can report the bug to the author here.
As for how to fix it -- for now, to work around the bug, you need to fulfill the conditions length(atl <- attr(tform, "term.labels")) && any(atl !=".")
to end up in the first half of that if statement. After some more digging through the function code to find out what "tform" was, it looks like in order to meet that condition your formula needs to have at least one named predictor variable, not just problems ~ 1
.
In other words, right now you cannot use this function to fit an intercept-only model.
I don't know enough about what this function is actually doing to know if it should be capable of fitting an intercept-only model, or if maybe you're trying to do something it's not meant to do and it's just not doing a good job of telling you that. But either way, the underlying problem here is "this function can't work with the formula you gave it".
Edit: Based on John Polo's comment on your post, there has already been a bug report for this, and the author's reply "rms does not allow intercept-only models. You can get the -2 log likelihood for such models from the fit object for the full model."
I'm not familiar with the library, but if I take my best guess I think the author means you should do something like this:
fit_example <- lrm(problems ~ var, data = example_data)
univarLR(fit_example)
logLik(fit_example)