8000 Can't call forecast::forecast.mlm() within another function · Issue #880 · robjhyndman/forecast · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Can't call forecast::forecast.mlm() within another function #880
Open
@nclJoshCowley

Description

@nclJoshCowley

I'm trying to use this package to make prediction intervals for an "mlm" object.

I think, because of the NSE used in the source code, newdata is not found when the function is called within another function.

Toy example (Reprex) added below to better explain what I mean.

library(forecast)
#> Warning: package 'forecast' was built under R version 4.0.4
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo

resp <- matrix(rnorm(200), 100, 2)
colnames(resp) <- paste0("R", 1:2)

expl <- matrix(rnorm(400), 100, 4)
colnames(expl) <- paste0("E", 1:4)

fit <- stats::lm(resp ~ ., data = as.data.frame(expl))
print(fit)
#> 
#> Call:
#> stats::lm(formula = resp ~ ., data = as.data.frame(expl))
#> 
#> Coefficients:
#>              R1        R2      
#> (Intercept)   0.11233  -0.01330
#> E1            0.09153   0.10792
#> E2            0.01813   0.18159
#> E3           -0.04822   0.10588
#> E4           -0.13863  -0.09265
print(class(fit))
#> [1] "mlm" "lm"

newdat <- matrix(seq(8), 2, 4)
colnames(newdat) <- paste0("E", 1:4)
print(newdat)
#>      E1 E2 E3 E4
#> [1,]  1  3  5  7
#> [2,]  2  4  6  8

# forecast::forecast.mlm()
forecast::forecast(
  object = fit,
  newdata = as.data.frame(newdat),
  level = 0.95
)
#> R1
#>   Point Forecast     Lo 95    Hi 95
#> 1     -0.9532618 -3.875441 1.968917
#> 2     -1.0304579 -4.239408 2.178492
#> 
#> R2
#>   Point Forecast     Lo 95    Hi 95
#> 1      0.5202065 -2.198623 3.239036
#> 2      0.8229431 -2.162701 3.808588


my_function <- function(my_fit, my_newdat) {
  results <- forecast::forecast(
    object = my_fit,
    newdata = as.data.frame(my_newdat),
    level = 0.95
  )
  
  # Do something with results, like plot ?
  # ...
}

my_function(fit, newdat)
#> Error in as.data.frame(my_newdat): object 'my_newdat' not found

Created on 2021-03-09 by the reprex package (v1.0.0)

