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 8 2.7425899 3.8384590 3.7409966 4.4821777 3.2501992 1.8697992 3.0838771 4.0057978 9 10 11 12 13 14 15 16 3.5637829 2.9540045 2.7335849 2.5588019 2.7220999 4.2939572 3.0781303 2.4607241 17 18 19 20 21 22 23 24 3.0885498 2.7559746 3.2589046 3.6151012 2.6844502 2.9659843 2.9560667 3.7773220 25 26 27 28 29 30 31 32 3.3928483 3.3282012 4.5583912 3.4332434 3.0390698 4.1912873 4.8953131 2.3102032 33 34 35 36 37 38 39 40 2.8444475 0.4543248 3.7793755 2.1535149 2.9800075 0.4160309 1.3722702 1.5065959 41 42 43 44 45 46 47 48 0.6949824 2.8315861 2.5238023 2.4915055 2.8700590 2.1602631 2.6203282 4.5326232 49 50 51 52 53 54 55 56 2.8353483 1.9528197 2.8254774 4.2998857 2.9271471 2.5519082 2.6227771 1.8688805 57 58 59 60 61 62 63 64 2.4858390 2.4435921 2.1862039 2.9837351 2.4384187 2.6094271 1.9838468 2.4769375 65 66 67 68 69 70 71 72 2.7091314 2.1314849 2.6825931 1.5185757 3.9842864 1.3541573 2.6071513 1.3470509 73 74 75 76 77 78 79 80 2.2683589 2.8892414 4.0604636 3.9042244 4.1391641 3.1999541 3.0430224 3.0557156 81 82 83 84 85 86 87 88 4.4722365 4.8204268 4.4213990 2.9486805 3.3133134 3.1255637 3.1514966 2.7422223 89 90 91 92 93 94 95 96 2.5740332 3.0858020 4.0817062 3.2434470 4.3916082 4.1899763 3.4056069 2.6184243 97 98 99 100 101 102 103 104 3.5339444 2.9130977 2.9150265 3.0682745 2.6623363 4.5045407 2.8866538 2.9113098 105 106 107 108 109 110 111 112 3.9268414 3.0786368 2.7085614 2.4655828 4.0554827 4.3157589 2.7113137 2.4634422 113 114 115 116 117 118 119 120 3.2549246 4.0141688 2.5420883 2.5959194 3.6913454 1.7026859 3.1755908 2.7503300 121 122 123 124 125 126 127 128 2.6574719 3.0611229 2.9497072 2.5275073 2.6716767 2.8706898 2.4231747 2.8375701 129 130 131 132 133 134 135 136 2.5989423 2.6812805 3.0969684 5.3866539 4.6266669 3.2916187 2.6948152 2.7817375 137 138 139 140 141 142 143 144 2.6947193 2.8917291 2.9505727 2.3843656 3.2844116 3.2047511 3.0933232 2.7896768 145 146 147 148 149 150 151 152 2.6927658 2.5160817 1.8194895 2.6317080 2.9007157 3.7684700 3.1484138 4.0805093 153 154 155 156 157 158 159 160 3.7132269 2.9929416 4.1183887 3.5697164 3.7218063 4.6621308 2.9445804 1.9284269 161 162 163 164 165 166 167 168 4.1635316 3.0527478 2.6393632 2.8425394 2.4336327 2.8346504 3.9715365 2.3514914 169 170 171 172 173 174 175 176 2.6416238 2.5690472 2.9083470 2.6511709 2.8990649 2.5094718 2.8337276 2.9724644 177 178 179 180 181 182 183 184 3.6965422 4.4236275 2.3900618 2.6402686 2.7983623 2.7326016 2.9565211 2.9548578 185 186 187 188 189 190 191 192 2.8923016 3.6118950 2.8046753 2.7733778 2.6903497 2.8224636 5.1760370 3.1737364 193 194 195 3.1495997 2.6163819 3.0791732 > > 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 1 2 3 4 5 6 7 8 2.6105831 3.8718663 3.6190959 4.7691521 3.2191738 1.9523240 2.9397950 3.9649138 9 10 11 12 13 14 15 16 3.3141545 2.8701156 2.7628966 2.3559038 2.6352545 4.5356112 3.0002345 2.3163499 17 18 19 20 21 22 23 24 2.9767137 2.7259689 3.1275307 3.5107460 2.7178917 2.9359117 2.9721046 3.8024379 25 26 27 28 29 30 31 32 3.4255799 3.3132158 4.7539939 3.5029836 2.9698982 4.4101726 4.6787008 2.1442654 33 34 35 36 37 38 39 40 2.5684235 0.9978000 3.6028352 2.1880118 2.8062064 0.8783722 1.5680276 1.3878162 41 42 43 44 45 46 47 48 1.1214643 2.7777373 2.3686127 2.4544350 2.7828216 2.1144584 2.6066879 4.5004329 49 50 51 52 53 54 55 56 2.7383040 2.0006470 2.7663878 4.4499163 2.8979403 2.4947868 2.5887354 1.6724210 57 58 59 60 61 62 63 64 2.3831079 2.3639948 2.1887196 2.9539547 2.4179751 2.5256233 2.0166313 2.4228554 65 66 67 68 69 70 71 72 2.5864744 2.0919856 2.5078491 1.6546360 4.0040401 1.5371981 2.5867345 1.5659902 73 74 75 76 77 78 79 80 2.2922454 2.8955698 4.0602010 3.8874041 4.3921040 3.2071801 3.0255897 3.0517541 81 82 83 84 85 86 87 88 4.8627782 4.8607226 4.7620954 2.8628175 3.3316319 3.1279854 3.1486628 2.7638229 89 90 91 92 93 94 95 96 2.6102134 3.0577642 4.1247850 3.2016542 4.1908580 4.0262377 3.3739622 2.6358891 97 98 99 100 101 102 103 104 3.5379179 2.8374783 2.8793414 3.0515883 2.6623833 4.6462536 2.8699881 2.9039562 105 106 107 108 109 110 111 112 3.9356371 2.9946288 2.6868907 2.4717016 3.9482961 4.4049649 2.6372376 2.4547683 113 114 115 116 117 118 119 120 3.1290999 3.9407429 2.5005438 2.5856592 3.6870493 1.8913230 2.9873568 2.7128596 121 122 123 124 125 126 127 128 2.6050424 2.9194323 2.9697862 2.5525602 2.7094970 2.7820483 2.4413604 2.8133624 129 130 131 132 133 134 135 136 2.4142578 2.6811338 3.0301387 6.1223418 4.6425787 3.1910562 2.7006801 2.7937300 137 138 139 140 141 142 143 144 2.7148334 2.8989851 2.9682955 2.4446337 3.2812716 3.1698128 3.0648844 2.7867117 145 146 147 148 149 150 151 152 2.6995352 2.5482065 1.9128163 2.6186825 2.8721045 3.8077935 3.1121861 4.1924428 153 154 155 156 157 158 159 160 3.7652940 2.9875489 4.2629185 3.5780488 3.7791540 5.0555002 2.8723906 2.0484809 161 162 163 164 165 166 167 168 4.4176769 3.0153195 2.6246704 2.8435899 2.4457049 2.7886431 3.8831013 2.3966594 169 170 171 172 173 174 175 176 2.6488232 2.6066912 2.8480615 2.6796850 2.8951535 2.5477071 2.8294905 2.9576509 177 178 179 180 181 182 183 184 3.7164585 4.8733088 2.3754037 2.5999326 2.7909731 2.6867889 2.9687440 2.8997914 185 186 187 188 189 190 191 192 2.8562161 3.4998330 2.7818094 2.7517904 2.6730246 2.8553866 4.6903542 3.0764050 193 194 195 3.0444500 2.6355761 3.0254008 > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "EL")$w 1 2 3 4 5 6 7 8 2.642242 3.713991 3.639431 5.037273 3.121358 1.987576 2.851231 4.161897 9 10 11 12 13 14 15 16 3.126186 2.784155 2.742523 2.206677 2.539921 4.698396 2.915297 2.186276 17 18 19 20 21 22 23 24 2.882375 2.679819 3.015025 3.385892 2.728979 2.837703 2.948054 3.811149 25 26 27 28 29 30 31 32 3.370438 3.261274 5.001458 3.453227 2.927158 4.725793 4.450825 2.097846 33 34 35 36 37 38 39 40 2.518467 1.368211 3.483301 2.413972 3.019360 1.162307 1.691315 1.554999 41 42 43 44 45 46 47 48 1.301343 2.685903 2.296619 2.477151 2.684960 2.120205 2.577020 4.509188 49 50 51 52 53 54 55 56 2.638718 1.979548 2.735872 4.521918 2.871991 2.523959 2.552145 1.677317 57 58 59 60 61 62 63 64 2.430828 2.472655 2.168662 2.912855 2.353872 2.432687 2.026282 2.348278 65 66 67 68 69 70 71 72 2.478864 2.076147 2.416758 1.691723 3.858003 1.599490 2.540562 1.596378 73 74 75 76 77 78 79 80 2.262809 2.862681 3.946156 3.790502 4.375231 3.169712 2.953425 3.038855 81 82 83 84 85 86 87 88 5.093681 4.864099 5.188558 2.745916 3.259618 3.072156 3.107392 2.761756 89 90 91 92 93 94 95 96 2.612355 2.993261 4.099407 3.099585 3.976466 3.810632 3.306764 2.608242 97 98 99 100 101 102 103 104 3.467503 2.797574 2.833348 3.044490 2.637084 4.779715 2.838186 2.873579 105 106 107 108 109 110 111 112 3.770742 2.951832 2.670661 2.487512 3.742105 4.259670 2.555433 2.419689 113 114 115 116 117 118 119 120 3.063136 3.783370 2.475633 2.582837 3.626583 1.919886 2.876858 2.757976 121 122 123 124 125 126 127 128 2.791930 2.776164 2.967423 2.576026 2.703537 2.692121 2.412790 2.819206 129 130 131 132 133 134 135 136 2.308694 2.731885 2.966554 6.716349 4.662683 3.046766 2.693387 2.798183 137 138 139 140 141 142 143 144 2.752356 2.905107 2.963864 2.482946 3.224359 3.118301 3.055762 2.779257 145 146 147 148 149 150 151 152 2.694679 2.545382 1.982056 2.597887 2.842905 3.772330 2.990348 4.147995 153 154 155 156 157 158 159 160 3.667932 2.940457 4.233239 3.504678 3.788637 5.402888 2.794696 2.046445 161 162 163 164 165 166 167 168 4.633712 3.076425 2.637384 2.847198 2.629263 2.926604 3.652959 2.737773 169 170 171 172 173 174 175 176 2.689617 2.672047 2.801221 2.759887 2.877396 2.578129 2.819600 2.929119 177 178 179 180 181 182 183 184 3.702075 6.457815 2.421323 2.645827 2.839922 2.693191 2.974995 2.833856 185 186 187 188 189 190 191 192 2.919162 3.361130 2.751054 2.706141 2.632774 2.852190 4.357712 2.996979 193 194 195 3.039622 2.734496 2.997264 > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "HD")$w 1 2 3 4 5 6 7 8 2.663015 3.807832 3.651068 4.896572 3.168423 1.995065 2.891814 4.072494 9 10 11 12 13 14 15 16 3.209951 2.823392 2.757932 2.274719 2.564209 4.633255 2.950003 2.228341 17 18 19 20 21 22 23 24 2.920460 2.697685 3.067969 3.445172 2.722254 2.874991 2.965239 3.812199 25 26 27 28 29 30 31 32 3.404325 3.293097 4.857369 3.499946 2.953712 4.580525 4.546681 2.098906 33 34 35 36 37 38 39 40 2.528608 1.184014 3.529254 2.297018 2.945514 1.023326 1.628975 1.464544 41 42 43 44 45 46 47 48 1.214888 2.719490 2.351989 2.490368 2.727148 2.145812 2.598473 4.518463 49 50 51 52 53 54 55 56 2.675875 1.990410 2.759959 4.500100 2.891442 2.536825 2.570546 1.646044 57 58 59 60 61 62 63 64 2.448275 2.475176 2.165437 2.934346 2.374451 2.453679 2.045267 2.363118 65 66 67 68 69 70 71 72 2.502628 2.099848 2.444161 1.677267 3.938056 1.569760 2.542867 1.621755 73 74 75 76 77 78 79 80 2.269948 2.874165 3.995898 3.843609 4.435249 3.194221 2.984016 3.053589 81 82 83 84 85 86 87 88 4.991377 4.843110 4.968076 2.781279 3.305085 3.101353 3.133502 2.764354 89 90 91 92 93 94 95 96 2.606416 3.019571 4.102573 3.144678 4.096101 3.908027 3.340049 2.615652 97 98 99 100 101 102 103 104 3.508956 2.817355 2.857291 3.059517 2.640965 4.715107 2.855419 2.891056 105 106 107 108 109 110 111 112 3.848386 2.967187 2.666152 2.465712 3.842145 4.348491 2.561612 2.412130 113 114 115 116 117 118 119 120 3.090254 3.885736 2.480928 2.568944 3.682050 1.885778 2.919476 2.734858 121 122 123 124 125 126 127 128 2.724927 2.828131 2.976034 2.553256 2.707474 2.718467 2.410525 2.820432 129 130 131 132 133 134 135 136 2.351181 2.712306 2.996301 6.473303 4.691477 3.101155 2.692768 2.802639 137 138 139 140 141 142 143 144 2.737753 2.910970 2.975195 2.480130 3.249013 3.143357 3.069195 2.783920 145 146 147 148 149 150 151 152 2.699311 2.543662 1.930232 2.594732 2.858799 3.799029 3.037146 4.193824 153 154 155 156 157 158 159 160 3.720419 2.963262 4.256304 3.540524 3.795017 5.213329 2.820006 2.016029 161 162 163 164 165 166 167 168 4.515284 3.061031 2.620454 2.846835 2.552129 2.887966 3.759730 2.621422 169 170 171 172 173 174 175 176 2.668040 2.643271 2.813961 2.731565 2.886781 2.557246 2.825110 2.943969 177 178 179 180 181 182 183 184 3.717734 5.471391 2.386712 2.619922 2.826657 2.682715 2.981018 2.860635 185 186 187 188 189 190 191 192 2.902365 3.426392 2.765821 2.716572 2.644983 2.855674 4.527050 3.029560 193 194 195 3.047313 2.697483 3.012484 > > 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.535480 3.872726 3.516167 4.954421 3.197631 1.978145 2.873352 3.897669 9 10 11 12 13 14 15 16 3.180741 2.831594 2.767373 2.290266 2.608504 4.666542 2.959296 2.282567 17 18 19 20 21 22 23 24 2.922521 2.712220 3.058993 3.446444 2.727948 2.921237 2.971254 3.797583 25 26 27 28 29 30 31 32 3.429392 3.289136 4.862578 3.519047 2.923938 4.531931 4.559647 2.129403 33 34 35 36 37 38 39 40 2.490478 1.201652 3.579652 2.233468 2.736383 1.064268 1.662939 1.458002 41 42 43 44 45 46 47 48 1.277887 2.756100 2.323011 2.434444 2.745844 2.102807 2.597789 4.463157 49 50 51 52 53 54 55 56 2.701506 2.032130 2.733940 4.518853 2.876630 2.462373 2.576128 1.695537 57 58 59 60 61 62 63 64 2.332350 2.313245 2.208410 2.931937 2.417828 2.505644 2.027266 2.414926 65 66 67 68 69 70 71 72 2.555673 2.084942 2.459868 1.720262 4.001240 1.625805 2.585843 1.628960 73 74 75 76 77 78 79 80 2.307810 2.892938 4.033504 3.857412 4.517410 3.197747 3.010740 3.035513 81 82 83 84 85 86 87 88 5.117980 4.818327 4.977811 2.831684 3.321476 3.114746 3.136079 2.767346 89 90 91 92 93 94 95 96 2.624410 3.034807 4.131892 3.176975 4.040032 3.919416 3.341217 2.642078 97 98 99 100 101 102 103 104 3.521584 2.804789 2.855779 3.029754 2.659833 4.701707 2.856656 2.892448 105 106 107 108 109 110 111 112 3.926897 2.951297 2.680540 2.481097 3.853247 4.429595 2.619802 2.464426 113 114 115 116 117 118 119 120 3.063324 3.847727 2.491947 2.589062 3.652874 1.970413 2.916482 2.696808 121 122 123 124 125 126 127 128 2.579626 2.861118 2.969190 2.566708 2.723879 2.751866 2.456047 2.800230 129 130 131 132 133 134 135 136 2.358733 2.679362 2.995054 6.725720 4.611744 3.137454 2.702708 2.792224 137 138 139 140 141 142 143 144 2.719538 2.893175 2.966823 2.456708 3.270338 3.141933 3.044654 2.781784 145 146 147 148 149 150 151 152 2.698805 2.558846 1.976149 2.617231 2.854317 3.810659 3.090440 4.231063 153 154 155 156 157 158 159 160 3.776294 2.978144 4.344245 3.570815 3.790932 5.342771 2.842212 2.107259 161 162 163 164 165 166 167 168 4.567883 2.978931 2.622282 2.839180 2.449922 2.755510 3.801526 2.401643 169 170 171 172 173 174 175 176 2.649567 2.619920 2.823146 2.685180 2.887830 2.564152 2.822154 2.943687 177 178 179 180 181 182 183 184 3.702724 5.155421 2.384794 2.591529 2.781610 2.671647 2.966401 2.876825 185 186 187 188 189 190 191 192 2.830991 3.444955 2.767505 2.745615 2.669745 2.865953 4.341713 3.031841 193 194 195 2.989765 2.637536 2.991748 > > proc.time() user system elapsed 0.14 0.10 0.23