R Under development (unstable) (2024-09-15 r87152 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # install.packages("sampling") # Install "sampling" if it is unavailable. > library(GECal) > > ############ > ## Example 1 > ############ > # matrix of sample calibration variables > Xs=cbind( + c(1,1,1,1,1,1,1,1,1,1), + c(1,1,1,1,1,0,0,0,0,0), + c(1,2,3,4,5,6,7,8,9,10) + ) > # inclusion probabilities > piks=rep(0.2,times=10); d=1/piks > # vector of population totals > total=c(50,24,290) > > # Calibration weights > sampling::calib(Xs, total, d = d, method="raking") * d [1] 3.887155 4.297846 4.751928 5.253985 5.809086 4.211085 4.656000 5.147922 [9] 5.691817 6.293176 > calibration <- GEcalib(~ 0 + Xs, dweight = d, const = total, method = "DS", entropy = "ET") > calibration$w 1 2 3 4 5 6 7 8 3.887155 4.297846 4.751928 5.253985 5.809086 4.211085 4.656000 5.147922 9 10 5.691817 6.293176 > > sampling::calib(Xs, total, d = d, method="linear") * d [1] 3.8 4.3 4.8 5.3 5.8 4.2 4.7 5.2 5.7 6.2 > calibration <- GEcalib(~ 0 + Xs, dweight = d, const = total, method = "DS", entropy = "SL") > calibration$w 1 2 3 4 5 6 7 8 9 10 3.8 4.3 4.8 5.3 5.8 4.2 4.7 5.2 5.7 6.2 > > ############ > ## Example 2 > ############ > # Example of g-weights (linear, raking, truncated, logit), > # with the data of Belgian municipalities as population. > # Firstly, a sample is selected by means of Poisson sampling. > # Secondly, the g-weights are calculated. > data(belgianmunicipalities, package = "sampling") > attach(belgianmunicipalities) > # matrix of calibration variables for the population > X=cbind(1, + Men03/mean(Men03), + Women03/mean(Women03), + Diffmen, + Diffwom, + TaxableIncome/mean(TaxableIncome), + Totaltaxation/mean(Totaltaxation), + medianincome/mean(medianincome), + averageincome/mean(averageincome)) > # selection of a sample with expectation size equal to 200 > # by means of Poisson sampling > # the inclusion probabilities are proportional to the average income > pik=sampling::inclusionprobabilities(averageincome,200) > N=length(pik) # population size > s=sampling::UPpoisson(pik) # sample > Xs=X[s==1,] # sample matrix of calibration variables > piks=pik[s==1]; d = 1 / piks# sample inclusion probabilities > n=length(piks) # expected sample size > # vector of population totals of the calibration variables > total=c(t(rep(1,times=N))%*%X) > # computation of the g-weights > # by means of different calibration methods > > all.equal(sampling::calib(Xs, total, d = d, method="linear") * d, + unname(GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "SL")$w)) [1] TRUE > GEcalib(~ 0 + Xs + g(d), dweight = d, const = c(total, sum(g(1 / pik, 1))), + method = "GEC", entropy = "SL")$w 1 2 3 4 5 6 7 2.11077357 2.23971615 2.41086400 2.75641432 2.59587630 2.85003306 2.22679788 8 9 10 11 12 13 14 2.13101414 2.50448942 2.82787737 2.52928058 2.38305132 2.34251432 3.72005959 15 16 17 18 19 20 21 3.30787349 2.61200474 3.13561713 3.56446481 2.47723537 2.94720711 2.23330985 22 23 24 25 26 27 28 2.19838543 7.69711680 3.00921769 6.71186864 3.31858873 2.58360354 2.56087817 29 30 31 32 33 34 35 2.04255428 0.05566034 2.70484635 2.55388564 1.80860275 2.85573057 2.29869418 36 37 38 39 40 41 42 1.71367233 2.04831886 2.39448869 2.40296697 2.37103358 2.69911807 2.86345384 43 44 45 46 47 48 49 2.26380812 2.24906389 1.36035913 1.22269826 2.68080719 1.45420367 1.88326755 50 51 52 53 54 55 56 3.43065846 2.34302143 3.51529018 2.12645572 1.64715701 3.13456366 1.84555896 57 58 59 60 61 62 63 2.02600129 2.28595566 0.96384816 2.81948037 2.01410110 2.82230960 1.81080356 64 65 66 67 68 69 70 1.12592749 3.02281027 2.12637720 3.08968297 2.99717218 3.15611050 2.31564269 71 72 73 74 75 76 77 2.75296543 3.46640769 2.69650113 5.55334664 2.14983775 2.45250113 4.45032628 78 79 80 81 82 83 84 4.44536579 2.09699862 3.25208595 2.55743957 2.52470340 3.70909215 2.98159120 85 86 87 88 89 90 91 2.87183242 3.10691370 3.45603814 3.16685901 2.58090754 3.53754328 3.30658948 92 93 94 95 96 97 98 2.79218590 3.20865960 2.48762419 2.74871606 3.74272299 3.13059410 3.30148887 99 100 101 102 103 104 105 2.85397425 2.80539181 4.16710327 3.08769237 2.79201232 2.99949240 1.59955425 106 107 108 109 110 111 112 3.44380533 3.67576753 2.59335111 2.35674963 2.56459129 2.61666286 3.02374912 113 114 115 116 117 118 119 3.36459099 3.54173262 3.61148848 2.73986317 2.65132542 3.06069924 2.49890133 120 121 122 123 124 125 126 3.07155205 4.11446930 2.88188972 2.70570356 2.30285180 3.01223517 2.91690313 127 128 129 130 131 132 133 2.54669280 6.12157324 2.08164945 2.92337931 2.91464182 3.13127167 3.37171218 134 135 136 137 138 139 140 3.10557780 3.07113326 2.39938676 2.33747889 2.73478716 2.27050211 2.27996838 141 142 143 144 145 146 147 2.80162190 5.10272379 2.52925329 4.46887406 4.54948145 2.72577061 2.88172823 148 149 150 151 152 153 154 3.03287374 3.12233110 2.52123779 2.47993558 2.70430056 2.56364016 3.20446760 155 156 157 158 159 160 161 2.48930602 3.02475866 2.46952448 2.86373371 2.70685169 2.37151211 2.61428544 162 163 164 165 166 167 168 4.06162099 2.69845917 2.30486491 2.76938017 2.28170216 2.99738250 3.89040805 169 170 171 172 173 174 175 3.36953732 2.22815651 4.50034763 3.73623851 2.91108299 4.17749117 3.36307310 176 177 178 179 180 181 182 2.69406651 2.76403346 2.60573319 2.13965046 2.27147212 2.62578746 2.65132382 183 184 185 186 187 188 189 2.41023428 2.11002564 3.04473285 2.72277678 2.38327853 2.34727365 2.82775625 190 191 192 193 194 195 196 2.70559759 2.37623131 2.60503863 2.52468387 2.74032966 2.51925702 2.53595803 197 198 199 200 201 202 203 2.36443143 2.64162705 2.53458063 3.61644558 2.15321660 3.03972708 3.18585325 204 205 206 207 208 209 2.96121042 2.24829659 2.06350585 2.49479910 3.32537296 2.84374263 > > all.equal(sampling::calib(Xs, total, d = d, method="raking") * d, + unname(GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "ET")$w)) [1] "Mean relative difference: 6.780519e-08" > GEcalib(~ 0 + Xs + g(d), dweight = d, const = c(total, sum(g(1 / pik, 0))), + method = "GEC", entropy = "ET")$w 1 2 3 4 5 6 7 8 2.147399 2.326925 2.415131 2.729294 2.618177 2.840622 2.310810 2.219172 9 10 11 12 13 14 15 16 2.491414 2.710234 2.534716 2.432233 2.415850 3.578843 3.187659 2.640381 17 18 19 20 21 22 23 24 3.136234 3.532865 2.550230 2.923166 2.250281 2.339205 7.977042 2.810001 25 26 27 28 29 30 31 32 7.773174 2.952167 2.189704 2.294121 2.192650 1.109778 2.593006 2.522299 33 34 35 36 37 38 39 40 1.878183 2.712315 2.096699 1.834261 2.148906 2.243030 2.379160 2.402896 41 42 43 44 45 46 47 48 2.527543 2.730228 2.176701 2.155057 1.542825 1.572420 2.646078 1.649939 49 50 51 52 53 54 55 56 1.772304 3.447659 2.365413 3.540415 2.107275 1.810904 3.037051 1.914775 57 58 59 60 61 62 63 64 2.142774 2.307944 1.327618 2.573230 2.087228 2.463917 1.888881 1.211499 65 66 67 68 69 70 71 72 2.703488 2.126918 3.126850 2.963346 3.065952 2.385744 2.759066 3.405031 73 74 75 76 77 78 79 80 2.745984 6.395309 2.319945 2.443967 4.736999 4.562022 2.191251 3.052306 81 82 83 84 85 86 87 88 2.559820 2.559897 3.609393 2.937070 2.842148 3.156347 3.423876 3.080963 89 90 91 92 93 94 95 96 2.547708 3.461607 3.266018 2.757775 3.096264 2.519616 2.651660 3.616145 97 98 99 100 101 102 103 104 3.114037 3.287987 2.737643 2.751123 4.030923 3.078553 2.725236 2.842743 105 106 107 108 109 110 111 112 1.594307 3.423871 3.703767 2.540263 2.393137 2.571787 2.603450 3.031379 113 114 115 116 117 118 119 120 3.090427 3.460341 3.598580 2.755242 2.693378 2.894199 2.592848 3.076613 121 122 123 124 125 126 127 128 4.022578 2.870299 2.589127 2.395377 2.972991 2.939480 2.560435 6.477379 129 130 131 132 133 134 135 136 2.143731 2.758756 2.727656 2.969207 3.265602 3.087025 3.055302 2.479238 137 138 139 140 141 142 143 144 2.412383 2.702604 2.195175 2.310847 2.721105 5.386276 2.484758 4.068886 145 146 147 148 149 150 151 152 4.709256 2.793931 2.864832 2.837065 2.987668 2.477196 2.556714 2.715887 153 154 155 156 157 158 159 160 2.568659 3.029858 2.354024 2.915300 2.557641 2.766656 2.734299 2.463553 161 162 163 164 165 166 167 168 2.601189 3.855978 2.742650 2.440362 2.824893 2.355764 2.878178 3.915435 169 170 171 172 173 174 175 176 3.247556 2.405931 4.603351 3.735923 2.881798 3.777081 3.223373 2.727767 177 178 179 180 181 182 183 184 2.775662 2.666603 2.217101 2.380130 2.675688 2.625385 2.512274 2.256533 185 186 187 188 189 190 191 192 3.005042 2.709854 2.456961 2.438973 2.733272 2.593343 2.444821 2.663209 193 194 195 196 197 198 199 200 2.509313 2.798196 2.542716 2.568989 2.437799 2.673343 2.584315 3.509451 201 202 203 204 205 206 207 208 2.211701 2.710739 3.074930 2.927266 2.278655 2.239974 2.463290 3.300428 209 2.765828 > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "EL")$w 1 2 3 4 5 6 7 8 2.436622 2.302042 2.528494 2.659776 2.577687 2.902929 2.355829 2.236443 9 10 11 12 13 14 15 16 2.469255 2.639500 2.349338 2.383001 2.377305 3.257933 3.256118 2.650944 17 18 19 20 21 22 23 24 3.042300 3.380067 2.596958 2.797541 2.275112 2.471283 7.572750 2.483092 25 26 27 28 29 30 31 32 8.697491 3.394243 2.008982 2.169711 2.214137 1.439833 2.720288 2.448801 33 34 35 36 37 38 39 40 2.156366 2.689153 2.085159 2.016291 2.114136 2.109607 2.410540 2.355147 41 42 43 44 45 46 47 48 2.608851 2.655109 2.354014 2.230666 1.713820 1.833689 2.606647 1.975906 49 50 51 52 53 54 55 56 1.888032 3.250533 2.297759 3.565893 2.088204 1.845599 2.730289 1.862725 57 58 59 60 61 62 63 64 2.023414 2.160364 1.526967 2.648078 2.050057 2.421432 1.954746 1.520211 65 66 67 68 69 70 71 72 2.534960 2.081028 3.056857 2.812308 2.908302 2.351498 2.684773 3.267610 73 74 75 76 77 78 79 80 2.698540 7.367999 2.827338 2.559434 4.846214 4.836988 2.312902 2.977273 81 82 83 84 85 86 87 88 2.554604 2.530218 3.538934 2.903005 2.756901 3.080543 3.367997 3.095545 89 90 91 92 93 94 95 96 2.570088 3.336243 3.145635 2.707248 2.973934 2.435323 2.420340 3.462347 97 98 99 100 101 102 103 104 3.019242 3.197800 2.687258 2.724124 4.032138 3.018822 2.706387 2.770450 105 106 107 108 109 110 111 112 1.834470 3.230698 3.583420 2.418604 2.369858 2.571662 2.553905 3.009049 113 114 115 116 117 118 119 120 2.928261 3.224657 3.465871 2.664935 2.587096 2.926060 2.445314 2.711051 121 122 123 124 125 126 127 128 3.952553 2.860247 2.480152 2.394794 3.017159 2.584186 2.395440 7.108949 129 130 131 132 133 134 135 136 2.049401 2.668705 2.362063 3.049379 3.414891 2.946740 3.075308 2.480214 137 138 139 140 141 142 143 144 2.446519 2.610355 2.227518 2.367367 2.538118 5.485959 2.490377 3.996506 145 146 147 148 149 150 151 152 4.274368 2.682397 3.094691 3.031868 2.807319 2.530508 2.678087 2.798241 153 154 155 156 157 158 159 160 2.649499 2.867302 2.432652 2.882980 2.514989 2.837781 2.724600 2.431953 161 162 163 164 165 166 167 168 2.505686 3.832602 2.625166 2.629301 2.818331 2.348151 2.892745 3.839785 169 170 171 172 173 174 175 176 3.130323 2.430407 4.618720 3.550893 2.821772 3.693237 3.045147 2.761389 177 178 179 180 181 182 183 184 2.789071 2.704444 2.699676 2.811471 2.671869 2.775399 2.593027 2.346051 185 186 187 188 189 190 191 192 3.073614 2.724814 2.592932 2.520415 2.821457 2.591410 2.522810 2.631118 193 194 195 196 197 198 199 200 2.614554 3.027018 2.718921 2.600629 2.506852 2.692118 2.629760 3.454074 201 202 203 204 205 206 207 208 2.244506 2.628304 2.918941 2.761888 2.280234 2.265030 2.338726 3.263655 209 2.870145 > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "HD")$w 1 2 3 4 5 6 7 8 2.427553 2.290282 2.476248 2.679756 2.556101 2.928499 2.308618 2.219571 9 10 11 12 13 14 15 16 2.473226 2.662535 2.345186 2.360422 2.359167 3.373475 3.297621 2.637231 17 18 19 20 21 22 23 24 3.071493 3.422881 2.572664 2.805055 2.251726 2.424875 7.781505 2.555089 25 26 27 28 29 30 31 32 8.093797 3.416632 2.057358 2.262794 2.199264 1.318196 2.771726 2.442970 33 34 35 36 37 38 39 40 2.155361 2.716987 2.120306 1.988783 2.079278 2.150682 2.411360 2.332642 41 42 43 44 45 46 47 48 2.628728 2.686920 2.351460 2.234767 1.687064 1.784528 2.613587 1.949385 49 50 51 52 53 54 55 56 1.897530 3.309694 2.296488 3.594137 2.097298 1.790942 2.781630 1.833615 57 58 59 60 61 62 63 64 1.972995 2.128266 1.468135 2.704510 2.011569 2.500535 1.916365 1.514331 65 66 67 68 69 70 71 72 2.588818 2.063290 3.074374 2.840415 2.948732 2.315370 2.697043 3.330375 73 74 75 76 77 78 79 80 2.689948 6.705635 2.758437 2.531642 4.821751 4.722997 2.247213 3.016784 81 82 83 84 85 86 87 88 2.543022 2.516290 3.592895 2.930340 2.776798 3.114930 3.420795 3.136764 89 90 91 92 93 94 95 96 2.533854 3.419173 3.189774 2.725382 3.006518 2.419587 2.449953 3.552195 97 98 99 100 101 102 103 104 3.049488 3.253246 2.719880 2.740736 4.025371 3.053192 2.733781 2.805601 105 106 107 108 109 110 111 112 1.832339 3.293663 3.661628 2.427583 2.351298 2.568154 2.555963 3.002603 113 114 115 116 117 118 119 120 2.981203 3.282733 3.554564 2.660143 2.582473 2.929453 2.436393 2.738667 121 122 123 124 125 126 127 128 4.131329 2.864434 2.499631 2.340195 3.038535 2.594336 2.381266 6.898550 129 130 131 132 133 134 135 136 1.999156 2.682182 2.403683 3.084491 3.487434 2.986884 3.111200 2.449129 137 138 139 140 141 142 143 144 2.416120 2.623026 2.226929 2.351413 2.550817 5.493562 2.487414 4.186084 145 146 147 148 149 150 151 152 4.476301 2.681952 3.108789 3.035542 2.848071 2.526561 2.648983 2.801379 153 154 155 156 157 158 159 160 2.637201 2.914109 2.441270 2.914457 2.489621 2.846903 2.731483 2.399979 161 162 163 164 165 166 167 168 2.506309 3.884096 2.614599 2.576938 2.789528 2.315103 2.912199 3.897901 169 170 171 172 173 174 175 176 3.157035 2.377263 4.566296 3.620299 2.839836 3.695475 3.085054 2.739346 177 178 179 180 181 182 183 184 2.780956 2.667962 2.593635 2.739292 2.662632 2.750047 2.554918 2.280398 185 186 187 188 189 190 191 192 3.107915 2.719178 2.549632 2.487640 2.835062 2.595091 2.487589 2.613650 193 194 195 196 197 198 199 200 2.580884 3.025842 2.688580 2.585579 2.461990 2.678649 2.612132 3.543454 201 202 203 204 205 206 207 208 2.198199 2.634884 2.963269 2.773521 2.251845 2.190994 2.348904 3.324204 209 2.869148 > > GEcalib(~ 0 + Xs + g(d), dweight = d, const = c(total, sum(g(1 / pik, -1/2))), + method = "GEC", entropy = "HD")$w 1 2 3 4 5 6 7 8 2.131840 2.352304 2.445287 2.711152 2.647658 2.778817 2.362971 2.251310 9 10 11 12 13 14 15 16 2.485041 2.670847 2.537473 2.462811 2.447491 3.466361 3.140821 2.655390 17 18 19 20 21 22 23 24 3.127394 3.520078 2.590480 2.919985 2.278046 2.406557 7.873560 2.711020 25 26 27 28 29 30 31 32 8.462525 2.847991 2.094212 2.174064 2.235363 1.349371 2.506954 2.528501 33 34 35 36 37 38 39 40 1.893961 2.650694 2.034967 1.891565 2.199688 2.160974 2.377187 2.431160 41 42 43 44 45 46 47 48 2.448464 2.674360 2.165336 2.140824 1.614794 1.688530 2.639035 1.716559 49 50 51 52 53 54 55 56 1.768533 3.413359 2.375340 3.528230 2.109464 1.894310 2.966113 1.957566 57 58 59 60 61 62 63 64 2.201329 2.335488 1.455868 2.422595 2.137693 2.313768 1.949026 1.275865 65 66 67 68 69 70 71 72 2.605410 2.148783 3.131782 2.940776 3.019765 2.431318 2.756164 3.362981 73 74 75 76 77 78 79 80 2.758830 7.012626 2.378795 2.469491 4.866159 4.564794 2.259917 2.988544 81 82 83 84 85 86 87 88 2.574455 2.582194 3.547040 2.913304 2.827616 3.153670 3.382461 3.032331 89 90 91 92 93 94 95 96 2.558424 3.397261 3.221819 2.735972 3.059243 2.540958 2.595520 3.523061 97 98 99 100 101 102 103 104 3.089686 3.259205 2.659202 2.729683 4.029387 3.061749 2.683692 2.765778 105 106 107 108 109 110 111 112 1.619247 3.374210 3.684901 2.521776 2.418946 2.581616 2.603534 3.036248 113 114 115 116 117 118 119 120 3.003388 3.405590 3.554745 2.756842 2.702520 2.853033 2.614523 3.056981 121 122 123 124 125 126 127 128 3.904271 2.854628 2.554806 2.439929 2.937701 2.931291 2.571408 6.649643 129 130 131 132 133 134 135 136 2.195575 2.717327 2.648915 2.907078 3.197034 3.060039 3.032962 2.515734 137 138 139 140 141 142 143 144 2.451572 2.690853 2.187200 2.335463 2.697490 5.447741 2.486006 3.840349 145 146 147 148 149 150 151 152 4.711018 2.810329 2.856180 2.782055 2.930892 2.479603 2.589878 2.723028 153 154 155 156 157 158 159 160 2.585217 2.963698 2.320873 2.877064 2.590231 2.746552 2.739027 2.501885 161 162 163 164 165 166 167 168 2.601416 3.791465 2.757473 2.503707 2.857084 2.399950 2.848515 3.903185 169 170 171 172 173 174 175 176 3.215706 2.475985 4.636419 3.698974 2.868563 3.594029 3.165226 2.735568 177 178 179 180 181 182 183 184 2.777682 2.678206 2.269949 2.421650 2.694133 2.624518 2.550297 2.323529 185 186 187 188 189 190 191 192 2.976463 2.710682 2.500105 2.481742 2.711893 2.565559 2.483269 2.683042 193 194 195 196 197 198 199 200 2.521486 2.803387 2.560283 2.591435 2.474368 2.685906 2.607459 3.441127 201 202 203 204 205 206 207 208 2.262325 2.640028 3.019494 2.912205 2.313515 2.313565 2.457003 3.265249 209 2.748373 > > proc.time() user system elapsed 0.20 0.04 0.25