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

target_market_share(..., by_company = TRUE) outputs unexpected columns #291

Closed
jdhoffa opened this issue May 3, 2021 · 1 comment · Fixed by #292
Closed

target_market_share(..., by_company = TRUE) outputs unexpected columns #291

jdhoffa opened this issue May 3, 2021 · 1 comment · Fixed by #292

Comments

@jdhoffa
Copy link
Member

jdhoffa commented May 3, 2021

In particular, when comparing the output of target_market_share(..., by_company = FALSE) and target_market_share(..., by_company = TRUE), I would expect to see only the added column name_ald.

What I see in practice is three added columns: name_ald, and then the two unexpected columns sector_weighted_production and technology_weighted_production.

library(r2dii.data)
library(r2dii.match) 
library(r2dii.analysis)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

matched <- match_name(loanbook_demo, ald_demo) %>%
  prioritize()

out_false <- target_market_share(
  matched,
  ald_demo,
  scenario_demo_2020,
  region_isos_demo,
  by_company = FALSE
)

out_true <- target_market_share(
  matched,
  ald_demo,
  scenario_demo_2020,
  region_isos_demo,
  by_company = TRUE
  )
#> Warning: You've supplied `by_company = TRUE` and `weight_production = TRUE`.
#> This will result in company-level results, weighted by the portfolio
#> loan size, which is rarely useful. Did you mean to set one of these
#> arguments to `FALSE`?

# similarly, as expected, if by_company = TRUE, the output has the same columns 
# regardless of if weight_production = T or F
setdiff(names(out_true), names(out_false))
#> [1] "name_ald"                       "sector_weighted_production"    
#> [3] "technology_weighted_production"

# I would expect the only added column to be `name_ald`
setdiff(names(out_true), names(out_false)) == "name_ald"
#> [1]  TRUE FALSE FALSE

Created on 2021-05-03 by the reprex package (v2.0.0)

@maurolepore
Copy link
Contributor

maurolepore commented May 3, 2021

For the record, the test for this may come near the one that avoids the regression to #186:

test_that("outputs same names regardless of the value of `weight_production` (#186)", {
  out_weighted <- target_market_share(
    fake_matched(),
    fake_ald(),
    fake_scenario(),
    region_isos_stable,
    weight_production = TRUE
  )

  out_unweighted <- target_market_share(
    fake_matched(),
    fake_ald(),
    fake_scenario(),
    region_isos_stable,
    weight_production = FALSE
  )

  diff_names <- setdiff(names(out_unweighted), names(out_weighted))

  expect_equal(diff_names, character(0))
})

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 a pull request may close this issue.

2 participants