Skip to contents

Trim observations with extreme propensity scores by replacing them with NA, effectively removing those units from downstream analyses. The returned object has the same length (or dimensions) as the input, with trimmed entries set to NA. After trimming, refit the propensity score model on the retained observations with ps_refit().

Usage

ps_trim(
  .propensity,
  method = c("ps", "adaptive", "pctl", "pref", "cr", "optimal", "density", "resid"),
  lower = NULL,
  upper = NULL,
  .exposure = NULL,
  .focal_level = NULL,
  .reference_level = NULL,
  ...,
  .sigma = NULL,
  .density = "normal",
  .treated = NULL,
  .untreated = NULL,
  ps = lifecycle::deprecated()
)

Arguments

.propensity

A numeric vector of propensity scores in (0, 1) for binary exposures, a matrix / data frame where each column gives the propensity score for one level of a categorical exposure, or a fitted model (see Fitted models and Trimming a dose model in Details). A data frame trimmed for a binary exposure is reduced to a single column: the second column of a two column data frame, which is the probability of the second level in the layout model predictions come in, and the first column otherwise. The column taken is announced; options(propensity.quiet = TRUE) silences the announcement. A matrix is held to the same open interval as a vector, so a score of exactly 0 or 1 in any cell is refused and a separated multinomial fit cannot be repaired by trimming it: setting an extreme score to missing gains nothing from a score already at an endpoint. ps_trunc() reads the closed interval for a categorical matrix and is the repair; see Propensity scores at 0 and 1 in wt_ate(). A data frame holding a .pred_class column, which a fitted tidymodels classification model returns when no prediction type is named, carries predicted levels rather than probabilities and is refused with an error of class propensity_df_class_column_error.

method

Trimming method. One of:

  • "ps" (default): Fixed threshold. Observations with propensity scores outside [lower, upper] are trimmed. For categorical exposures, observations where any column falls below lower (the symmetric threshold delta) are trimmed.

  • "adaptive": Data-driven threshold that minimizes the asymptotic variance of the IPW estimator (Crump et al., 2009). The lower and upper arguments are ignored. The threshold adapts to the estimated scores, tightening under poor overlap, and the estimand becomes the population it keeps. ps_trunc(method = "adaptive") instead adapts to the sample size alone and keeps every unit.

  • "pctl": Quantile-based. Observations outside the [lower, upper] quantiles of the propensity score distribution are trimmed. Defaults: lower = 0.05, upper = 0.95.

  • "pref": Preference score trimming. Transforms propensity scores to the preference scale (Walker et al., 2013) and trims outside [lower, upper]. Requires .exposure. Binary exposures only. Defaults: lower = 0.3, upper = 0.7.

  • "cr": Common range (clinical equipoise). Trims to the overlap region of the propensity score distributions across exposure groups. Requires .exposure. Binary exposures only. The lower and upper arguments are ignored. When the two distributions do not overlap at all, so that the lowest score among the focal units sits above the highest among the reference units, the overlap region is empty and every observed unit is trimmed. That is a truthful record of an empty region rather than an error, and it differs deliberately from ps_trunc(), which refuses the same data with an error of class propensity_no_overlap_error because there is no range left to bound the scores to.

  • "optimal": Multi-category optimal trimming (Yang et al., 2016). Categorical exposures only. Requires .exposure.

  • "density": Quantile floor on the conditional density of a continuous exposure. Units whose conditional density at their observed dose falls below its lower quantile are trimmed. Default: lower = 0.01.

  • "resid": Bound on the absolute standardized residual of a continuous exposure. Units more than upper spreads from their predicted dose are trimmed. upper has no default.

"density" and "resid" need the residuals and the family of the model that fit the exposure's conditional mean, so they accept only a dose model (see Trimming a dose model in Details). They refuse a vector or matrix of values, a binomial or quasibinomial glm, and a multinom with an error of class propensity_method_error. A dose model accepts only these two methods, and refuses every other one, including the default "ps" when method is not supplied, with the same class.

For categorical exposures, only "ps" and "optimal" are supported.

lower, upper

Numeric thresholds whose interpretation depends on method:

  • "ps": absolute propensity score bounds (defaults: 0.1, 0.9). For categorical exposures, only lower is used, as the symmetric threshold delta, and it defaults to 0.1, the default ps_trunc() uses as well. With k exposure levels, a threshold of 1/k or larger cannot be met by every column of a row that sums to one, and is an error.

  • "pctl": quantile probabilities (defaults: 0.05, 0.95).

  • "pref": preference score bounds (defaults: 0.3, 0.7).

  • "adaptive", "cr", "optimal": ignored (thresholds are data-driven).

  • "density": lower is the quantile probability of the floor, in (0, 0.5) (default 0.01). upper is refused with an error of class propensity_unsupported_arg_error.

  • "resid": upper is the bound on the absolute standardized residual, a single positive finite number, and is required (an error of class propensity_missing_arg_error without it). lower is refused with an error of class propensity_unsupported_arg_error.

A "density" lower or "resid" upper out of range is refused with an error of class propensity_range_error.

.exposure

An exposure variable. Required for "pref", "cr" (binary vector), and "optimal" (factor or character). For "density" and "resid", the dose is read from the model's response unless supplied here. Not required for other methods.

.focal_level

The value of .exposure representing the focal (treated) group, used by "pref" and "cr". Every binary coding honors it: 0/1 numeric, logical, two-level factor, and two-level character exposures are all coded with the named level as focal, and a level the exposure never takes is an error. With no level named, a binary exposure defaults to its higher level, which is 1 for a 0/1 exposure, TRUE for a logical one, and the second of the two levels a factor or character exposure takes. Levels a factor declares but never takes are not candidates. Naming any other level reverses the coding, so .propensity must then hold the probability of the named level.

.reference_level

The value of .exposure representing the reference (control) group. Naming it makes the exposure's other level focal, with the same consequence for .propensity, and a level the exposure never takes is an error. Automatically detected if not supplied.

...

Additional arguments passed to methods.

.sigma

For "density" and "resid", a single residual spread to read the conditional density at, recorded as "supplied". With none supplied, the spread is the one the density family estimates from the residuals, as in wt_ate(): the root mean square, unless the family estimates a scale of its own. A spread for each unit is refused with an error of class propensity_sigma_error, and a spread supplied with a family that estimates its own scale is refused with an error of class propensity_density_error. The other methods refuse any .sigma with an error of class propensity_sigma_error.

.density

For "density" and "resid", the family of the conditional density, in any form wt_ate() accepts: "normal" (the default), "laplace", "kernel", a specification such as dens_t(), or a function of the standardized residual. "resid" accepts only dens_normal(), dens_t(), and dens_laplace(), and refuses a kernel or user-written density with an error of class propensity_density_error. The other methods refuse any family but the normal with the same class.

.treated

[Deprecated] Use .focal_level instead.

.untreated

[Deprecated] Use .reference_level instead.

ps

[Deprecated] Use .propensity instead. A call that names ps must name the arguments after it as well, since a positional argument binds to .propensity.

Value

A ps_trim object (a numeric vector with class "ps_trim", or a matrix with class "ps_trim_matrix"). Trimmed observations are NA. Metadata is stored in the "ps_trim_meta" attribute and can be accessed with ps_trim_meta(). Key fields include:

  • method: the trimming method used

  • keep_idx: integer indices of retained observations

  • trimmed_idx: integer indices of trimmed (NA) observations

  • n_obs: the number of observations those indices describe

  • Method-specific fields such as cutoff (adaptive), q_lower/q_upper (pctl), cr_lower/cr_upper (cr), delta (categorical ps), or lambda (optimal)

  • focal_inverted (vector scores only): TRUE when the scores are one minus the probability a fitted model reports, because the model was trimmed with its first level named as focal, and FALSE otherwise, including for every vector of scores supplied directly

