ProjectMOSAIC / mosaicModel

Utilities for creating, visualizing, and predicting with models
Other
2 stars 1 forks source link

Bug in mod_error: wrong data type #12

Closed rpruim closed 6 years ago

rpruim commented 6 years ago

Still need to investigate the cause.

library(mosaicModel)
mtcars <- 
  mtcars %>% 
  mutate(transmission = ifelse(am, "manual", "automatic"))
fuel_mod_2 <- lm(mpg ~ ns(hp, 2) * transmission, data = mtcars)
mod_error(fuel_mod_2)
#> Warning in mod_error(fuel_mod_2): Calculating error from training data.
#> Warning in mean.default((actual - model_output)^2, na.rm = TRUE): argument
#> is not numeric or logical: returning NA
#> mse 
#>  NA

Created on 2018-06-18 by the reprex package (v0.2.0).

rpruim commented 6 years ago

Looks like mod_eval() is being used to get the results of the model. This returns a data frame. What is needed is a vector of just the results.

I'm writing a wrapper called mod_output() to extract just this vector from the data frame.

rpruim commented 6 years ago

This seems to have fixed the immediate error

library(mosaicModel)
#> Loading required package: mosaicCore
#> Loading required package: splines
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:mosaicCore':
#> 
#>     count, tally
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
mtcars <-
  mtcars %>%
  mutate(transmission = ifelse(am, "manual", "automatic"))
fuel_mod_2 <- lm(mpg ~ ns(hp, 2) * transmission, data = mtcars)
mod_error(fuel_mod_2)
#> Warning in mod_error(fuel_mod_2): Calculating error from training data.
#>      mse 
#> 5.915142

Created on 2018-06-18 by the reprex package (v0.2.0).

rpruim commented 6 years ago

I've reverted a bit, renamed mod_eval_fun() to mod_output() as per #13. See #15 for a decision that still needs to be made. For now, a 1-column data frame is converted to a vector inside mod_error() so that the error functions can be correctly evaluated.