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

convert weights to psw class via vctrs #10

Merged
merged 2 commits into from
Jan 14, 2025
Merged

convert weights to psw class via vctrs #10

merged 2 commits into from
Jan 14, 2025

Conversation

malcolmbarrett
Copy link
Collaborator

@malcolmbarrett malcolmbarrett commented Jan 11, 2025

This PR adds the psw object type for managing weights. A key benefit here is tracking the estimand and transformations of the weights.

wt_ate() and friends do these behind the scenes so most users won't need these but they are exported for expansion purposes

library(propensity)
library(tibble)

# Example 1: Creating and inspecting psw objects
wts <- psw(c(0.5, 1.0, 1.5, 2.0), estimand = "ate")
wts
#> <psw{estimand = ate}[4]>
#> [1] 0.5 1.0 1.5 2.0
is_psw(wts)
#> [1] TRUE
estimand(wts)
#> [1] "ate"

# Example 2: Arithmetic operations with psw
result <- wts * 2
result
#> <psw{estimand = ate}[4]>
#> [1] 1 2 3 4

wts2 <- psw(c(0.3, 0.7, 1.1, 1.5), estimand = "cens")
combined <- wts * wts2
combined
#> <psw{estimand = ate * cens}[4]>
#> [1] 0.15 0.70 1.65 3.00

# Example 3: Math operations with psw
sqrt_wts <- sqrt(wts)
sqrt_wts
#> [1] 0.7071068 1.0000000 1.2247449 1.4142136

# Example 4: Using psw with tibble
data <- tibble(
  y = c(2, 4, 6, 8),
  x = c(0, 1, 0, 1),
  wts = wts
)

data$weighted_y <- data$y * as.double(data$wts)
data$weighted_mean <- sum(data$weighted_y) / sum(data$wts)
data
#> # A tibble: 4 × 5
#>       y     x        wts weighted_y weighted_mean
#>   <dbl> <dbl> <psw{ate}>      <dbl>         <dbl>
#> 1     2     0        0.5          1             6
#> 2     4     1        1.0          4             6
#> 3     6     0        1.5          9             6
#> 4     8     1        2.0         16             6

# Example 5: Complex expression with psw
y <- c(2, 4, 6, 8)
x <- c(0, 1, 0, 1)

term1 <- sum(y * x * wts) / sum(x * wts)
term2 <- sum(y * (1 - x) * wts) / sum((1 - x) * wts)

term1
#> [1] 6.666667
term2
#> [1] 5

Created on 2025-01-11 with reprex v2.1.1

If a model is fit with psw weights, we can tell what type of weights they are

# model code omitted 
model.frame(outcome_mod) |>
   model.weights() |>
   estimand()
#> [1] "ate"

@malcolmbarrett
Copy link
Collaborator Author

I rendered the book with this version of propensity to make sure it all still worked

@malcolmbarrett malcolmbarrett merged commit f48b6f0 into main Jan 14, 2025
6 checks passed
@malcolmbarrett malcolmbarrett deleted the vctrs branch January 14, 2025 18:40
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.

1 participant