5

In R, you might have estimated a model with a log-transformed dependent variable:

mfit <- lm(
    formula = log(salary) ~ yrs.service + yrs.since.phd, 
    data    = Salaries
  )

Then you might want to alter the model frame and call an update to refit the model:

n     <- nrow(Salaries)
mfr   <- model.frame(mfit)[sample(1:n, size=n, replace=TRUE),]
mfit2 <- update(mfit, data = mfr)

This will cause an error:

Error in eval(expr, envir, enclos) : object 'salary' not found

The reason is that the formula still has dependent variable log(salary) and the variable in the model frame is called log(salary). R thinks that it can find salary and then call log on it. The same error would occur without the resampling, the example just shows why one might want to do it.

The procedure above is from a bootstrap package where resampling rows is performed. Is this behavior to be expected, or is it a bug? I know that one can get around it by transforming the variables in the data argument, but this seems annoying and overlooked...

1
  • it's not quite the same statistical model, but you could also fit glm(salary~yrs.service + yrs.since.phd,family=gaussian(link="log"))
    – Ben Bolker
    Apr 24, 2012 at 16:05

2 Answers 2

0

Instead of sampling from model.frame(mfit), you can sample from na.omit(get_all_vars(myformula, Salaries)) instead. So, your example would become the following:

myformula <- log(salary) ~ yrs.service + yrs.since.phd

mfit <- lm(formula = myformula, data = Salaries)

n       <- nrow(Salaries)
newdata <- na.omit(get_all_vars(myformula, Salaries))[sample(1:n, size=n, replace=TRUE),]
mfit2   <- update(mfit, data = newdata)

We can use the following simple example to confirm that model.frame(myformula, df) and na.omit(get_all_vars(myformula, df)) select the same raw (untransformed) data from a data frame:

df <- data.frame(w = rnorm(10), x = rnorm(10), y = rnorm(10), z = rnorm(10))
df[1, 1] <- NA
df[2, 2] <- NA
df[3, 3] <- NA
df[4, 4] <- NA

identical(data.frame(na.omit(get_all_vars(z ~ w + x, df))), data.frame(model.frame(z ~ w + x, df)))
# [1] TRUE

Note that I wrapped the results of na.omit(get_all_vars(...)) and model.frame(...) in data.frame just to drop the extraneous attributes for comparison purposes. Of course, model.frame does additional work, like log transforming the salary in your example. But if all you need to do is sample the original data, then na.omit(get_all_vars(...)) works fine and then you can pass your new data frame to lm or update.

-1

I don't think it's a bug. Since the formula can receive functions and operators, i.e.,

log(foo)*3 ~ abs(fooller) + fooz

It can't separate what's an object called abs(fooller) from the result of function abs() with the argument fooller.

In my point of view it's a problem of naming conventions. You shouln't name a variable, or column, as a name that can be misunderstood as a function. Instead you could use salary.log.

1
  • this is not actually a very useful answer. abs(fooller) is not a legal symbol/variable name in R (if you make a variable called <code>abs(fooler)</code> [i.e. backtick-protected], then you deserve what you get), and all.vars() can extract fooller
    – Ben Bolker
    Feb 5, 2018 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.