Zhenning
Zhenning

Reputation: 58

R6 seems not working properly with stats::lm()

I'm fairly new to both R6 and Object Oriented Programming, that I really couldn't figure out why I can't supply either weights or subset to stats::lm() called by a method. Codes below, where you can see that when calling stats::lm() with either of these two arguments, R complains that the provided variables can't be found. However, the provided variables can be printed by the same methods, proving that they should be accessible.

gen_reg_data <- R6::R6Class(
  classname = "regressionClass", 
  public = list(
    initialize = function(data){
      private$data <- data
    }, 
    lm = function(formula){
      stats::lm(formula = formula, data = private$data)
    }, 
    weighted_lm = function(formula, weight_col){
      weight_vec <- abs(private$data[, weight_col])
      print(head(weight_vec))
      tryCatch(
        stats::lm(formula = formula,
                  weights = weight_vec, 
                  data = private$data), 
        error = function(e) e
      )
    }, 
    subset_lm = function(formula){
      subset_vec <- private$data$X10 >= 0
      print(head(subset_vec))
      tryCatch(
        stats::lm(formula = formula,
                  subset = subset_vec,
                  data = private$data), 
        error = function(e) e
      )
    }
  ), 
  private = list(
    data = NULL
  )
)

set.seed(1L)
raw_dat <- data.frame(matrix(rnorm(2600), nrow = 100))
tst_dat <- gen_reg_data$new(raw_dat)

# The following line print the results of lm
tst_dat$lm(formula = "X1 ~ X4")

# Following line prints the head of `weight_vec`, showing it's accessible but 
# then an error message would pop up that weight_vec can't be found
tst_dat$weighted_lm(formula = "X1 ~ X4", weight_col = "X5")

# Following line does the same for `subset_vec`
tst_dat$subset_lm(formula = "X1 ~ X4")

I tried adding those tryCatch blocks and print line to check if the complained variables can be accessed by the methods or not. Looks like they are, but stats::lm() still cannot find them.

Below is a screenshot of my results: enter image description here

Upvotes: 2

Views: 71

Answers (1)

Zhenning
Zhenning

Reputation: 58

As commented by @njp, the issue is caused by how R handles formula objects. Simplest solution seems to be including the variables to be provided to subset or weight arguments as columns of the data argument. Example below: `

stats::lm(formula = formula,
          weights = weight_vec, 
          data = cbind(private$data, weight_vec))

`

Upvotes: 0

Related Questions