bschneidr / fastsurvey

A fork of the `survey` R package, using {Rcpp}
8 stars 0 forks source link

Differences in numeric precision #5

Closed bschneidr closed 1 year ago

bschneidr commented 1 year ago

Need to check whether these small differences are avoidable within arma_onestage() and arma_multistage(), or if they instead need to be sandpapered over in multistage_rcpp().

Instead of returning a true zero, some of the variances (!!) are tiny, tiny negative numbers, which results in NaNs when using SE(). This has to be avoided.

library(survey)

 dstrat<-svydesign(id=~1,strata=~stype, weights=~pw,
        data=apistrat, fpc=~fpc)

multistage(x, clusters,stratas,fpcs$sampsize, fpcs$popsize,
            lonely.psu=getOption("survey.lonely.psu"),
            one.stage=one.stage,stage=1,cal=cal)
#>       stypeE stypeH stypeM
#> stypeE      0      0      0
#> stypeH      0      0      0
#> stypeM      0      0      0
multistage_rcpp(x, clusters,stratas,fpcs$sampsize, fpcs$popsize,
                 lonely.psu=getOption("survey.lonely.psu"),
                 one.stage=one.stage,stage=1,cal=cal)
#>              stypeE        stypeH        stypeM
#> stypeE 6.498285e-19  1.723051e-19  1.258938e-18
#> stypeH 1.723051e-19 -5.484499e-19 -3.625601e-19
#> stypeM 1.258938e-18 -3.625601e-19 -1.224819e-18

.Machine$double.eps
[1] 2.220446e-16

rcpp_result < .Machine$double.eps
#>        stypeE stypeH stypeM
#> stypeE   TRUE   TRUE   TRUE
#> stypeH   TRUE   TRUE   TRUE
#> stypeM   TRUE   TRUE   TRUE