Session info
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 4.0.3 (2020-10-10)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United Kingdom.1252 
#>  ctype    English_United Kingdom.1252 
#>  tz       Europe/London               
#>  date     2021-03-09                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version  date       lib source        
#>  assertthat    0.2.1    2019-03-21 [1] CRAN (R 4.0.3)
#>  backports     1.2.0    2020-11-02 [1] CRAN (R 4.0.3)
#>  cli           2.1.0    2020-10-12 [1] CRAN (R 4.0.3)
#>  colorspace    1.4-1    2019-03-18 [1] CRAN (R 4.0.3)
#>  crayon        1.3.4    2017-09-16 [1] CRAN (R 4.0.3)
#>  curl          4.3      2019-12-02 [1] CRAN (R 4.0.3)
#>  digest        0.6.27   2020-10-24 [1] CRAN (R 4.0.3)
#>  dplyr         1.0.2    2020-08-18 [1] CRAN (R 4.0.3)
#>  ellipsis      0.3.1    2020-05-15 [1] CRAN (R 4.0.3)
#>  evaluate      0.14     2019-05-28 [1] CRAN (R 4.0.3)
#>  fansi         0.4.1    2020-01-08 [1] CRAN (R 4.0.3)
#>  forecast    * 8.13     2020-09-12 [1] CRAN (R 4.0.4)
#>  fracdiff      1.5-1    2020-01-24 [1] CRAN (R 4.0.4)
#>  fs            1.5.0    2020-07-31 [1] CRAN (R 4.0.3)
#>  generics      0.1.0    2020-10-31 [1] CRAN (R 4.0.3)
#>  ggplot2       3.3.3    2020-12-30 [1] CRAN (R 4.0.3)
#>  glue          1.4.2    2020-08-27 [1] CRAN (R 4.0.3)
#>  gtable        0.3.0    2019-03-25 [1] CRAN (R 4.0.3)
#>  highr         0.8      2019-03-20 [1] CRAN (R 4.0.3)
#>  htmltools     0.5.0    2020-06-16 [1] CRAN (R 4.0.3)
#>  knitr         1.30     2020-09-22 [1] CRAN (R 4.0.3)
#>  lattice       0.20-41  2020-04-02 [1] CRAN (R 4.0.3)
#>  lifecycle     0.2.0    2020-03-06 [1] CRAN (R 4.0.3)
#>  lmtest        0.9-38   2020-09-09 [1] CRAN (R 4.0.4)
#>  magrittr      2.0.1    2020-11-17 [1] CRAN (R 4.0.3)
#>  munsell       0.5.0    2018-06-12 [1] CRAN (R 4.0.3)
#>  nlme          3.1-149  2020-08-23 [1] CRAN (R 4.0.3)
#>  nnet          7.3-14   2020-04-26 [1] CRAN (R 4.0.3)
#>  pillar        1.4.6    2020-07-10 [1] CRAN (R 4.0.3)
#>  pkgconfig     2.0.3    2019-09-22 [1] CRAN (R 4.0.3)
#>  purrr         0.3.4    2020-04-17 [1] CRAN (R 4.0.3)
#>  quadprog      1.5-8    2019-11-20 [1] CRAN (R 4.0.3)
#>  quantmod      0.4.18   2020-12-09 [1] CRAN (R 4.0.4)
#>  R6            2.5.0    2020-10-28 [1] CRAN (R 4.0.3)
#>  Rcpp          1.0.5    2020-07-06 [1] CRAN (R 4.0.3)
#>  reprex        1.0.0    2021-01-27 [1] CRAN (R 4.0.4)
#>  rlang         0.4.8    2020-10-08 [1] CRAN (R 4.0.3)
#>  rmarkdown     2.5      2020-10-21 [1] CRAN (R 4.0.3)
#>  rstudioapi    0.12     2020-11-10 [1] CRAN (R 4.0.3)
#>  scales        1.1.1    2020-05-11 [1] CRAN (R 4.0.3)
#>  sessioninfo   1.1.1    2018-11-05 [1] CRAN (R 4.0.3)
#>  stringi       1.5.3    2020-09-09 [1] CRAN (R 4.0.3)
#>  stringr       1.4.0    2019-02-10 [1] CRAN (R 4.0.3)
#>  styler        1.3.2    2020-02-23 [1] CRAN (R 4.0.3)
#>  tibble        3.0.4    2020-10-12 [1] CRAN (R 4.0.3)
#>  tidyselect    1.1.0    2020-05-11 [1] CRAN (R 4.0.3)
#>  timeDate      3043.102 2018-02-21 [1] CRAN (R 4.0.3)
#>  tseries       0.10-48  2020-12-04 [1] CRAN (R 4.0.4)
#>  TTR           0.24.2   2020-09-01 [1] CRAN (R 4.0.4)
#>  urca          1.3-0    2016-09-06 [1] CRAN (R 4.0.4)
#>  vctrs         0.3.4    2020-08-29 [1] CRAN (R 4.0.3)
#>  withr         2.3.0    2020-09-22 [1] CRAN (R 4.0.3)
#>  xfun          0.19     2020-10-30 [1] CRAN (R 4.0.3)
#>  xts           0.12.1   2020-09-09 [1] CRAN (R 4.0.4)
#>  yaml          2.2.1    2020-02-01 [1] CRAN (R 4.0.3)
#>  zoo           1.8-8    2020-05-02 [1] CRAN (R 4.0.3)
#> 
#> [1] C:/R/R-4.0.3/library

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0