"Error in sum(x) : invalid 'type' (character) of argument" when trying to create raking weights

74 Views Asked by At

I am trying to create weights for a dataset using sex, region, age and race based on total population information. I run into an error that states: Error in sum(x) : invalid 'type' (character) of argument when I use the anesrake package.

My code is as follows:

#pop distribution 2022 ACS for creating weight targets
#northeast 17.1% midwest 20.6 south 38.6 west 23.6
region <- c(0.206, 0.171, 0.386, 0.236)
#sex 49.6 Male 50.4 female
sex <- c(.504, .496)
#race latinos is 19.1, non hispanic black is 11.9 and white is 57.7 other is 11.3
racecommon <- c(.191,.119,.577, .113)
#agecat -- 10-14, 15-17, 18-19, 20, 21, 22-24, 25-29, other
agecat <- c(.064, .039, .026, .014, .014,.04,.066, .737)
df$caseid <- 1:length(df$sex) #making an ID variable for study participants
df$caseid <- as.numeric(df$caseid)


df2 <- df %>% select(caseid, sex, region, agecat, racecommon)
df2 <- na.omit(df2)
targets <- list("region", "sex", "racecommon","agecat")
anesrakefinder(targets, df2, choosemethod = "total")
sapply(df2, class)
outsave <- anesrake(targets, df2, caseid = df2$caseid,
                    verbose= FALSE, cap = 5, choosemethod = "total",
                    type = "pctlim", pctlim = .05 , nlim = 5,
                    iterate = TRUE , force1 = TRUE)

This is what my data looks like in general:

caseid sex region agecat racecommon
1 1 2 2 1
2 1 1 1 1
3 2 4 2 1
4 2 3 2 3
5 2 3 2 3
6 2 1 2 3

In my survey there are no individuals over the age of 29 (this survey is meant for younger people). Could the lack of people that fit into the "other" category prevent this code from running?

In previous code I made sure to make all of my variables numeric and exclude any NAs and I still get this character error.

0

There are 0 best solutions below