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 lmtst_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 foundtst_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.