A trim of a dose model records method, lower ("density", else NULL), upper ("resid", else NULL), threshold (the realized floor on the conditional density), sigma (the spread it was read at), sigma_kind ("pooled", "mle", or "supplied"), density (the density specification), keep_idx, trimmed_idx, and n_obs.

Details

How trimming works

Trimming identifies observations with extreme (near 0 or 1) propensity scores and sets them to NA. These observations are excluded from subsequent weight calculations and effect estimation. The goal is to remove units that lack sufficient overlap between exposure groups, which would otherwise receive extreme weights and destabilize estimates.

Choosing a method

  • Use "ps" when you have a specific threshold in mind or want a simple default.

  • Use "adaptive" for a principled, data-driven cutoff that targets variance reduction.

  • Use "pctl" to trim a fixed percentage of extreme values from each tail.

  • Use "pref" when you want to restrict to the region of clinical equipoise based on the preference score.

  • Use "cr" to restrict to the common support region where both exposure groups have observed propensity scores.

  • Use "optimal" for multi-category (3+) exposures; this is the only data-driven method available for categorical treatments.

  • Use "density" or "resid" for a continuous exposure, on its dose model.

For a binary or categorical exposure, trimming and refitting is the first recourse when the analysis can accept a change of estimand. When every unit must be kept, bound the scores with ps_trunc() instead, for a binary exposure with method = "adaptive", or bound the weights with wt_trunc().

Typical workflow

  1. Fit a propensity score model

  2. Apply ps_trim() to flag extreme values

  3. Call ps_refit() to re-estimate propensity scores on the retained sample

  4. Compute weights with wt_ate() or another weight function

Fitted models

.propensity can be the fitted propensity score model instead of the scores it reports. A binomial stats::glm() and a two-level nnet::multinom() are read as one score per unit; a nnet::multinom() of three or more levels is read as one column per level. Those are the shapes predict(fit, type = "response") and fitted(fit) give, and trimming a fit trims exactly what trimming those values would.

The methods that read an exposure ("pref", "cr", and every method on the categorical route) take it from the model when .exposure is not supplied, announcing the variable they read; options(propensity.quiet = TRUE) silences the announcement. An .exposure you supply is used instead, and a categorical model's columns are matched to its levels by name, so an exposure whose levels are ordered differently is still trimmed against the right column.

Trimming a dose model

For a continuous exposure, .propensity can be the model of the exposure's conditional mean: a stats::lm(), a glm of the gaussian() family (or another family whose variance is constant), a MASS::rlm(), or an mgcv::gam(), the models wt_ate() reads a dose from. A glm whose spread changes with its mean, such as poisson(), is refused with an error of class propensity_model_family_error. Only "density" and "resid" apply.

For a dose, the generalized propensity score is the conditional density \(f(a \mid x)\) (Hirano and Imbens, 2004), and a unit whose observed dose has a small conditional density gets a large weight, as a unit with a propensity score near 0 does for a binary exposure. Both methods read the fitted conditional mean mu, the spread sigma of the residuals under the family in .density, the standardized residual z = (a - mu) / sigma, and the conditional density f = g(z) / sigma, where g is the family's standardized density. "density" keeps the units whose f is at least its lower quantile. "resid" keeps those whose |z| is at most upper; for the symmetric unimodal families it accepts, that is the floor g(upper) / sigma on f, which is the threshold it records. The trim is one-sided, at the low end of the density, since only a small density makes a large weight (Branson et al., 2024).

The retained values are the conditional means, and the trimmed ones are NA. A unit with a missing mean or dose takes no part in the trim. .focal_level, .reference_level, and their deprecated forms are refused with an error of class propensity_focal_level_error, since a dose has no levels.

A trim changes the estimand. The analysis describes the units whose observed dose was plausible under the model, not the full sample, and which units those are depends on the threshold. Branson et al. (2024) define the trimmed dose response at each dose among the units whose conditional density at that dose exceeds the threshold, a population that changes with the dose; this trim is its sample analogue at each unit's own observed dose rather than the same population. A dose-response curve fit to the trimmed sample is a curve for the units it keeps.

After trimming, ps_refit() refits the conditional mean on the retained rows and re-estimates the spread there under the recorded family, and wt_ate() builds the weights from the refit under the family and spread the record holds. ipw() refuses those weights, as it refuses every weight built from a trimmed score.

The other way to hold down extreme weights for a dose is wt_trunc(), which bounds the weights and keeps every unit; see Truncating weights or trimming the density there for the simulation results behind this summary. With a density family that fits the residuals, a density trim at lower = 0.01 did not help: at an effective sample size of 64% of the sample, its interval covered 0.843, against 0.933 for the untrimmed weights under the same fixed-weight interval. A loose bound on the weights left that coverage where it was and lowered the root mean squared error. With heavy-tailed residuals read through a normal density, or a residual spread the model left out, the trim covered 0.979 to 0.989, against 0.618 to 0.881 for the untrimmed weights and 0.644 to 0.906 for the bound, because it removed the units the misspecified density fit worst. Two cautions go with those numbers. The trim's intervals were computed on the trimmed sample, held the weights fixed, and were conservative, and its bias was measured against the effect in the full population, so part of that bias is the change of estimand. "resid" was not simulated; the results carry over to it only because a bound on the standardized residual is a floor on the conditional density.

A family that fits the residuals is the better repair where it applies. Under heavy tails, dens_t() with 4 degrees of freedom at its default scale had about a third of the root mean squared error of the bounded normal weights and an M-estimation interval that covered 0.926 to 0.939 with standard errors close to right, and it kept every unit. The trim's coverage was higher, but its interval was conservative. Check the family first; trim when the units at the low-density end are ones the analysis should not describe, and say so when reporting the estimand.

Object behavior

Arithmetic operations on ps_trim objects return plain numeric vectors, since transformed propensity scores (e.g., 1/ps) are no longer propensity scores. Trimmed values propagate as NA in calculations; use na.rm = TRUE where appropriate.

When combining ps_trim objects with c(), trimming parameters must match. Mismatched parameters trigger a warning and return a numeric vector.

Use ps_trim_meta() to inspect the trimming metadata, including the method, cutoffs, and which observations were retained or trimmed.

Missing values

A propensity score that arrives missing is not one this function removed, so it takes no part in the trimming record: it joins neither the retained nor the trimmed positions, and is_unit_trimmed() reports FALSE for it. The value propagates as NA into the result. For a matrix of categorical propensity scores, a row with a missing cell comes back exactly as it arrived, since there is no complete probability vector to place against a threshold.

The methods that read a cutoff off the scores read it off the scores they have. "adaptive", "pctl", "cr", and "optimal" all work their cutoffs out from the complete scores or rows, so the cutoff is the one the same call would produce with the missing observations dropped.

"pref" centers its preference scores on the proportion exposed across the whole sample, which is a fact about the exposure rather than about the scores, so a unit whose propensity score is missing still counts toward it and the cutoff is not the one the shorter call would produce. That unit's own preference score is missing, which leaves it outside both the retained and the trimmed positions like any other missing score.

A missing exposure is a different matter, and "pref" and "cr" refuse one with an error of class propensity_missing_value_error. Their cutoffs come from the exposure groups, and a unit that belongs to neither leaves them undefined. Remove or impute the missing exposure values first.

The trimming record

