I am trying to calculate the quantile (0 to 100) for each observation for a continuous variable (let's call it 'value') within each group when I have sampling weights and assign each observation to its respective quantile in a new variable.
In other words, each row is an observation and each observation belongs to a single group. All of the groups have more than 2 observations. Within each group, I need to estimate the distribution of value using the sampling weights in my data, determine at which percentile an observation falls within its group's distribution, then add that percentile as a column to the data frame.
As far as I can tell, the survey
package has svyby()
and svyquantile()
but the latter returns values for the specified quantiles rather than the quantile of a value for a given observation.
# Load survey package
library(survey)
# Set seed for replication
set.seed(123)
# Create data with value, group, weight
dat <- data.frame(value = 1:6,
group = rep(1:3,2),
weight = abs(rnorm(6))
# Declare survey design
d <- survey::svydesign(id =~1, data = dat, weights = weight)
# Do something to calculate the quantile and add it to the data
????
This is similar to this question but is not done by subgroup: Compute quantiles incorporating Sample Design (Survey package)