R anesrake error: “Error in x + weights: non-numeric argument for binary operator” Ask Question

Hi, and welcome!

A reproducible example, called a reprex is a great way to attract more and better answers. Using the example in the documentation, here's what one looks like.

library(anesrake)
#> Loading required package: Hmisc
#> Loading required package: lattice
#> Loading required package: survival
#> Loading required package: Formula
#> Loading required package: ggplot2
#> 
#> Attaching package: 'Hmisc'
#> The following objects are masked from 'package:base':
#> 
#>     format.pval, units
#> Loading required package: weights
#> Loading required package: gdata
#> gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.
#> 
#> gdata: read.xls support for 'XLSX' (Excel 2007+) files ENABLED.
#> 
#> Attaching package: 'gdata'
#> The following object is masked from 'package:stats':
#> 
#>     nobs
#> The following object is masked from 'package:utils':
#> 
#>     object.size
#> The following object is masked from 'package:base':
#> 
#>     startsWith
#> Loading required package: mice
#> 
#> Attaching package: 'mice'
#> The following objects are masked from 'package:base':
#> 
#>     cbind, rbind
data("anes04")

anes04$caseid <- 1:length(anes04$age)

anes04$agecats <- cut(anes04$age, c(0, 25,35,45,55,65,99))
levels(anes04$agecats) <- c("age1824", "age2534", "age3544",
          "age4554", "age5564", "age6599")

marriedtarget <- c(.4, .6)

agetarg <- c(.10, .15, .17, .23, .22, .13)
names(agetarg) <- c("age1824", "age2534", "age3544",
          "age4554", "age5564", "age6599")

targets <- list(marriedtarget, agetarg)

names(targets) <- c("married", "agecats")

outsave <- anesrake(targets, anes04, caseid=anes04$caseid,
          verbose=TRUE)
#> [1] "Raking...Iteration 1"
#> [1] "Current iteration changed total weights by 303.931933308375"
#> [1] "Raking...Iteration 2"
#> [1] "Current iteration changed total weights by 50.8760710934304"
#> [1] "Raking...Iteration 3"
#> [1] "Current iteration changed total weights by 2.45181318049929"
#> [1] "Raking...Iteration 4"
#> [1] "Current iteration changed total weights by 0.115529921587497"
#> [1] "Raking...Iteration 5"
#> [1] "Current iteration changed total weights by 0.00543798477855262"
#> [1] "Raking...Iteration 6"
#> [1] "Current iteration changed total weights by 0.000255952672060356"
#> [1] "Raking...Iteration 7"
#> [1] "Current iteration changed total weights by 1.20470391882233e-05"
#> [1] "Raking...Iteration 8"
#> [1] "Current iteration changed total weights by 5.67023318742699e-07"
#> [1] "Raking...Iteration 9"
#> [1] "Current iteration changed total weights by 2.66883061206258e-08"
#> [1] "Raking...Iteration 10"
#> [1] "Current iteration changed total weights by 1.25607590995003e-09"
#> [1] "Raking...Iteration 11"
#> [1] "Current iteration changed total weights by 5.92315085867767e-11"
#> [1] "Raking...Iteration 12"
#> [1] "Current iteration changed total weights by 2.76700884427328e-12"
#> [1] "Raking...Iteration 13"
#> [1] "Current iteration changed total weights by 2.07389660999979e-13"
#> [1] "Raking...Iteration 14"
#> [1] "Current iteration changed total weights by 1.23678844943242e-13"
#> [1] "Raking...Iteration 15"
#> [1] "Current iteration changed total weights by 1.2356782264078e-13"
#> [1] "Raking converged in 15 iterations"

caseweights <- data.frame(cases=outsave$caseid, weights=outsave$weightvec)

summary(caseweights)
#>      cases           weights      
#>  Min.   :   1.0   Min.   :0.4665  
#>  1st Qu.: 303.8   1st Qu.:0.6956  
#>  Median : 606.5   Median :0.8864  
#>  Mean   : 606.5   Mean   :1.0000  
#>  3rd Qu.: 909.2   3rd Qu.:1.1874  
#>  Max.   :1212.0   Max.   :1.7870

summary(outsave)
#> $convergence
#> [1] "Complete convergence was achieved after 15 iterations"
#> 
#> $base.weights
#> [1] "No Base Weights Were Used"
#> 
#> $raking.variables
#> [1] "married" "agecats"
#> 
#> $weight.summary
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  0.4665  0.6956  0.8864  1.0000  1.1874  1.7870 
#> 
#> $selection.method
#> [1] "variable selection conducted using _pctlim_ - discrepancies selected using _total_."
#> 
#> $general.design.effect
#> [1] 1.128633
#> 
#> $married
#>       Target Unweighted N Unweighted %     Wtd N Wtd % Change in % Resid. Disc.
#> FALSE    0.6          563     0.464905  726.6693   0.6   0.1350950 0.000000e+00
#> TRUE     0.4          648     0.535095  484.4462   0.4  -0.1350950 5.551115e-17
#> Total    1.0         1211     1.000000 1211.1155   1.0   0.2701899 5.551115e-17
#>       Orig. Disc.
#> FALSE   0.1350950
#> TRUE   -0.1350950
#> Total   0.2701899
#> 
#> $agecats
#>         Target Unweighted N Unweighted %   Wtd N Wtd %  Change in %
#> age1824   0.10          150    0.1237624  121.20  0.10 -0.023762376
#> age2534   0.15          205    0.1691419  181.80  0.15 -0.019141914
#> age3544   0.17          217    0.1790429  206.04  0.17 -0.009042904
#> age4554   0.23          237    0.1955446  278.76  0.23  0.034455446
#> age5564   0.22          216    0.1782178  266.64  0.22  0.041782178
#> age6599   0.13          187    0.1542904  157.56  0.13 -0.024290429
#> Total     1.00         1212    1.0000000 1212.00  1.00  0.152475248
#>         Resid. Disc.  Orig. Disc.
#> age1824 0.000000e+00 -0.023762376
#> age2534 0.000000e+00 -0.019141914
#> age3544 0.000000e+00 -0.009042904
#> age4554 2.775558e-17  0.034455446
#> age5564 5.551115e-17  0.041782178
#> age6599 0.000000e+00 -0.024290429
#> Total   8.326673e-17  0.152475248

Created on 2020-01-17 by the reprex package (v0.3.0)

The difficulty in resolving the code you're having trouble with is that data is not specified. (It doesn't have to be all your data or even your data at all, just representative; often it's possible to convert a built-in data set to meet the need.) Another aside: data is also the name of a built-in function. It's good practice to use dat or some other non-reserved term.

According to the documentation the weightsvector argument is optional and

1 + NULL
numeric(0)

so that leaves x. Are you sure data$caseID is numeric?