Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
Created August 5, 2020 15:10
Show Gist options
  • Save explodecomputer/1466aa7713e6c24d37a0553f373a29b7 to your computer and use it in GitHub Desktop.
Save explodecomputer/1466aa7713e6c24d37a0553f373a29b7 to your computer and use it in GitHub Desktop.
weighted mean
# Number of individuals in the population
npop <- 100000
# Distribution of variable in the population
x <- rnorm(npop)
# Individuals selected into sample with high x value
s <- rbinom(npop, 1, plogis(x * 0.4))
# Population mean of x
mean(x)
# Mean of x in selected sample
mean(x[s==1])
# Mean of x in unselected sample
mean(x[s==0])
# Estimate probability of being selected into sample
# Ordinarily might use a range of different variables to construct a predictor
mod <- glm(s ~ x, family="binomial")
# Get probabilities of being selected into sample
f <- fitted.values(mod)
# Generate weights
weight <- 1/f[s==1]
# Estimate mean of x in population by weighting the sample
sum(x[s==1] * weight) / sum(weight)
# Compare to population mean
mean(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment