1

I'm currently working with public use microdata that requires survey weighting, thus I've gotten fairly familiar with the survey package and srvyr for summary statistics.

I am trying to figure out a way to create an indicator variable for each observation in a survey object datatable which corresponds to that observation's quantile when using the quantile function. For instance, I might want to create a dummy for each observation according to the observation's quantile when calculating for "height".

I found an example of what I'm trying to do here:

Compute quantiles incorporating Sample Design (Survey package)

When following the steps provided in the answer,

data(api)

dclus1 <- svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
data(api)
a <- svyquantile(~api00, design = dclus1, quantiles = seq(0, 1, by=0.1), method = "linear", ties="rounded")

dclus1 <- update( dclus1 , qtile = factor( findInterval( api00 , a ) ) )

I get this returned :

Error in findInterval(api00, a) : 
  'list' object cannot be coerced to type 'double'

I cannot seem to get findInterval to work on the survey quantile list object generated. From documentation for findInterval, it seems it wants a numeric object or vector. Is there a way to ammend this answer provided to get what I need? Am I missing something ?

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
elemn
  • 11
  • 1
  • 1
    Without sample data it's difficult to help you, but why are you working with list columns? Is there any way to unnest said columns so your code runs? – Juan C Dec 21 '22 at 20:28
  • @elemn when i put `library(survey)` at the top of your script and `svymean(~qtile,dclus1)` at the bottom, it runs without error. would it be possible for you to edit your question so it shows each step exactly on a fresh console? thanks! – Anthony Damico Dec 22 '22 at 13:57

1 Answers1

0

Further to @juan-c 's cmomment, I decided to pull out the quantile information I calculated via

j = a$[1:4]

In order to do :

dclus1 <- update( dclus1 , qtile = factor( findInterval( api00 , j) ) )

Which solved my problem.

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
elemn
  • 11
  • 1