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 0.1830670 1.5981580 1.9654203 0.8399211 2.5154844 1.0674246 3.5234559 8 9 10 11 12 13 14 2.9543162 0.6548962 2.1824802 2.6598579 4.1155204 1.4783808 3.4062583 15 16 17 18 19 20 21 2.0966395 2.9490594 2.7051326 4.0452432 3.7821780 3.4547559 3.1854888 22 23 24 25 26 27 28 2.7917464 5.1265832 2.7870989 3.3737495 4.3377565 3.5104144 4.2077187 29 30 31 32 33 34 35 3.8546370 4.0080958 6.3474343 1.3206836 1.8876973 2.0900094 2.2528333 36 37 38 39 40 41 42 3.5667961 2.6942935 2.3880028 -0.3121757 2.4254674 2.1285659 1.2048025 43 44 45 46 47 48 49 2.5827352 2.9840505 2.7664382 1.8606195 1.5835504 1.6670462 2.5372283 50 51 52 53 54 55 56 1.8240476 1.5801627 2.2821875 3.1479001 3.2413375 0.8217201 1.9719835 57 58 59 60 61 62 63 2.4682304 2.9340094 2.3671280 1.6416962 1.0810387 2.7742432 2.8658461 64 65 66 67 68 69 70 2.0386135 4.4734859 3.1352108 2.8933692 2.4900856 2.6740444 3.9036272 71 72 73 74 75 76 77 1.3704978 3.3945986 1.9415599 2.9720327 3.5395412 3.5214440 3.0651550 78 79 80 81 82 83 84 3.9200954 4.4963689 4.2705213 4.0326156 3.3004741 3.1124837 1.4599970 85 86 87 88 89 90 91 2.5791160 2.6345363 3.8794848 4.0920775 3.4819480 1.9203199 3.7518116 92 93 94 95 96 97 98 3.4157007 3.6744773 3.4430571 3.0032816 2.4875334 2.2153331 2.8958059 99 100 101 102 103 104 105 2.6848097 3.5305379 3.2194230 3.4968982 0.7087708 1.8745810 1.3997272 106 107 108 109 110 111 112 2.1104977 2.0798468 2.7058555 2.6346419 2.7510643 2.6159298 2.9041156 113 114 115 116 117 118 119 2.3548741 2.8982804 3.3144631 3.4150353 3.8781416 3.0580861 2.2958132 120 121 122 123 124 125 126 1.8894286 4.5365609 3.3206698 1.4657307 2.2608313 3.7345460 3.6506126 127 128 129 130 131 132 133 2.0553440 2.9654933 3.0069447 3.7989201 4.2679743 3.5598692 3.7765809 134 135 136 137 138 139 140 3.1158566 3.7858209 3.3622076 3.4929263 3.5902925 2.4138357 3.3554177 141 142 143 144 145 146 147 3.1781616 2.9231729 3.8862973 1.2049725 3.3735296 1.9695799 1.4969506 148 149 150 151 152 153 154 3.0789018 2.8035894 3.7288801 2.1872378 1.5066290 3.8068740 3.4045520 155 156 157 158 159 160 161 3.7156306 2.9820610 3.7695436 3.0381816 2.7069069 2.8238519 5.5533993 162 163 164 165 166 167 168 3.4552349 2.7630510 3.8207129 2.9580539 3.5816136 3.5140180 3.3087861 169 170 171 172 173 174 175 2.6122023 3.9771895 4.4252856 3.6884564 3.7031862 3.0968522 5.0835959 176 177 178 179 180 181 182 4.1410199 3.9859915 4.2454206 4.0205820 3.8376207 3.7200456 3.3578060 183 184 185 186 187 188 189 3.8816651 3.1777310 2.7428371 3.4274744 2.4874201 0.8160541 2.2932830 190 191 192 193 194 195 196 3.7969167 3.8953942 3.8644807 5.0006699 3.2898877 4.2132686 4.6912415 197 198 199 1.2579291 3.7911260 2.7808821 > > 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] TRUE > GEcalib(~ 0 + Xs + g(d), dweight = d, const = c(total, sum(g(1 / pik, 0))), + method = "GEC", entropy = "ET")$w Messeage from nleqslv: nleqslv_res$message = x-values within tolerance 'xtol' , nleqslv_res$termcd = 2 [1] NA Warning message: In GEcalib(~0 + Xs + g(d), dweight = d, const = c(total, sum(g(1/pik, : Convergence failed > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "EL")$w 1 2 3 4 5 6 7 8 1.375106 1.579907 2.023853 1.475341 2.334497 1.517303 3.191749 2.466427 9 10 11 12 13 14 15 16 1.294501 2.247482 2.207255 3.811727 1.917594 2.956558 1.787579 2.517223 17 18 19 20 21 22 23 24 2.314148 4.008589 3.046422 2.998522 3.320601 2.638871 6.498028 2.515610 25 26 27 28 29 30 31 32 3.186242 4.971992 3.309683 4.965361 3.401957 4.403749 7.979714 1.699752 33 34 35 36 37 38 39 40 1.561562 1.602585 2.039445 2.839361 2.388274 2.126005 1.212882 2.197791 41 42 43 44 45 46 47 48 2.399942 1.763302 2.426601 2.565768 2.451155 2.021253 1.951828 1.994496 49 50 51 52 53 54 55 56 2.225277 2.008489 1.851588 2.333196 2.808653 2.883784 1.518986 2.266703 57 58 59 60 61 62 63 64 2.441184 3.289563 2.279134 2.162988 1.999330 2.451952 2.829294 1.909007 65 66 67 68 69 70 71 72 4.786918 2.868410 2.704796 2.402554 2.242210 3.247271 1.548198 3.149594 73 74 75 76 77 78 79 80 1.690031 2.862433 3.022039 3.033760 2.793185 3.523591 4.610091 4.112172 81 82 83 84 85 86 87 88 4.144241 2.875823 3.201843 2.073463 2.360678 2.445479 4.130779 4.419146 89 90 91 92 93 94 95 96 3.111304 1.992193 3.839590 3.228486 3.210655 3.137849 2.987451 1.816854 97 98 99 100 101 102 103 104 2.128738 2.634186 2.438050 3.489384 2.890906 3.148462 1.647373 1.855686 105 106 107 108 109 110 111 112 1.531882 2.117687 2.194777 2.439271 2.615066 2.387750 2.531597 2.666685 113 114 115 116 117 118 119 120 1.955685 2.640194 2.864865 2.977101 3.382666 3.233392 2.063740 2.472949 121 122 123 124 125 126 127 128 5.078548 3.163019 2.160426 2.998248 3.161719 3.152993 2.264931 3.281200 129 130 131 132 133 134 135 136 2.494931 3.239935 4.344238 3.442569 3.597650 3.260864 3.687534 3.004503 137 138 139 140 141 142 143 144 3.358683 3.069243 2.355601 2.954829 3.030537 2.755814 3.820849 1.488705 145 146 147 148 149 150 151 152 2.694103 2.097100 2.216038 2.939265 2.510522 3.293967 2.187084 1.917354 153 154 155 156 157 158 159 160 4.194116 3.079526 4.273390 2.973501 3.379314 2.885791 2.666772 2.587613 161 162 163 164 165 166 167 168 8.569689 3.051219 2.538173 3.679468 2.613277 3.630188 3.317052 2.882856 169 170 171 172 173 174 175 176 2.532487 4.428818 4.891815 3.509142 3.286811 2.967466 6.649837 3.946577 177 178 179 180 181 182 183 184 4.321949 3.379704 3.817636 4.863612 4.459639 4.463977 4.000186 3.213151 185 186 187 188 189 190 191 192 2.778921 3.054596 2.661839 1.659679 2.298712 3.360547 3.926223 3.830892 193 194 195 196 197 198 199 6.540730 2.885233 3.920697 3.302782 1.996671 3.433518 2.625302 > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "HD")$w 1 2 3 4 5 6 7 8 1.305436 1.607134 1.970391 1.393737 2.351283 1.408514 3.243519 2.492174 9 10 11 12 13 14 15 16 1.212663 2.258242 2.195067 3.892856 1.798953 3.036305 1.772596 2.593558 17 18 19 20 21 22 23 24 2.340979 4.044175 3.162420 3.061162 3.326573 2.663442 6.056020 2.521523 25 26 27 28 29 30 31 32 3.182901 4.806295 3.343901 4.795029 3.505123 4.372849 7.739307 1.573143 33 34 35 36 37 38 39 40 1.568769 1.684737 2.054904 2.874589 2.376504 2.118728 1.081154 2.184375 41 42 43 44 45 46 47 48 2.420201 1.779822 2.445594 2.675246 2.477640 2.002804 1.968153 2.039266 49 50 51 52 53 54 55 56 2.236512 2.099512 1.832216 2.357411 2.846309 2.902693 1.471531 2.280987 57 58 59 60 61 62 63 64 2.489397 3.321604 2.261196 2.215395 2.023522 2.467483 2.912362 1.863131 65 66 67 68 69 70 71 72 4.633049 2.881473 2.728730 2.399001 2.273821 3.285532 1.557039 3.188530 73 74 75 76 77 78 79 80 1.622847 2.890957 3.058196 3.068524 2.831308 3.619139 4.694994 4.134843 81 82 83 84 85 86 87 88 4.198508 2.924722 3.203325 1.927956 2.357681 2.415044 4.191305 4.387793 89 90 91 92 93 94 95 96 3.164824 1.927943 3.855610 3.278702 3.304656 3.184774 2.990971 1.832420 97 98 99 100 101 102 103 104 2.089758 2.670755 2.453361 3.521943 2.920671 3.220730 1.613055 1.839881 105 106 107 108 109 110 111 112 1.473767 2.106081 2.190583 2.463293 2.631293 2.394381 2.484774 2.673996 113 114 115 116 117 118 119 120 1.893989 2.661667 2.886756 3.050171 3.467644 3.236610 2.100037 2.375357 121 122 123 124 125 126 127 128 5.179653 3.174801 2.005974 2.951344 3.188794 3.256614 2.177177 3.281971 129 130 131 132 133 134 135 136 2.525421 3.381428 4.437402 3.522143 3.672522 3.273282 3.733754 3.064095 137 138 139 140 141 142 143 144 3.401060 3.113763 2.383462 2.985501 3.061133 2.788269 3.877768 1.425340 145 146 147 148 149 150 151 152 2.778063 1.994144 2.093713 2.994663 2.565701 3.352640 2.072119 1.787589 153 154 155 156 157 158 159 160 4.220033 3.118124 4.325368 2.987736 3.439830 2.919540 2.682532 2.603426 161 162 163 164 165 166 167 168 6.957279 3.092553 2.522750 3.688622 2.631466 3.638221 3.371468 2.897537 169 170 171 172 173 174 175 176 2.519944 4.429259 4.817747 3.563763 3.367418 2.982891 6.402449 4.041291 177 178 179 180 181 182 183 184 4.388667 3.424621 3.886640 4.913479 4.555141 4.541429 4.063827 3.220741 185 186 187 188 189 190 191 192 2.789909 3.081537 2.640974 1.534013 2.265489 3.450487 4.012501 3.885285 193 194 195 196 197 198 199 6.136718 2.940052 3.965970 3.796297 1.817879 3.532056 2.637955 > > 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 1.214791 1.581430 2.060228 1.425160 2.394432 1.556086 3.235346 2.743793 9 10 11 12 13 14 15 16 1.317329 2.173078 2.520683 3.609177 1.864057 3.166045 1.998676 2.660301 17 18 19 20 21 22 23 24 2.493167 3.934841 3.259700 3.224177 3.087010 2.643175 6.154004 2.613544 25 26 27 28 29 30 31 32 3.332534 4.678662 3.374723 4.522795 3.742343 4.224122 8.255381 1.567230 33 34 35 36 37 38 39 40 1.671740 1.640921 2.068782 3.451898 2.604791 2.285863 1.065616 2.306952 41 42 43 44 45 46 47 48 2.075068 1.475552 2.378159 2.274159 2.606544 1.944669 1.722919 1.617726 49 50 51 52 53 54 55 56 2.357372 1.665317 1.746110 2.211915 2.921398 3.065595 1.366965 2.005168 57 58 59 60 61 62 63 64 2.264870 2.518714 2.334088 1.687918 1.459590 2.563074 2.204318 2.018156 65 66 67 68 69 70 71 72 4.497157 3.002157 2.783399 2.409005 2.398824 3.917135 1.472823 3.298659 73 74 75 76 77 78 79 80 1.915049 2.781084 3.463168 3.424021 2.828150 3.756244 4.848738 4.517925 81 82 83 84 85 86 87 88 4.124885 3.053517 3.060801 1.861242 2.406850 2.552048 3.850245 4.387915 89 90 91 92 93 94 95 96 3.346862 1.924580 3.762264 3.268242 3.434067 3.245733 2.823132 1.867863 97 98 99 100 101 102 103 104 2.190844 2.721405 2.521715 3.479879 3.057692 3.214077 1.366622 1.868426 105 106 107 108 109 110 111 112 1.478974 2.130622 2.130625 2.516972 2.568611 2.560288 2.489374 2.803053 113 114 115 116 117 118 119 120 2.329831 2.762250 3.194049 3.224530 3.695544 2.859843 2.111814 2.009539 121 122 123 124 125 126 127 128 5.165301 3.096232 1.784207 2.280961 3.675907 3.439323 2.083054 2.794198 129 130 131 132 133 134 135 136 2.820300 3.582640 4.434020 3.391248 3.684302 2.924783 3.734753 3.126765 137 138 139 140 141 142 143 144 3.412280 3.447134 2.289330 3.182173 2.933329 2.795682 3.860324 1.554380 145 146 147 148 149 150 151 152 3.053840 2.038832 1.862026 2.797887 2.404945 3.593430 2.114414 1.754969 153 154 155 156 157 158 159 160 3.813609 3.278729 3.655263 2.864934 3.717248 2.900381 2.627333 2.700365 161 162 163 164 165 166 167 168 7.508063 3.346261 2.647023 3.927026 2.777892 3.542980 3.395159 3.113399 169 170 171 172 173 174 175 176 2.542379 4.086620 4.954233 3.614058 3.589630 2.962724 6.353624 4.351723 177 178 179 180 181 182 183 184 4.011040 4.360026 3.985403 3.968211 3.658328 3.215268 3.908844 3.138243 185 186 187 188 189 190 191 192 2.733529 3.268538 2.506477 1.608310 2.353237 3.637353 3.925801 3.898152 193 194 195 196 197 198 199 6.321211 3.073163 4.354887 3.850729 1.715963 3.676228 2.667124 > > proc.time() user system elapsed 0.32 0.07 0.39