A ps_trim records which units were trimmed as positions among the observations it was written for, along with how many observations that was. Operations that hand this package the subscript re-index those positions onto the result: subsetting with [, sort(), rep(), and na.omit() all return a record written for what they return, and a subscript naming a position more than once reports that unit at every place it now holds. unique() does the same when it can, as described below.

Operations that change how many observations there are without supplying a subscript cannot re-index the record, and it is dropped rather than worked out from the values, since reading membership back from the NA pattern would report a propensity score that arrived missing as one this package removed. vctrs::vec_slice(), which is how filtering, joining, and grouped verbs in dplyr reach a column, is the usual route, and combining two or more vectors with c() is another, because concatenation appends one set of observations to another. The record is dropped without comment on every route: most of these length changes build vectors the caller never holds, such as the pieces a grouped verb slices a column into or the rows a tibble slices off to print, so a warning would mostly describe something that is not the result. The values, the class, and the method and its cutoffs are untouched.

A combine drops the positions even when it is handed a single vector. c() of one ps_trim returns it unchanged, record included, but vctrs::vec_c(x), dplyr::bind_rows(df), vctrs::vec_rbind(df), and an ungrouped dplyr::reframe() rebuild the column, so a later ps_refit() or is_unit_trimmed() on the result refuses it.

unique() keeps one element for each distinct value, or one row for each distinct row of a matrix of scores, and that element or row stands for every unit holding the same scores. A matrix comes back as a matrix of the same class with its column names. The record is re-indexed onto the result when all of the merged units share one status, and dropped otherwise: a trimmed score and one that arrived missing are both NA, and a trimmed row is NA throughout, so a vector or matrix holding both returns a single NA element or row that neither status describes.

as.data.frame() and tibble::as_tibble() turn a matrix of scores into a data frame whose columns are ps_trim vectors carrying the matrix's record, one row per unit, so the weight functions read the data frame as they read the matrix and a subset of its rows re-indexes the record. A data frame whose columns are not all trimmed with the same record is refused by the weight functions with an error of class propensity_matrix_type_error. A matrix without column names gives columns named V1, V2, and so on, which the weight functions then refuse because they name no exposure level, so name the columns after the exposure levels before converting.

A record can also outlive the observations it describes, because it travels by routes vctrs does not see: growing a ps_trim by subassignment carries it across the length change. is_unit_trimmed() and ps_refit() therefore check that the record covers the object they are given and raise an error of class propensity_missing_meta_error when it does not, rather than name trimmed units at stale positions.

That check compares how many observations the record was written for against how many the object holds, which a reordering does not change, so a route that could reorder the observations without saying how drops the positions instead, at any length: vctrs::vec_slice(), dplyr::arrange(), dplyr::filter(), and vctrs::vec_assign() and the helpers built on it return a ps_trim whose record keeps its method and cutoffs and names no units, and is_unit_trimmed() and ps_refit() refuse it. Subsetting with [, sort(), unique(), and rep() know where the units went and re-index the record, and [<- and is.na<- move no unit and keep it, so reorder with [, or put the propensity scores in the order you want before trimming them.

Casting a numeric vector into a ps_trim with vctrs::vec_cast() is a type operation and not a trimming. The result is described by the method and cutoffs of the target and records that none of the arriving values was trimmed, so it can hold scores outside those cutoffs, including 0 and 1. Call ps_trim() on the scores to trim them.

References

Branson, Z., Kennedy, E. H., Balakrishnan, S., & Wasserman, L. (2024). Causal effect estimation after propensity score trimming with continuous treatments. arXiv preprint arXiv:2309.00706.

Crump, R. K., Hotz, V. J., Imbens, G. W., & Mitnik, O. A. (2009). Dealing with limited overlap in estimation of average treatment effects. Biometrika, 96(1), 187–199.

Hirano, K., & Imbens, G. W. (2004). The propensity score with continuous treatments. In Applied Bayesian Modeling and Causal Inference from Incomplete-Data Perspectives (pp. 73–84).

Walker, A. M., Patrick, A. R., Lauer, M. S., et al. (2013). A tool for assessing the feasibility of comparative effectiveness research. Comparative Effectiveness Research, 3, 11–20.

Yang, S., Imbens, G. W., Cui, Z., Faries, D. E., & Kadziola, Z. (2016). Propensity score matching and subclassification in observational studies with multi-level treatments. Biometrics, 72(4), 1055–1065.

See also

ps_trunc() for bounding (winsorizing) instead of discarding, ps_refit() to re-estimate propensity scores after trimming, ps_calibrate() for calibration-based adjustment, ps_trim_meta() to inspect trimming metadata, is_ps_trimmed() and is_unit_trimmed() for logical queries.

Examples

set.seed(2)
n <- 300
x <- rnorm(n)
z <- rbinom(n, 1, plogis(1.3 * x))
fit <- glm(z ~ x, family = binomial)
ps <- predict(fit, type = "response")

# Fixed threshold trimming (default)
trimmed <- ps_trim(ps, method = "ps", lower = 0.1, upper = 0.9)
trimmed
#> <ps_trim; trimmed 44 of 300[300]>
#>         1         2         3         4         5         6         7         8 
#> 0.1780112 0.4934483 0.8725819 0.1353577 0.4025855 0.4752489 0.6683913 0.3506150 
#>         9        10        11        12        13        14        15        16 
#>        NA 0.3831809 0.5738044 0.7467782 0.3038553 0.1508037 0.8997256        NA 
#>        17        18        19        20        21        22        23        24 
#> 0.7187207 0.4419184 0.7548592 0.5787647        NA 0.1244366 0.8728587        NA 
#>        25        26        27        28        29        30        31        32 
#> 0.4313639        NA 0.5939254 0.2474276 0.6938170 0.5298266 0.6778670 0.5399668 
#>        33        34        35        36        37        38        39        40 
#> 0.7707828 0.3366773 0.2037945 0.2476600        NA 0.1768609 0.2572600 0.3484614 
#>        41        42        43        44        45        46        47        48 
#> 0.3065403        NA 0.1895191        NA 0.6415565        NA 0.3300895 0.3990495 
#>        49        50        51        52        53        54        55        56 
#> 0.3683877 0.1246121 0.1902500        NA 0.2564150 0.8160957 0.1494022        NA 
#>        57        58        59        60        61        62        63        64 
#> 0.3247367 0.7345271 0.7859021 0.8849770        NA        NA 0.2208817 0.4841803 
#>        65        66        67        68        69        70        71        72 
#> 0.6036086 0.1941979        NA 0.2790100 0.4585602 0.1783019 0.1731103 0.5439312 
#>        73        74        75        76        77        78        79        80 
#> 0.3822372 0.5796397 0.4114856 0.1759469 0.8218240 0.6877562 0.7649259        NA 
#>        81        82        83        84        85        86        87        88 
#> 0.7505008        NA 0.2641422 0.1005949        NA        NA 0.2330120 0.3365148 
#>        89        90        91        92        93        94        95        96 
#> 0.3055473 0.5632496 0.8745082 0.8863194 0.1269292 0.1023453        NA 0.1166039 
#>        97        98        99       100       101       102       103       104 
#>        NA 0.4322875 0.1893249 0.2462384 0.7703638 0.5197606 0.3273939 0.2099624 
#>       105       106       107       108       109       110       111       112 
#> 0.1851823        NA 0.7356255        NA 0.2954896 0.3163021 0.1530042 0.3471981 
#>       113       114       115       116       117       118       119       120 
#> 0.5921212 0.8328274 0.6227067 0.5867797 0.8065734 0.7877456 0.4663064 0.2023006 
#>       121       122       123       124       125       126       127       128 
#> 0.8087856 0.4774812 0.8903829 0.2928150 0.1499937 0.6139848 0.2290136 0.6467536 
#>       129       130       131       132       133       134       135       136 
#>        NA        NA 0.6627758 0.5441083 0.7165979        NA 0.8025574 0.7998822 
#>       137       138       139       140       141       142       143       144 
#> 0.7597742 0.6921037        NA        NA 0.2509264 0.5711077 0.1970442 0.4590332 
#>       145       146       147       148       149       150       151       152 
#> 0.6800801 0.2329425 0.6525433 0.6180388 0.1970997 0.1584870 0.7452343 0.3731672 
#>       153       154       155       156       157       158       159       160 
#> 0.6727629 0.1889404 0.8164247 0.1043218 0.6858279 0.5895482 0.5223260 0.6558189 
#>       161       162       163       164       165       166       167       168 
#> 0.5672709 0.2368400 0.3418011 0.5540597 0.1083159 0.1806594        NA        NA 
#>       169       170       171       172       173       174       175       176 
#> 0.1187105 0.7490531 0.7738375 0.7077039 0.4491471 0.5416643 0.1764396 0.2333126 
#>       177       178       179       180       181       182       183       184 
#> 0.3434474 0.1704628 0.7023006        NA 0.1524604 0.1153463 0.5651259 0.1351849 
#>       185       186       187       188       189       190       191       192 
#> 0.6161453 0.7945146 0.4382952 0.6065642 0.2328341 0.6027459 0.1139088 0.4037497 
#>       193       194       195       196       197       198       199       200 
#> 0.1040353 0.3422376 0.7735701 0.6661116 0.2893391 0.2011360 0.1863223 0.2107038 
#>       201       202       203       204       205       206       207       208 
#> 0.5327159 0.1544197        NA 0.5052145 0.1642856 0.5622725 0.3887452 0.3163883 
#>       209       210       211       212       213       214       215       216 
#> 0.6341619 0.5095796 0.7582940 0.2665601        NA        NA 0.4774214 0.5852567 
#>       217       218       219       220       221       222       223       224 
#> 0.8030404 0.1067663 0.1338595 0.8855459 0.5671613 0.6707177 0.2000938        NA 
#>       225       226       227       228       229       230       231       232 
#> 0.4538222 0.5912550 0.3993802 0.7230688 0.3094663        NA 0.8702859        NA 
#>       233       234       235       236       237       238       239       240 
#> 0.2415827 0.4195113        NA 0.1483657 0.2541910 0.5957602 0.4626159 0.2496911 
#>       241       242       243       244       245       246       247       248 
#> 0.4020747        NA 0.7936546 0.6179515 0.6899355 0.2556513 0.4650462 0.5270157 
#>       249       250       251       252       253       254       255       256 
#> 0.2803714 0.2850014 0.6020472 0.3002118 0.3705819 0.3257820 0.7087374 0.5960756 
#>       257       258       259       260       261       262       263       264 
#> 0.3306472 0.3363861 0.7464321 0.3725508 0.8297224 0.3965473 0.3250342        NA 
#>       265       266       267       268       269       270       271       272 
#> 0.2345937        NA        NA 0.5886632 0.6106387 0.4172303        NA 0.4137080 
#>       273       274       275       276       277       278       279       280 
#> 0.4312953 0.1206438 0.5164038 0.8752400 0.4176263 0.7591012 0.3016993 0.4527210 
#>       281       282       283       284       285       286       287       288 
#>        NA 0.6501483 0.8168531 0.2393292 0.8311549 0.8851143 0.7936328 0.4317872 
#>       289       290       291       292       293       294       295       296 
#> 0.8222307 0.3983355 0.1356224 0.6302455 0.4602807 0.3527491 0.8562989 0.3153233 
#>       297       298       299       300 
#> 0.5741435        NA 0.1008201 0.2253829 

# How many observations were trimmed?
sum(is_unit_trimmed(trimmed))
#> [1] 44

# Data-driven adaptive trimming
ps_trim(ps, method = "adaptive")
#> <ps_trim; trimmed 44 of 300[300]>
#>         1         2         3         4         5         6         7         8 
#> 0.1780112 0.4934483 0.8725819 0.1353577 0.4025855 0.4752489 0.6683913 0.3506150 
#>         9        10        11        12        13        14        15        16 
#>        NA 0.3831809 0.5738044 0.7467782 0.3038553 0.1508037 0.8997256        NA 
#>        17        18        19        20        21        22        23        24 
#> 0.7187207 0.4419184 0.7548592 0.5787647        NA 0.1244366 0.8728587        NA 
#>        25        26        27        28        29        30        31        32 
#> 0.4313639        NA 0.5939254 0.2474276 0.6938170 0.5298266 0.6778670 0.5399668 
#>        33        34        35        36        37        38        39        40 
#> 0.7707828 0.3366773 0.2037945 0.2476600        NA 0.1768609 0.2572600 0.3484614 
#>        41        42        43        44        45        46        47        48 
#> 0.3065403        NA 0.1895191        NA 0.6415565        NA 0.3300895 0.3990495 
#>        49        50        51        52        53        54        55        56 
#> 0.3683877 0.1246121 0.1902500        NA 0.2564150 0.8160957 0.1494022        NA 
#>        57        58        59        60        61        62        63        64 
#> 0.3247367 0.7345271 0.7859021 0.8849770        NA        NA 0.2208817 0.4841803 
#>        65        66        67        68        69        70        71        72 
#> 0.6036086 0.1941979        NA 0.2790100 0.4585602 0.1783019 0.1731103 0.5439312 
#>        73        74        75        76        77        78        79        80 
#> 0.3822372 0.5796397 0.4114856 0.1759469 0.8218240 0.6877562 0.7649259        NA 
#>        81        82        83        84        85        86        87        88 
#> 0.7505008        NA 0.2641422 0.1005949        NA        NA 0.2330120 0.3365148 
#>        89        90        91        92        93        94        95        96 
#> 0.3055473 0.5632496 0.8745082 0.8863194 0.1269292 0.1023453        NA 0.1166039 
#>        97        98        99       100       101       102       103       104 
#>        NA 0.4322875 0.1893249 0.2462384 0.7703638 0.5197606 0.3273939 0.2099624 
#>       105       106       107       108       109       110       111       112 
#> 0.1851823        NA 0.7356255        NA 0.2954896 0.3163021 0.1530042 0.3471981 
#>       113       114       115       116       117       118       119       120 
#> 0.5921212 0.8328274 0.6227067 0.5867797 0.8065734 0.7877456 0.4663064 0.2023006 
#>       121       122       123       124       125       126       127       128 
#> 0.8087856 0.4774812 0.8903829 0.2928150 0.1499937 0.6139848 0.2290136 0.6467536 
#>       129       130       131       132       133       134       135       136 
#>        NA        NA 0.6627758 0.5441083 0.7165979        NA 0.8025574 0.7998822 
#>       137       138       139       140       141       142       143       144 
#> 0.7597742 0.6921037        NA        NA 0.2509264 0.5711077 0.1970442 0.4590332 
#>       145       146       147       148       149       150       151       152 
#> 0.6800801 0.2329425 0.6525433 0.6180388 0.1970997 0.1584870 0.7452343 0.3731672 
#>       153       154       155       156       157       158       159       160 
#> 0.6727629 0.1889404 0.8164247 0.1043218 0.6858279 0.5895482 0.5223260 0.6558189 
#>       161       162       163       164       165       166       167       168 
#> 0.5672709 0.2368400 0.3418011 0.5540597 0.1083159 0.1806594        NA        NA 
#>       169       170       171       172       173       174       175       176 
#> 0.1187105 0.7490531 0.7738375 0.7077039 0.4491471 0.5416643 0.1764396 0.2333126 
#>       177       178       179       180       181       182       183       184 
#> 0.3434474 0.1704628 0.7023006        NA 0.1524604 0.1153463 0.5651259 0.1351849 
#>       185       186       187       188       189       190       191       192 
#> 0.6161453 0.7945146 0.4382952 0.6065642 0.2328341 0.6027459 0.1139088 0.4037497 
#>       193       194       195       196       197       198       199       200 
#> 0.1040353 0.3422376 0.7735701 0.6661116 0.2893391 0.2011360 0.1863223 0.2107038 
#>       201       202       203       204       205       206       207       208 
#> 0.5327159 0.1544197        NA 0.5052145 0.1642856 0.5622725 0.3887452 0.3163883 
#>       209       210       211       212       213       214       215       216 
#> 0.6341619 0.5095796 0.7582940 0.2665601        NA        NA 0.4774214 0.5852567 
#>       217       218       219       220       221       222       223       224 
#> 0.8030404 0.1067663 0.1338595 0.8855459 0.5671613 0.6707177 0.2000938        NA 
#>       225       226       227       228       229       230       231       232 
#> 0.4538222 0.5912550 0.3993802 0.7230688 0.3094663        NA 0.8702859        NA 
#>       233       234       235       236       237       238       239       240 
#> 0.2415827 0.4195113        NA 0.1483657 0.2541910 0.5957602 0.4626159 0.2496911 
#>       241       242       243       244       245       246       247       248 
#> 0.4020747        NA 0.7936546 0.6179515 0.6899355 0.2556513 0.4650462 0.5270157 
#>       249       250       251       252       253       254       255       256 
#> 0.2803714 0.2850014 0.6020472 0.3002118 0.3705819 0.3257820 0.7087374 0.5960756 
#>       257       258       259       260       261       262       263       264 
#> 0.3306472 0.3363861 0.7464321 0.3725508 0.8297224 0.3965473 0.3250342        NA 
#>       265       266       267       268       269       270       271       272 
#> 0.2345937        NA        NA 0.5886632 0.6106387 0.4172303        NA 0.4137080 
#>       273       274       275       276       277       278       279       280 
#> 0.4312953 0.1206438 0.5164038 0.8752400 0.4176263 0.7591012 0.3016993 0.4527210 
#>       281       282       283       284       285       286       287       288 
#>        NA 0.6501483 0.8168531 0.2393292 0.8311549 0.8851143 0.7936328 0.4317872 
#>       289       290       291       292       293       294       295       296 
#> 0.8222307 0.3983355 0.1356224 0.6302455 0.4602807 0.3527491 0.8562989 0.3153233 
#>       297       298       299       300 
#> 0.5741435        NA 0.1008201 0.2253829 

# Quantile-based trimming at 5th and 95th percentiles
ps_trim(ps, method = "pctl")
#> <ps_trim; trimmed 30 of 300[300]>
#>          1          2          3          4          5          6          7 
#> 0.17801124 0.49344831 0.87258189 0.13535765 0.40258554 0.47524889 0.66839132 
#>          8          9         10         11         12         13         14 
#> 0.35061504         NA 0.38318089 0.57380442 0.74677823 0.30385529 0.15080370 
#>         15         16         17         18         19         20         21 
#> 0.89972561         NA 0.71872070 0.44191837 0.75485924 0.57876473         NA 
#>         22         23         24         25         26         27         28 
#> 0.12443658 0.87285871         NA 0.43136394         NA 0.59392536 0.24742762 
#>         29         30         31         32         33         34         35 
#> 0.69381700 0.52982663 0.67786695 0.53996676 0.77078278 0.33667732 0.20379449 
#>         36         37         38         39         40         41         42 
#> 0.24766004         NA 0.17686094 0.25726004 0.34846140 0.30654026         NA 
#>         43         44         45         46         47         48         49 
#> 0.18951912 0.91394741 0.64155648         NA 0.33008953 0.39904946 0.36838768 
#>         50         51         52         53         54         55         56 
#> 0.12461207 0.19024995         NA 0.25641497 0.81609573 0.14940217         NA 
#>         57         58         59         60         61         62         63 
#> 0.32473671 0.73452714 0.78590206 0.88497697         NA         NA 0.22088166 
#>         64         65         66         67         68         69         70 
#> 0.48418025 0.60360857 0.19419787         NA 0.27900998 0.45856017 0.17830185 
#>         71         72         73         74         75         76         77 
#> 0.17311028 0.54393115 0.38223719 0.57963968 0.41148556 0.17594694 0.82182398 
#>         78         79         80         81         82         83         84 
#> 0.68775620 0.76492590 0.09594356 0.75050082 0.06658957 0.26414219 0.10059488 
#>         85         86         87         88         89         90         91 
#>         NA 0.90461881 0.23301201 0.33651483 0.30554734 0.56324964 0.87450818 
#>         92         93         94         95         96         97         98 
#> 0.88631941 0.12692921 0.10234530 0.08426251 0.11660386         NA 0.43228752 
#>         99        100        101        102        103        104        105 
#> 0.18932487 0.24623842 0.77036380 0.51976065 0.32739386 0.20996243 0.18518227 
#>        106        107        108        109        110        111        112 
#>         NA 0.73562548         NA 0.29548965 0.31630208 0.15300421 0.34719807 
#>        113        114        115        116        117        118        119 
#> 0.59212123 0.83282741 0.62270671 0.58677974 0.80657336 0.78774559 0.46630644 
#>        120        121        122        123        124        125        126 
#> 0.20230064 0.80878565 0.47748117 0.89038286 0.29281504 0.14999367 0.61398482 
#>        127        128        129        130        131        132        133 
#> 0.22901364 0.64675362 0.06419209         NA 0.66277576 0.54410827 0.71659793 
#>        134        135        136        137        138        139        140 
#>         NA 0.80255741 0.79988215 0.75977424 0.69210372         NA 0.09079429 
#>        141        142        143        144        145        146        147 
#> 0.25092645 0.57110768 0.19704419 0.45903320 0.68008008 0.23294255 0.65254331 
#>        148        149        150        151        152        153        154 
#> 0.61803876 0.19709968 0.15848705 0.74523426 0.37316715 0.67276290 0.18894043 
#>        155        156        157        158        159        160        161 
#> 0.81642474 0.10432180 0.68582789 0.58954824 0.52232604 0.65581895 0.56727085 
#>        162        163        164        165        166        167        168 
#> 0.23684003 0.34180113 0.55405967 0.10831585 0.18065940         NA         NA 
#>        169        170        171        172        173        174        175 
#> 0.11871049 0.74905308 0.77383752 0.70770386 0.44914706 0.54166428 0.17643958 
#>        176        177        178        179        180        181        182 
#> 0.23331263 0.34344738 0.17046280 0.70230062 0.07304003 0.15246043 0.11534630 
#>        183        184        185        186        187        188        189 
#> 0.56512587 0.13518488 0.61614535 0.79451459 0.43829517 0.60656419 0.23283410 
#>        190        191        192        193        194        195        196 
#> 0.60274592 0.11390882 0.40374970 0.10403526 0.34223762 0.77357008 0.66611156 
#>        197        198        199        200        201        202        203 
#> 0.28933909 0.20113598 0.18632231 0.21070377 0.53271587 0.15441973         NA 
#>        204        205        206        207        208        209        210 
#> 0.50521454 0.16428559 0.56227253 0.38874517 0.31638831 0.63416193 0.50957963 
#>        211        212        213        214        215        216        217 
#> 0.75829404 0.26656013 0.90146489 0.09307013 0.47742144 0.58525674 0.80304044 
#>        218        219        220        221        222        223        224 
#> 0.10676633 0.13385955 0.88554593 0.56716130 0.67071770 0.20009381 0.91614132 
#>        225        226        227        228        229        230        231 
#> 0.45382217 0.59125501 0.39938022 0.72306882 0.30946625         NA 0.87028588 
#>        232        233        234        235        236        237        238 
#>         NA 0.24158271 0.41951127 0.06438704 0.14836574 0.25419100 0.59576019 
#>        239        240        241        242        243        244        245 
#> 0.46261588 0.24969111 0.40207472         NA 0.79365463 0.61795146 0.68993549 
#>        246        247        248        249        250        251        252 
#> 0.25565132 0.46504618 0.52701568 0.28037136 0.28500140 0.60204716 0.30021180 
#>        253        254        255        256        257        258        259 
#> 0.37058188 0.32578201 0.70873738 0.59607558 0.33064721 0.33638608 0.74643214 
#>        260        261        262        263        264        265        266 
#> 0.37255079 0.82972242 0.39654734 0.32503416         NA 0.23459370 0.09722939 
#>        267        268        269        270        271        272        273 
#> 0.91692000 0.58866324 0.61063867 0.41723032         NA 0.41370803 0.43129530 
#>        274        275        276        277        278        279        280 
#> 0.12064385 0.51640377 0.87523995 0.41762629 0.75910122 0.30169925 0.45272098 
#>        281        282        283        284        285        286        287 
#>         NA 0.65014831 0.81685306 0.23932916 0.83115493 0.88511432 0.79363282 
#>        288        289        290        291        292        293        294 
#> 0.43178717 0.82223069 0.39833546 0.13562237 0.63024555 0.46028071 0.35274910 
#>        295        296        297        298        299        300 
#> 0.85629892 0.31532329 0.57414353         NA 0.10082012 0.22538288 

# Refit after trimming, then compute weights
trimmed <- ps_trim(ps, method = "adaptive")
refitted <- ps_refit(trimmed, fit)
wt_ate(refitted, .exposure = z)
#> ℹ Treating `.exposure` as binary
#> <psw{estimand = ate; trimmed}[300]>
#>   [1] 1.240579 2.058164 1.179439 1.179070 2.479678 2.130709 1.546037 1.552398
#>   [9]       NA 2.593380 2.268587 1.386216 1.455212 1.200838 1.140328       NA
#>  [17] 3.272545 1.782675 1.371331 2.292253       NA 1.163991 1.179032       NA
#>  [25] 2.328539       NA 1.731055 3.842912 1.490847 1.926820 1.525053 1.893076
#>  [33] 3.917615 2.914998 1.280093 1.352152       NA 1.238860 1.368835 1.547658
#>  [41] 1.460487       NA 4.876301       NA 2.643752       NA 2.967353 2.499634
#>  [49] 2.687495 1.164231 1.259096       NA 3.722181 1.266295 6.029169       NA
#>  [57] 3.011351 1.409295 1.316469 1.161399       NA       NA 1.307391 1.913671
#>  [65] 1.704729 1.265160       NA 1.407989 1.832054 1.241014 1.233279 1.880192
#>  [73] 2.599182 1.771319 1.698883 5.210593 4.889604 2.985370 1.353148       NA
#>  [81] 1.379326       NA 1.381016 1.131873       NA       NA 1.327354 1.521848
#>  [89] 3.180870 2.219879 1.176614 1.159464 1.167411 1.134197       NA 1.153324
#>  [97]       NA 1.755289 1.257681 1.349711 3.911330 2.040050 2.989342 1.289839
#> [105] 1.251377       NA 3.455793       NA 3.277919 1.479963 5.902359 1.544890
#> [113] 1.736044 1.239690 2.526861 1.750976 1.281806 1.313320 1.855984 1.277750
#> [121] 4.594831 1.891632 7.509198 1.433882 1.199683 2.476368 1.320718 2.678015
#> [129]       NA       NA 1.558717 1.879621 1.444250       NA 4.467031 1.292873
#> [137] 1.362404 1.494456       NA       NA 1.357789 1.796221 4.709750 1.833498
#> [145] 1.520225 1.327239 2.717307 1.666857 1.269646 1.211872 1.389090 1.603744
#> [153] 1.536293 4.889627 4.762739 1.136827 1.507810 1.743207 2.049824 2.740082
#> [161] 1.807638 1.333760 1.533168 2.179201 1.142162 1.244550       NA       NA
#> [169] 7.402829 1.381999 1.337371 1.462140 2.244100 1.887538 1.238231 1.327856
#> [177] 2.863148 1.229363 3.113295       NA 1.203205 1.151622 2.228379 1.178829
#> [185] 2.488677 1.301856 1.772273 1.696843 1.327058 1.707044 1.149680 2.473178
#> [193] 1.136445 1.534110 3.959981 1.551162 1.427282 1.275928 4.950895 1.291019
#> [201] 1.917087 1.206013       NA 2.013803 1.220295 2.215480 1.641148 1.480137
#> [209] 2.596564 1.997821 1.365083 1.385341       NA       NA 2.121784 1.755278
#> [217] 4.476669 1.140089 1.176986 1.160579 1.807966 1.540838 1.274301       NA
#> [225] 1.817720 1.738450 1.667666 1.431467 1.466275       NA 1.182816       NA
#> [233] 3.925953 2.388469       NA 1.197366 3.751310 2.377394 1.844505 1.355652
#> [241] 2.482541       NA 1.303304 1.667082 3.003822 3.732130 1.852048 1.936381
#> [249] 3.436024 1.419123 2.410589 1.448110 2.673096 1.499338 3.173717 1.725144
#> [257] 1.509464 2.917273 1.386860 1.602298 1.244567 1.660522 1.497792       NA
#> [265] 1.329995       NA       NA 2.341051 1.686081 1.714101       NA 1.704739
#> [273] 2.328877 7.296804 2.027404 1.175543 1.715160 1.363621 1.451002 1.814421
#> [281]       NA 1.587922 1.265073 1.337953 1.242313 1.161201 1.303341 1.753889
#> [289] 1.256446 2.503704 1.179438 2.572282 2.194293 1.557123 1.203649 1.477989
#> [297] 1.787285       NA 1.132171 1.314740

# Trim the scores a fitted model reports, reading the exposure off the model
ps_trim(fit, method = "cr")
#> ℹ Using exposure variable "z" from the propensity score model
#> <ps_trim; trimmed 27 of 300[300]>
#>          1          2          3          4          5          6          7 
#> 0.17801124 0.49344831 0.87258189 0.13535765 0.40258554 0.47524889 0.66839132 
#>          8          9         10         11         12         13         14 
#> 0.35061504         NA 0.38318089 0.57380442 0.74677823 0.30385529 0.15080370 
#>         15         16         17         18         19         20         21 
#>         NA         NA 0.71872070 0.44191837 0.75485924 0.57876473         NA 
#>         22         23         24         25         26         27         28 
#> 0.12443658 0.87285871         NA 0.43136394         NA 0.59392536 0.24742762 
#>         29         30         31         32         33         34         35 
#> 0.69381700 0.52982663 0.67786695 0.53996676 0.77078278 0.33667732 0.20379449 
#>         36         37         38         39         40         41         42 
#> 0.24766004 0.06402613 0.17686094 0.25726004 0.34846140 0.30654026 0.04714018 
#>         43         44         45         46         47         48         49 
#> 0.18951912         NA 0.64155648         NA 0.33008953 0.39904946 0.36838768 
#>         50         51         52         53         54         55         56 
#> 0.12461207 0.19024995         NA 0.25641497 0.81609573 0.14940217 0.04671897 
#>         57         58         59         60         61         62         63 
#> 0.32473671 0.73452714 0.78590206 0.88497697 0.05903129         NA 0.22088166 
#>         64         65         66         67         68         69         70 
#> 0.48418025 0.60360857 0.19419787 0.04471987 0.27900998 0.45856017 0.17830185 
#>         71         72         73         74         75         76         77 
#> 0.17311028 0.54393115 0.38223719 0.57963968 0.41148556 0.17594694 0.82182398 
#>         78         79         80         81         82         83         84 
#> 0.68775620 0.76492590 0.09594356 0.75050082 0.06658957 0.26414219 0.10059488 
#>         85         86         87         88         89         90         91 
#>         NA         NA 0.23301201 0.33651483 0.30554734 0.56324964 0.87450818 
#>         92         93         94         95         96         97         98 
#> 0.88631941 0.12692921 0.10234530 0.08426251 0.11660386         NA 0.43228752 
#>         99        100        101        102        103        104        105 
#> 0.18932487 0.24623842 0.77036380 0.51976065 0.32739386 0.20996243 0.18518227 
#>        106        107        108        109        110        111        112 
#>         NA 0.73562548         NA 0.29548965 0.31630208 0.15300421 0.34719807 
#>        113        114        115        116        117        118        119 
#> 0.59212123 0.83282741 0.62270671 0.58677974 0.80657336 0.78774559 0.46630644 
#>        120        121        122        123        124        125        126 
#> 0.20230064 0.80878565 0.47748117 0.89038286 0.29281504 0.14999367 0.61398482 
#>        127        128        129        130        131        132        133 
#> 0.22901364 0.64675362 0.06419209 0.06266942 0.66277576 0.54410827 0.71659793 
#>        134        135        136        137        138        139        140 
#> 0.04369801 0.80255741 0.79988215 0.75977424 0.69210372         NA 0.09079429 
#>        141        142        143        144        145        146        147 
#> 0.25092645 0.57110768 0.19704419 0.45903320 0.68008008 0.23294255 0.65254331 
#>        148        149        150        151        152        153        154 
#> 0.61803876 0.19709968 0.15848705 0.74523426 0.37316715 0.67276290 0.18894043 
#>        155        156        157        158        159        160        161 
#> 0.81642474 0.10432180 0.68582789 0.58954824 0.52232604 0.65581895 0.56727085 
#>        162        163        164        165        166        167        168 
#> 0.23684003 0.34180113 0.55405967 0.10831585 0.18065940         NA         NA 
#>        169        170        171        172        173        174        175 
#> 0.11871049 0.74905308 0.77383752 0.70770386 0.44914706 0.54166428 0.17643958 
#>        176        177        178        179        180        181        182 
#> 0.23331263 0.34344738 0.17046280 0.70230062 0.07304003 0.15246043 0.11534630 
#>        183        184        185        186        187        188        189 
#> 0.56512587 0.13518488 0.61614535 0.79451459 0.43829517 0.60656419 0.23283410 
#>        190        191        192        193        194        195        196 
#> 0.60274592 0.11390882 0.40374970 0.10403526 0.34223762 0.77357008 0.66611156 
#>        197        198        199        200        201        202        203 
#> 0.28933909 0.20113598 0.18632231 0.21070377 0.53271587 0.15441973         NA 
#>        204        205        206        207        208        209        210 
#> 0.50521454 0.16428559 0.56227253 0.38874517 0.31638831 0.63416193 0.50957963 
#>        211        212        213        214        215        216        217 
#> 0.75829404 0.26656013         NA 0.09307013 0.47742144 0.58525674 0.80304044 
#>        218        219        220        221        222        223        224 
#> 0.10676633 0.13385955 0.88554593 0.56716130 0.67071770 0.20009381         NA 
#>        225        226        227        228        229        230        231 
#> 0.45382217 0.59125501 0.39938022 0.72306882 0.30946625         NA 0.87028588 
#>        232        233        234        235        236        237        238 
#> 0.04743962 0.24158271 0.41951127 0.06438704 0.14836574 0.25419100 0.59576019 
#>        239        240        241        242        243        244        245 
#> 0.46261588 0.24969111 0.40207472         NA 0.79365463 0.61795146 0.68993549 
#>        246        247        248        249        250        251        252 
#> 0.25565132 0.46504618 0.52701568 0.28037136 0.28500140 0.60204716 0.30021180 
#>        253        254        255        256        257        258        259 
#> 0.37058188 0.32578201 0.70873738 0.59607558 0.33064721 0.33638608 0.74643214 
#>        260        261        262        263        264        265        266 
#> 0.37255079 0.82972242 0.39654734 0.32503416 0.04998908 0.23459370 0.09722939 
#>        267        268        269        270        271        272        273 
#>         NA 0.58866324 0.61063867 0.41723032         NA 0.41370803 0.43129530 
#>        274        275        276        277        278        279        280 
#> 0.12064385 0.51640377 0.87523995 0.41762629 0.75910122 0.30169925 0.45272098 
#>        281        282        283        284        285        286        287 
#>         NA 0.65014831 0.81685306 0.23932916 0.83115493 0.88511432 0.79363282 
#>        288        289        290        291        292        293        294 
#> 0.43178717 0.82223069 0.39833546 0.13562237 0.63024555 0.46028071 0.35274910 
#>        295        296        297        298        299        300 
#> 0.85629892 0.31532329 0.57414353         NA 0.10082012 0.22538288 

# Trim a dose model on the scale of its conditional density
dose <- 1 + 0.5 * x + rt(n, df = 3)
dose_fit <- lm(dose ~ x)
ps_trim(dose_fit, method = "density", lower = 0.05)
#> ℹ Using exposure variable "dose" from the propensity score model
#> <ps_trim; trimmed 15 of 300[300]>
#>   [1]  0.42939235  1.09006968  1.94693677  0.28680810  0.92816170  1.05804921
#>   [7]  1.40955162  0.83078134  2.18917407  0.89241182  1.23225111  1.57677128
#>  [13]  0.73733962  0.34220638  2.06565480 -0.43428963  1.51377446  0.99904335
#>  [19]  1.59575061  1.24117672  2.25412348  0.24433098  1.94803175  2.17096038
#>  [25]  0.98019043 -0.52018258  1.26864305  0.61283227  1.46100564          NA
#>  [31]  1.42847473  1.17197688  1.63443228  0.80362799  0.50282739  0.61338051
#>  [37] -0.07695168  0.42592949  0.63573276  0.82661942  0.74290294 -0.21932966
#>  [43]  0.46311104  2.13974897  1.35735721  2.19311121  0.79060333  0.92169244
#>  [49]  0.86469980  0.24503825  0.46519850  2.23914942  0.63378747  1.75630633
#>  [55]          NA -0.22346747  0.77992308  1.54874422  1.67294893  1.99810063
#>  [61] -0.11497789  2.21773756  0.54773574  1.07377241  1.28635301  0.47637020
#>  [67] -0.24360284  0.68445115  1.02858683  0.43026445  0.41451402  1.17899374
#>  [73]  0.89065668  1.24275403  0.94436412  0.42316534  1.77328293  1.44853829
#>  [79]          NA  0.11600668  1.58546323 -0.05849822  0.65142256  0.13907368
#>  [85] -0.37129213  2.09001943  0.57812066  0.80330827  0.74084879  1.21334515
#>  [91]  1.95459883  2.00392477  0.25429792  0.14750935  0.05332484  0.21185234
#>  [97]  2.17383418  0.98184437  0.46255518  0.61002168  1.63339100  1.13633248
#> [103]  0.78523598  0.51934498  0.45059496  2.22799665          NA  2.20396205
#> [109]  0.71982466  0.76290580          NA  0.82417245  1.26535859  1.80713482
#> [115]  1.32173555  1.25566040  1.72896844  1.67777809  1.04227851  0.49877122
#> [121]  1.73522619  1.06198124  2.02192736  0.71416468  0.33942104  1.30549651
#> [127]  0.56823119  1.36731942 -0.07573634 -0.08699855  1.39846633  1.17930747
#> [133]  1.50917138 -0.25422887  1.71774635          NA  1.60750104  1.45746761
#> [139]  2.26588287  0.08927363  0.62104937  1.22740991          NA  1.02942388
#> [145]          NA  0.57794987  1.37849621  1.31302677  0.48447213  0.36803416
#> [151]  1.57319103  0.87370106  1.41824676  0.46145377  1.75727019  0.15688221
#> [157]  1.44459956  1.26068221  1.14084917  1.38485812  1.22053498  0.58747867
#> [163]  0.81367151  1.19696789  0.17535374  0.43729835  2.24574142 -0.30490817
#> [169]  0.22076851  1.58207270  1.64206502  1.49010683  1.01190062  1.17498029
#> [175]  0.42465659  0.57885939  0.81688295  0.40633792  1.47869144 -0.01482674
#> [181]  0.34786509  0.20646291  1.21669791  0.28615913  1.30950600  1.69577790
#> [187]  0.99258284  1.29178761  0.57768316  1.28476948          NA  0.93028744
#> [193]  0.15553319  0.81452371          NA  1.40504011  0.70676339  0.49559335
#> [199]  0.45390678  0.52130612  1.15916550  0.35449252  2.73054913  1.11074987
#> [205]  0.38686049  1.21160039  0.90272781  0.76308099  1.34329166  1.11842326
#> [211]  1.60394523  0.65687238  2.07419130  0.10125236  1.06187604  1.25290208
#> [217]  1.71908694  0.16825996  0.28115746  2.00056180  1.22033889  1.41417151
#> [223]  0.49273803  2.15214956  1.02019530  1.26378323  0.92229838  1.52326968
#> [229]  0.74893483  2.35638147  1.93793228 -0.21640944  0.59892920  0.95888442
#> [235]          NA          NA  0.62864782  1.27198814  1.03575937  0.61815694
#> [241]  0.92722831  2.74124982  1.69346711  1.31286431  1.45300570  0.63202597
#> [247]  1.04005312  1.14911142  0.68742022  0.69745284  1.28348764  0.72974566
#> [253]  0.86883809  0.78201580  1.49230436  1.27256361  0.79171095  0.80305489
#> [259]  1.57596748  0.87254292  1.79740775  0.91710321          NA -0.19223190
#> [265]  0.58200010  0.12248144  2.15662179  1.25907579  1.29930313  0.95476587
#> [271] -0.68414308  0.94839325  0.98006748  0.22883164  1.13042501  1.95753594
#> [277]  0.95548128  1.60588243  0.73285216  1.01824291  2.22052580  1.37386241
#> [283]  1.75852700  0.59350771  1.80187769  1.99869379  1.69340860  0.98094845
#> [289]          NA  0.92038387  0.28780107          NA  1.03163075  0.83489383
#> [295]  1.88582027  0.76091550  1.23286043 -0.30381489  0.14016646  0.55914528
ps_trim(dose_fit, method = "resid", upper = 3, .density = dens_t(4))
#> ℹ Using exposure variable "dose" from the propensity score model
#> <ps_trim; trimmed 14 of 300[300]>
#>   [1]  0.42939235  1.09006968  1.94693677  0.28680810  0.92816170  1.05804921
#>   [7]  1.40955162  0.83078134  2.18917407  0.89241182  1.23225111  1.57677128
#>  [13]  0.73733962  0.34220638  2.06565480 -0.43428963  1.51377446  0.99904335
#>  [19]  1.59575061  1.24117672  2.25412348  0.24433098  1.94803175  2.17096038
#>  [25]  0.98019043 -0.52018258  1.26864305  0.61283227  1.46100564          NA
#>  [31]  1.42847473  1.17197688  1.63443228  0.80362799  0.50282739  0.61338051
#>  [37] -0.07695168  0.42592949  0.63573276  0.82661942  0.74290294 -0.21932966
#>  [43]  0.46311104  2.13974897  1.35735721  2.19311121  0.79060333  0.92169244
#>  [49]  0.86469980  0.24503825  0.46519850  2.23914942  0.63378747  1.75630633
#>  [55]          NA -0.22346747  0.77992308  1.54874422  1.67294893  1.99810063
#>  [61] -0.11497789  2.21773756  0.54773574  1.07377241  1.28635301  0.47637020
#>  [67] -0.24360284  0.68445115  1.02858683  0.43026445  0.41451402  1.17899374
#>  [73]  0.89065668  1.24275403  0.94436412  0.42316534  1.77328293  1.44853829
#>  [79]          NA  0.11600668  1.58546323 -0.05849822  0.65142256  0.13907368
#>  [85] -0.37129213  2.09001943  0.57812066  0.80330827  0.74084879  1.21334515
#>  [91]  1.95459883  2.00392477  0.25429792  0.14750935  0.05332484  0.21185234
#>  [97]  2.17383418  0.98184437  0.46255518  0.61002168  1.63339100  1.13633248
#> [103]  0.78523598  0.51934498  0.45059496  2.22799665          NA  2.20396205
#> [109]  0.71982466  0.76290580          NA  0.82417245  1.26535859  1.80713482
#> [115]  1.32173555  1.25566040  1.72896844  1.67777809  1.04227851  0.49877122
#> [121]  1.73522619  1.06198124  2.02192736  0.71416468  0.33942104  1.30549651
#> [127]  0.56823119  1.36731942 -0.07573634 -0.08699855  1.39846633  1.17930747
#> [133]  1.50917138 -0.25422887  1.71774635          NA  1.60750104  1.45746761
#> [139]  2.26588287  0.08927363  0.62104937  1.22740991          NA  1.02942388
#> [145]          NA  0.57794987  1.37849621  1.31302677  0.48447213  0.36803416
#> [151]  1.57319103  0.87370106  1.41824676  0.46145377  1.75727019  0.15688221
#> [157]  1.44459956  1.26068221  1.14084917  1.38485812  1.22053498  0.58747867
#> [163]  0.81367151  1.19696789  0.17535374  0.43729835  2.24574142 -0.30490817
#> [169]  0.22076851  1.58207270  1.64206502  1.49010683  1.01190062  1.17498029
#> [175]  0.42465659  0.57885939  0.81688295  0.40633792  1.47869144 -0.01482674
#> [181]  0.34786509  0.20646291  1.21669791  0.28615913  1.30950600  1.69577790
#> [187]  0.99258284  1.29178761  0.57768316  1.28476948          NA  0.93028744
#> [193]  0.15553319  0.81452371          NA  1.40504011  0.70676339  0.49559335
#> [199]  0.45390678  0.52130612  1.15916550  0.35449252  2.73054913  1.11074987
#> [205]  0.38686049  1.21160039  0.90272781  0.76308099  1.34329166  1.11842326
#> [211]  1.60394523  0.65687238  2.07419130  0.10125236  1.06187604  1.25290208
#> [217]  1.71908694  0.16825996  0.28115746  2.00056180  1.22033889  1.41417151
#> [223]  0.49273803  2.15214956  1.02019530  1.26378323  0.92229838  1.52326968
#> [229]  0.74893483  2.35638147  1.93793228 -0.21640944  0.59892920  0.95888442
#> [235] -0.07431245          NA  0.62864782  1.27198814  1.03575937  0.61815694
#> [241]  0.92722831  2.74124982  1.69346711  1.31286431  1.45300570  0.63202597
#> [247]  1.04005312  1.14911142  0.68742022  0.69745284  1.28348764  0.72974566
#> [253]  0.86883809  0.78201580  1.49230436  1.27256361  0.79171095  0.80305489
#> [259]  1.57596748  0.87254292  1.79740775  0.91710321          NA -0.19223190
#> [265]  0.58200010  0.12248144  2.15662179  1.25907579  1.29930313  0.95476587
#> [271] -0.68414308  0.94839325  0.98006748  0.22883164  1.13042501  1.95753594
#> [277]  0.95548128  1.60588243  0.73285216  1.01824291  2.22052580  1.37386241
#> [283]  1.75852700  0.59350771  1.80187769  1.99869379  1.69340860  0.98094845
#> [289]          NA  0.92038387  0.28780107          NA  1.03163075  0.83489383
#> [295]  1.88582027  0.76091550  1.23286043 -0.30381489  0.14016646  0.55914528

if (rlang::is_installed("nnet")) {
  trt <- factor(sample(c("a", "b", "c"), n, replace = TRUE))
  multinomial_fit <- nnet::multinom(trt ~ x, trace = FALSE)
  ps_trim(multinomial_fit, method = "optimal")
}
#> ℹ Using exposure variable "trt" from the propensity score model
#> <ps_trim_matrix[300 x 3]; trimmed 0 of 300; method=optimal>
#>            a         b         c
#> 1  0.2833559 0.3409328 0.3757113
#> 2  0.2986578 0.3516829 0.3496593
#> 3  0.3183352 0.3645221 0.3171427
#> 4  0.2800465 0.3385216 0.3814319
#> 5  0.2949150 0.3491145 0.3559705
#> 6  0.2979181 0.3511784 0.3509035
#> 7  0.3060233 0.3566205 0.3373562
#> 8  0.2926613 0.3475487 0.3597900
#> 9  0.3238455 0.3679135 0.3082410
#> 10 0.2940878 0.3485415 0.3573707
#> # ... with 290 more rows