Im trying to calculate weighted confidence intervals and need to use the a weighted mean to do so. But I keep running into the same failure that I cant figure out how to solve. The data is based on the European Social Survey and I have loaded the following libraries:
library(tidyverse)
library(haven) 
library(essurvey) 
library(radiant.data) 
The following code should output, among others, confidence intervals:
ESS %>% # Use the ESS, then
  transmute( # Create new variables and only keep these new ones
    # Make the following variables factors:
    cntry = as_factor(cntry), 
    # Make the following variables numeric:
    pspwght = zap_labels(pspwght),
    hmsacld = max(zap_labels(hmsacld), na.rm = TRUE) - zap_labels(hmsacld), #Turning scale around
  ) %>%
  group_by(cntry) %>% # Group data by country, then
  summarize(
    n = sum(pspwght, na.rm = TRUE),
    mean_hmsacld = weighted.mean(hmsacld, pspwght, na.rm = TRUE), 
    sd_hmsacld = weighted.sd(hmsacld, pspwght), 
    se_hmsacld = sd_hmsacld / sqrt(n),
    min95 = mean_hmsacld - se_hmsacld * qt(p = 0.975, df = n),
    max95 = mean_hmsacld + se_hmsacld * qt(p = 0.975, df = n)
  )
Instead, I get the following error:
Error in weighted.mean.default(x, wt) : 
  'x' and 'w' must have the same length
Any idea how to fix this?
Thanks
 
    