Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data_read() preserves class for rds files #558

Merged
merged 6 commits into from
Oct 19, 2024
Merged

data_read() preserves class for rds files #558

merged 6 commits into from
Oct 19, 2024

Conversation

strengejacke
Copy link
Member

@strengejacke strengejacke commented Oct 18, 2024

Need to add tests.
Behaviour of older datawizard versions (see column agecat):

d <- datawizard::data_read("https://github.com/vincentarelbundock/marginaleffects/raw/refs/heads/main/vignettes/chapters/data/hiv.rds")
#> Reading data...
#> Variables where all values have associated labels are now converted into
#>   factors. If this is not intended, use `convert_factors = FALSE`.
#> 0 out of 8 variables were fully labelled and converted into factors.
str(d)
#> 'data.frame':    2884 obs. of  8 variables:
#>  $ village  : num  43 117 2 6 11 14 15 25 37 100 ...
#>  $ outcome  : num  1 0 0 0 0 0 1 0 1 0 ...
#>  $ distance : num  0.549 0.84 3.342 2.323 1.386 ...
#>  $ amount   : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ incentive: num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ age      : num  14 14 15 15 15 15 15 15 15 15 ...
#>  $ hiv2004  : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ agecat   : num  1 1 1 1 1 1 1 1 1 1 ...
#>  - attr(*, ".internal.selfref")=<externalptr>

new behaviour

d <- datawizard::data_read("https://github.com/vincentarelbundock/marginaleffects/raw/refs/heads/main/vignettes/chapters/data/hiv.rds")
#> Reading data...
str(d)
#> 'data.frame':    2884 obs. of  8 variables:
#>  $ village  : int  43 117 2 6 11 14 15 25 37 100 ...
#>  $ outcome  : int  1 0 0 0 0 0 1 0 1 0 ...
#>  $ distance : num  0.549 0.84 3.342 2.323 1.386 ...
#>  $ amount   : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ incentive: int  0 0 0 0 0 0 0 0 0 0 ...
#>  $ age      : int  14 14 15 15 15 15 15 15 15 15 ...
#>  $ hiv2004  : int  0 0 0 0 0 0 0 0 0 0 ...
#>  $ agecat   : Factor w/ 3 levels "<18","18-35",..: 1 1 1 1 1 1 1 1 1 1 ...
#>  - attr(*, ".internal.selfref")=<externalptr>

Created on 2024-10-19 with reprex v2.1.1

@strengejacke
Copy link
Member Author

strengejacke commented Oct 19, 2024

@etiennebacher Previously, we had the option convert_factors for SPSS, SAS, Stata und "unknown" (all that went to rio::import()). While you may have rds or RData files that have label-attributes, .post_process_imported_data() can reliably only deal with SPSS/SAS/Stata files. Everything that has no labels is converted into numeric (the usual variable type for those files after import). However, passing an rds-file through that function would also convert everything into numeric, even factors, integer, POSIXct etc.

There are two options:

  1. Add all possible classes to .post_process_imported_data(), to make sure variable types are preserved

    datawizard/R/data_read.R

    Lines 190 to 192 in 3f46e31

    } else {
    # else, fall back to numeric
    i <- as.numeric(i)

  2. simply only process data files that usually have labelled data, and where imported data is always numeric.

I think option 2) (in this PR) is safer. We now make sure rds-files (and other unknown formats) are returned as they are, but we no longer can automatically convert labelled numeric vectors into factors using convert_factors = TRUE (only for unknown! like we already had for txt/csv). I think the benefits (returning data as desired) outweigh the disadvantages (I guess people rarely have labelled data saved as rds or rdata, and even if so, we have other functions to convert labelled numerics into factors).

Copy link
Member

@etiennebacher etiennebacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I don't have a strong opinion on which option to follow but you use this more than me so what you have looks good to me.

NEWS.md Outdated Show resolved Hide resolved
@etiennebacher etiennebacher merged commit 003e2b8 into main Oct 19, 2024
14 checks passed
@etiennebacher etiennebacher deleted the fix_for_rds branch October 19, 2024 14:27
Copy link

codecov bot commented Oct 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.29%. Comparing base (3f46e31) to head (3d5febc).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #558   +/-   ##
=======================================
  Coverage   91.29%   91.29%           
=======================================
  Files          76       76           
  Lines        5983     5983           
=======================================
  Hits         5462     5462           
  Misses        521      521           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants