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.6846595 2.9855800 2.9717130 2.0004952 2.4799660 2.2996176 3.1856662 2.6354462 9 10 11 12 13 14 15 16 2.7506042 1.7173748 2.4438312 2.4430498 3.0092682 2.5177507 1.8581903 3.1286261 17 18 19 20 21 22 23 24 2.4898658 2.4314867 2.7693894 3.3498068 2.7886152 3.1448388 2.7276128 3.0513244 25 26 27 28 29 30 31 32 3.0323343 3.4547923 3.0188893 3.0086950 2.6516866 3.0717426 2.9386042 2.0300244 33 34 35 36 37 38 39 40 1.5997433 2.5077742 1.2050679 2.7510523 3.4271996 4.2539839 1.0901106 0.9119482 41 42 43 44 45 46 47 48 2.1080176 2.4901527 2.0898488 2.5413013 2.1394520 2.6897986 2.4558942 2.4999697 49 50 51 52 53 54 55 56 2.7810979 2.0057282 2.5049995 1.6513688 2.3005801 2.4823007 2.0480074 2.1847714 57 58 59 60 61 62 63 64 1.4975265 2.3241219 2.5035958 2.6461920 2.3026840 2.3133082 2.9236944 2.4658483 65 66 67 68 69 70 71 72 1.8483033 2.9732578 2.1421106 1.1287405 2.2009218 2.3881424 1.7135031 2.6579774 73 74 75 76 77 78 79 80 2.7320381 2.6297166 2.0081262 2.3257688 1.9669065 2.1936898 2.2741231 2.3174809 81 82 83 84 85 86 87 88 2.3701819 1.9151854 2.3081888 2.6662065 3.2698409 2.7157093 2.5733139 2.6644382 89 90 91 92 93 94 95 96 2.8199091 2.0656947 3.7822181 3.1549851 3.2301818 2.9225984 2.9006074 3.1799900 97 98 99 100 101 102 103 104 2.7904103 3.5598337 3.2062493 2.7381223 3.4617568 3.2207182 3.1164862 2.8628285 105 106 107 108 109 110 111 112 2.5932811 2.9486434 2.8951426 2.6926269 2.6481190 2.0926214 2.8278476 2.9850144 113 114 115 116 117 118 119 120 3.2608369 2.6896236 2.7407409 2.9008958 2.7594384 3.0421901 2.5995693 2.4796022 121 122 123 124 125 126 127 128 2.6343128 2.7363576 3.3261199 2.7715084 2.4249483 2.3788970 2.9279509 2.8816324 129 130 131 132 133 134 135 136 3.2695480 2.8052053 3.7938416 3.0263605 2.8136549 3.0597449 3.4163843 4.1226285 137 138 139 140 141 142 143 144 3.4477032 3.4348047 2.7223084 3.7174137 2.5972648 2.7902525 4.2570936 2.9713554 145 146 147 148 149 150 151 152 2.7254037 3.9039758 3.5281931 2.8141268 3.0215776 3.2622585 3.0811082 2.8582823 153 154 155 156 157 158 159 160 2.8646207 2.8668439 2.6293646 2.8904218 2.8159741 2.9805860 3.0852758 1.9598536 161 162 163 164 165 166 167 168 3.2852057 2.8519663 2.7009900 1.2325365 3.4515198 3.6007972 2.8328348 3.5184702 169 170 171 172 173 174 175 176 3.6250752 2.6376944 2.6749553 3.2400460 2.5229575 3.1003040 3.1134135 3.0172031 177 178 179 180 181 182 183 184 3.0602705 3.0129547 2.7043355 2.8168690 2.7220256 2.7490528 2.9938975 3.0270914 185 186 187 188 189 190 191 192 3.2758076 3.0520039 3.2784388 3.2880426 3.0445090 3.1446027 2.8206877 3.0801215 193 194 195 196 197 198 199 200 3.2770118 3.5562110 3.2134922 2.9568732 3.0028095 3.0077714 3.1578815 3.0232892 201 202 203 204 205 206 207 208 3.0609885 2.9664871 3.5762836 3.3842193 3.0071482 3.5594728 2.5925880 2.8197492 209 210 211 212 2.8204972 2.0677788 3.3975225 3.5387430 > > 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.657467 3.019973 2.995996 2.002782 2.456457 2.240283 3.236271 2.605529 9 10 11 12 13 14 15 16 2.753569 1.778454 2.395143 2.405241 3.049165 2.497096 1.890229 3.180298 17 18 19 20 21 22 23 24 2.399211 2.407759 2.731680 3.485933 2.794371 3.215055 2.723006 3.064778 25 26 27 28 29 30 31 32 3.059650 3.649104 3.058609 3.023940 2.667516 3.133410 2.918659 1.971716 33 34 35 36 37 38 39 40 1.579531 2.392112 1.342911 2.564011 3.262703 4.232006 1.266417 1.194204 41 42 43 44 45 46 47 48 2.091068 2.454470 2.079488 2.530439 2.080542 2.659468 2.426470 2.482149 49 50 51 52 53 54 55 56 2.790941 1.991940 2.478140 1.707663 2.284426 2.455970 2.055694 2.172524 57 58 59 60 61 62 63 64 1.604801 2.299656 2.480576 2.644923 2.293458 2.286534 2.917446 2.446282 65 66 67 68 69 70 71 72 1.886494 2.989138 2.126736 1.265380 2.194027 2.374505 1.761922 2.585491 73 74 75 76 77 78 79 80 2.728819 2.609713 1.986747 2.296653 1.982648 2.188017 2.235404 2.303704 81 82 83 84 85 86 87 88 2.333462 1.952194 2.259823 2.658599 3.235364 2.683684 2.558037 2.626414 89 90 91 92 93 94 95 96 2.782880 2.048160 4.006741 3.185765 3.233263 2.916104 2.891587 3.216841 97 98 99 100 101 102 103 104 2.738723 3.537826 3.136100 2.729355 3.533991 3.254819 3.144808 2.851629 105 106 107 108 109 110 111 112 2.618477 2.960428 2.871321 2.669625 2.632227 2.081149 2.822995 2.966092 113 114 115 116 117 118 119 120 3.332247 2.675947 2.718681 2.930130 2.752871 2.925682 2.583236 2.449279 121 122 123 124 125 126 127 128 2.609298 2.696788 3.405548 2.770452 2.402885 2.361251 2.973183 2.884227 129 130 131 132 133 134 135 136 3.283274 2.785872 3.812886 2.994084 2.763258 3.034926 3.348693 4.322047 137 138 139 140 141 142 143 144 3.430398 3.477415 2.650929 3.677948 2.553242 2.754468 4.583892 2.943510 145 146 147 148 149 150 151 152 2.655202 3.903014 3.522357 2.764480 2.924173 3.257554 3.047761 2.827586 153 154 155 156 157 158 159 160 2.855000 2.785114 2.595020 2.863287 2.802709 2.893687 3.067322 1.947376 161 162 163 164 165 166 167 168 3.289436 2.825034 2.680397 1.263608 3.429418 3.516468 2.792769 3.622995 169 170 171 172 173 174 175 176 3.754558 2.599491 2.634244 3.261621 2.508611 3.119119 3.117906 2.801586 177 178 179 180 181 182 183 184 3.087178 3.008893 2.688553 2.788442 2.701019 2.712392 2.984945 3.058731 185 186 187 188 189 190 191 192 3.280764 3.071416 3.360401 3.392322 3.052766 3.219544 2.757805 3.095269 193 194 195 196 197 198 199 200 3.297365 3.595766 3.271487 2.939476 2.987174 3.037839 3.232724 3.050294 201 202 203 204 205 206 207 208 3.191810 2.912897 3.575940 3.434773 3.007405 3.545745 2.566469 2.777964 209 210 211 212 2.784285 2.037398 3.365765 3.638336 > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "EL")$w 1 2 3 4 5 6 7 8 2.680510 3.081248 3.052910 1.800340 2.445399 2.275206 3.215236 2.682040 9 10 11 12 13 14 15 16 2.808075 1.796874 2.420703 2.521394 3.095189 2.513827 1.967306 3.220648 17 18 19 20 21 22 23 24 2.424206 2.448332 2.738644 3.468312 2.787021 3.198229 2.826004 3.107357 25 26 27 28 29 30 31 32 3.056086 3.926729 3.109376 2.985125 2.707234 3.192673 2.981274 2.050986 33 34 35 36 37 38 39 40 1.660453 2.395791 1.517349 2.532969 3.119060 3.595961 1.391607 1.247163 41 42 43 44 45 46 47 48 2.072372 2.623249 1.942889 2.627094 1.966282 2.694516 2.459495 2.495793 49 50 51 52 53 54 55 56 2.787184 1.766410 2.450213 1.636257 2.131152 2.392328 1.943027 2.208523 57 58 59 60 61 62 63 64 1.619724 2.229067 2.435938 2.634922 2.107999 2.202732 2.994147 2.285034 65 66 67 68 69 70 71 72 1.739180 3.001660 1.909091 1.383613 1.843195 2.416913 1.615336 2.608128 73 74 75 76 77 78 79 80 2.729212 2.621861 1.952107 2.400963 1.797506 2.192849 2.324125 2.452400 81 82 83 84 85 86 87 88 2.447771 1.808199 2.218105 2.726047 3.264196 2.718168 2.564662 2.637949 89 90 91 92 93 94 95 96 2.786378 2.232006 3.969235 3.157307 3.190104 2.884429 2.902176 3.205373 97 98 99 100 101 102 103 104 2.686765 3.442073 3.062016 2.739194 3.543162 3.227347 3.080914 2.879324 105 106 107 108 109 110 111 112 2.931688 3.014648 2.934751 2.693717 2.697033 2.152225 2.795202 3.021492 113 114 115 116 117 118 119 120 3.339146 2.721587 2.718361 2.911795 2.752519 2.691813 2.477078 2.443119 121 122 123 124 125 126 127 128 2.616142 2.748565 3.507968 2.765985 2.453936 2.588919 3.058854 3.009650 129 130 131 132 133 134 135 136 3.341896 2.857260 3.685987 3.078537 2.836106 3.049870 3.243501 4.107145 137 138 139 140 141 142 143 144 3.388996 3.377629 2.855649 3.525699 2.668773 2.839951 4.695914 2.940520 145 146 147 148 149 150 151 152 2.658797 3.613976 3.380808 2.874646 2.918844 3.183168 3.047389 2.917209 153 154 155 156 157 158 159 160 2.853647 2.814868 2.689190 2.929903 2.819245 2.897497 3.026851 1.960749 161 162 163 164 165 166 167 168 3.232878 2.924044 2.683929 1.445140 3.301872 3.324491 2.859232 3.509815 169 170 171 172 173 174 175 176 3.565078 2.632761 2.657132 3.200832 2.429256 3.096971 3.084250 2.747570 177 178 179 180 181 182 183 184 3.038894 2.966126 2.752971 2.852113 2.764577 2.748323 3.119684 3.142859 185 186 187 188 189 190 191 192 3.300987 3.074083 3.321083 3.500191 3.104786 3.368456 2.745273 3.129881 193 194 195 196 197 198 199 200 3.295007 3.512719 3.308466 2.949229 3.028395 3.126988 3.180377 3.078099 201 202 203 204 205 206 207 208 3.464911 2.942704 3.385311 3.376191 3.091492 3.386392 2.553818 2.864565 209 210 211 212 2.782941 2.027829 3.197801 3.582585 > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "HD")$w 1 2 3 4 5 6 7 8 2.681468 3.076981 3.042623 1.808642 2.452578 2.283707 3.204687 2.687052 9 10 11 12 13 14 15 16 2.789008 1.790538 2.431798 2.526383 3.087484 2.526758 1.967346 3.201856 17 18 19 20 21 22 23 24 2.437266 2.451342 2.754574 3.429130 2.783253 3.184503 2.818022 3.102804 25 26 27 28 29 30 31 32 3.051450 3.812017 3.092792 2.993012 2.686981 3.171939 2.982870 2.050423 33 34 35 36 37 38 39 40 1.665926 2.410409 1.462885 2.554110 3.184800 3.649806 1.332758 1.202295 41 42 43 44 45 46 47 48 2.078419 2.624785 1.948806 2.622352 1.965129 2.699708 2.469763 2.498807 49 50 51 52 53 54 55 56 2.785012 1.775378 2.451873 1.637390 2.141537 2.394819 1.963448 2.212869 57 58 59 60 61 62 63 64 1.612253 2.236394 2.441174 2.634334 2.119316 2.204498 2.987769 2.293321 65 66 67 68 69 70 71 72 1.743460 3.000772 1.928167 1.321891 1.855573 2.415835 1.621270 2.626465 73 74 75 76 77 78 79 80 2.733414 2.627607 1.954148 2.409171 1.803818 2.205931 2.334165 2.457261 81 82 83 84 85 86 87 88 2.459281 1.816236 2.239850 2.722979 3.264448 2.732133 2.566165 2.654812 89 90 91 92 93 94 95 96 2.804466 2.222381 3.907228 3.161015 3.193989 2.889862 2.906660 3.197969 97 98 99 100 101 102 103 104 2.715906 3.431593 3.099160 2.752398 3.516772 3.227217 3.093236 2.883399 105 106 107 108 109 110 111 112 2.864890 2.991270 2.937355 2.688570 2.702904 2.159563 2.802909 3.024173 113 114 115 116 117 118 119 120 3.326291 2.723279 2.725254 2.904986 2.751915 2.768289 2.496305 2.447430 121 122 123 124 125 126 127 128 2.620222 2.754763 3.463229 2.770018 2.458681 2.577946 3.024871 3.000575 129 130 131 132 133 134 135 136 3.326351 2.860931 3.670703 3.074636 2.842019 3.059355 3.257570 4.093291 137 138 139 140 141 142 143 144 3.406198 3.400161 2.853136 3.530597 2.676593 2.842844 4.612760 2.957131 145 146 147 148 149 150 151 152 2.683639 3.606638 3.381621 2.879249 2.943487 3.192236 3.058078 2.918526 153 154 155 156 157 158 159 160 2.863004 2.837154 2.694300 2.930582 2.825569 2.913100 3.037807 1.966536 161 162 163 164 165 166 167 168 3.233641 2.924567 2.697783 1.375587 3.324437 3.390321 2.866920 3.498928 169 170 171 172 173 174 175 176 3.555728 2.652026 2.674834 3.203937 2.428780 3.098110 3.098161 2.801939 177 178 179 180 181 182 183 184 3.043763 2.976010 2.755943 2.857291 2.766139 2.756980 3.106651 3.123648 185 186 187 188 189 190 191 192 3.292540 3.072884 3.317184 3.464502 3.097491 3.332626 2.773378 3.124060 193 194 195 196 197 198 199 200 3.289734 3.504085 3.293667 2.960207 3.028003 3.112352 3.164516 3.074653 201 202 203 204 205 206 207 208 3.310860 2.952836 3.388890 3.374301 3.087964 3.386285 2.572405 2.872599 209 210 211 212 2.803556 2.046527 3.225446 3.561304 > > 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.643877 3.031579 3.006569 2.006002 2.444495 2.223819 3.264966 2.591692 9 10 11 12 13 14 15 16 2.750875 1.808531 2.374653 2.390005 3.064381 2.485790 1.909315 3.199505 17 18 19 20 21 22 23 24 2.365045 2.395871 2.710441 3.569875 2.792197 3.253348 2.717807 3.069008 25 26 27 28 29 30 31 32 3.072103 3.776131 3.075901 3.030425 2.668081 3.161443 2.907758 1.965196 33 34 35 36 37 38 39 40 1.579410 2.348264 1.407733 2.482136 3.148334 4.182552 1.348900 1.297351 41 42 43 44 45 46 47 48 2.089175 2.439885 2.081418 2.522846 2.069721 2.642469 2.413108 2.472134 49 50 51 52 53 54 55 56 2.791548 1.993787 2.463571 1.738296 2.275500 2.442238 2.060978 2.168732 57 58 59 60 61 62 63 64 1.652645 2.288576 2.468169 2.639222 2.285587 2.274409 2.912758 2.432813 65 66 67 68 69 70 71 72 1.904025 2.995160 2.122574 1.346786 2.187702 2.366695 1.786802 2.553068 73 74 75 76 77 78 79 80 2.724131 2.597676 1.988065 2.286334 1.989632 2.185193 2.222544 2.297791 81 82 83 84 85 86 87 88 2.318961 1.965621 2.240313 2.651330 3.211563 2.666269 2.546632 2.605848 89 90 91 92 93 94 95 96 2.761129 2.047974 4.172360 3.199899 3.235743 2.910134 2.884095 3.238270 97 98 99 100 101 102 103 104 2.708183 3.526914 3.081721 2.721364 3.580476 3.272385 3.157073 2.844091 105 106 107 108 109 110 111 112 2.623555 2.964375 2.855244 2.656730 2.621315 2.080652 2.817784 2.952845 113 114 115 116 117 118 119 120 3.371023 2.666638 2.703942 2.938302 2.746350 2.845069 2.570774 2.433745 121 122 123 124 125 126 127 128 2.595001 2.677158 3.453757 2.766734 2.391765 2.353419 2.989890 2.882863 129 130 131 132 133 134 135 136 3.279728 2.771391 3.821702 2.977309 2.739739 3.019753 3.305277 4.448734 137 138 139 140 141 142 143 144 3.407298 3.494642 2.618938 3.644527 2.533205 2.733391 4.795708 2.924368 145 146 147 148 149 150 151 152 2.618219 3.902314 3.520567 2.739242 2.870971 3.254510 3.027277 2.811709 153 154 155 156 157 158 159 160 2.848604 2.742076 2.578175 2.849792 2.794819 2.846444 3.054633 1.951833 161 162 163 164 165 166 167 168 3.292248 2.810913 2.668969 1.305837 3.405733 3.429620 2.772848 3.692208 169 170 171 172 173 174 175 176 3.844898 2.579311 2.612037 3.274951 2.497079 3.126838 3.117136 2.688573 177 178 179 180 181 182 183 184 3.099045 3.006852 2.679241 2.774243 2.689493 2.692801 2.979644 3.073037 185 186 187 188 189 190 191 192 3.284775 3.081311 3.403660 3.449627 3.055647 3.251958 2.724089 3.102341 193 194 195 196 197 198 199 200 3.308129 3.621492 3.304725 2.929317 2.979176 3.049054 3.273624 3.059678 201 202 203 204 205 206 207 208 3.258294 2.885526 3.577994 3.462692 3.005615 3.539461 2.551722 2.757119 209 210 211 212 2.761359 2.033532 3.341848 3.703379 > > proc.time() user system elapsed 0.18 0.06 0.23