tidymodels / broom

Convert statistical analysis objects from R into tidy format
https://broom.tidymodels.org
Other
1.45k stars 302 forks source link

Create tidy method #1196

Closed guivivi closed 5 months ago

guivivi commented 5 months ago

Hello,

I am trying to follow the instructions from https://www.tidymodels.org/learn/develop/broom/ to add tidy() in my personal package to use tidy() with the model results from the midasr package https://www.jstatsoft.org/article/view/v072i04

I have followed these steps:

  1. Add manually the generics package to the Imports section of the DESCRIPTION file.

  2. Save in the R folder of my package a file called do_import_generics.R with this content:

    ' @importFrom generics tidy

    ' @export

    generics::tidy

  3. Save in the R folder of my package a file called .R with this content:

    ' Tidy a midas_r object

    '

    ' @aliases tidy.midas_r

    '

    ' @param x A midas_r object.

    ' @param ... Unused, included for generic consistency only.

    '

    ' @return A tidy [tibble::tibble()] summarizing component-level

    ' information about the model

    '

    ' @examples

    '

    ' @export

    tidy.midas_r <- function(x, ...) { result <- summary(x)$coefficients %>% tibble::as_tibble(rownames = "term") %>% dplyr::rename(estimate = Estimate, std.error = Std. Error, statistic = t value, p.value = Pr(>|t|))

    result }

  4. Run roxygen2::roxygenise() in RStudio and R CMD check, build and INSTALL consecutively in the terminal.

However, when I load my package and try tidy(), I get this Error: No tidy method for objects of class midas_r and I really do not know why. I do see in my NAMESPACE the following: export(tidy) importFrom(generics,tidy)

but it seems that the tidy function is not recognizing tidy.midas_r.

In case it might good to know, when I run R CMD check I get this NOTE: Found the following hidden files and directories: R/.R These were most likely included in error. See section 'Package structure' in the 'Writing R Extensions' manual.

Could you please provide me with any hint?

Many thanks in advance.

simonpcouch commented 5 months ago

Thank you for the issue! In your NAMESPACE, I would expect to see an entry like S3method(tidy,midas_r). Do you see this? Does running devtools::document() introduce that entry?

guivivi commented 5 months ago

No, I do not see it, even when I add to the .R file:

#' @export
tidy <- function(x, ...) {
  UseMethod("tidy")
}
and then I keep:
#' @export
tidy.midas_r <- function(x, ...) {.......}

When I run devtools::document() it does not appear S3method(tidy,midas_r) either. The same happened with roxygen2::roxygenise()

I have noticed that when I run R CMD build mypackage I get this message: * excluding invalid files Subdirectory 'R' contains invalid file names: '.R'

Maybe has it something to do with it?

Many thanks for the help.

simonpcouch commented 5 months ago

I'm not sure what the impact of the R CMD BUILD error might be without access to your repository, but sounds worth addressing while you troubleshoot.

You can try the roxygen tag #' @method tidy midas_r to make that method definition explicit. Otherwise, without access to the repository, I'm unable to troubleshoot further.

guivivi commented 5 months ago

Indeed I have renamed the .R file and it works now. I have named the file as for instance do_tidy_method.R and its content is:

#' Tidy midasr
#' 
#' @description 
#' Tidy a midas_r object.
#'
#' @param x A `midas_r` object.
#' @param ... Unused, included for generic consistency only.
#' 
#' @return A tibble summarizing component-level
#' information about the midasr model.
#' 
#' @seealso
#' \code{\link[midasr]{midas_r}}
#'
#' @examples 
#' \dontrun{
#' }
#'
#' @export
tidy <- function(x, ...) {
  UseMethod("tidy")
}

#' @export
tidy.midas_r <- function(x, ...) {
  result <- summary(x)$coefficients %>%
    as_tibble(rownames = "term") %>%
    rename(estimate = 1, std.error = 2, statistic = 3, p.value = 4)

  result
}

In this way, the warning in R CMD build disappears, the tidy function appears as an own function of my package and it works with a midas_r object.

Many thanks for the support.

github-actions[bot] commented 5 months ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.