First time posting - and very new user of R Studio here. I've made a reprex below of an error i keep getting.
I am trying to rake survey data to make it representative of UK population data. I have a set of responses and have found distribution data to make it representative. The raking procedure I am using is anesrake , similar to this: R anesrake error: “Error in x + weights: non-numeric argument for binary operator” Ask Question.
I have 4 columns of data with 30 rows: caseid, gender (M,F); age (18-24, 25-34, 35-44, 45-54, 55-64, 65-74, 75) and Answer (1,0). My data is saved on shared drive, havent included in the reprex.
I have loaded target distributions for age and gender, but continue to get the following error: "Error in targetvec - dat: non-numeric argument to binary operator"
I have attempted to include a reprex below.
Any assistance would really help!
Thanks
Ed
library(readxl)
library(weights)
library(anesrake)
library(plyr)
library(dplyr)
library(reshape2)
library(reprex)
rtest <- read_excel(data is not shared)
View(rtest)
# UK Census 2019
gender <- c(.51,.49)
age <- c(0.1,.17,.163,0.166,0.161,0.128,0.112)
# definitions of target list
targets <- list(gender, age)
#important to use same variable names of the dataset
names(targets) <- c("gender", "age")
#label levels of targets#
names(targets$gender) <- levels(rtest$gender)
names(targets$age) <- levels(rtest$age)
# change table type
rtest <- as.data.frame(rtest)
class(rtest)
#> [1] "data.frame"
#measure variance in population vs sample
anesrakefinder(targets, rtest, choosemethod = "total")
#> gender age
#> 0.1533333 0.4053333
#convert to factor (unsure if this works)
rtest$gender <- as.factor(rtest$gender)
rtest$age <- as.factor(rtest$age)
#raking procedure
raking <- anesrake(targets, rtest, rtest$caseid, cap = 5,choosemethod = "total", type = "pctlim",pctlim = 0.05)
#> Error in targetvec - dat: non-numeric argument to binary operator
Created on 2021-02-05 by the reprex package (v1.0.0)