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 1.75275960 -0.01809693 2.19477450 2.84669843 2.18663158 2.72946673 7 8 9 10 11 12 1.84487533 2.84158597 3.15298413 2.82590243 2.38326689 2.41718794 13 14 15 16 17 18 3.77347693 3.11724444 3.23951312 3.19862550 3.71800836 3.14362048 19 20 21 22 23 24 3.42010357 3.44943820 3.08757710 2.88611532 2.87162024 3.50661491 25 26 27 28 29 30 3.35088614 2.78384071 3.48106171 3.77791501 6.14607523 4.70895877 31 32 33 34 35 36 4.52324321 2.86264406 3.12353609 3.24485873 2.91407664 2.65312739 37 38 39 40 41 42 4.05557254 3.00770824 2.46589415 2.99512784 2.15042707 1.94531852 43 44 45 46 47 48 3.14048752 3.42557902 2.66506922 3.05806450 2.86384704 2.41595654 49 50 51 52 53 54 3.42935696 2.68767368 2.62568067 2.36410878 3.62599811 2.83787097 55 56 57 58 59 60 3.23740231 2.81871786 2.77145568 2.74757749 3.09764051 1.89037538 61 62 63 64 65 66 2.75656495 2.07632670 3.42282371 2.99547216 2.57748176 3.16558265 67 68 69 70 71 72 3.35194402 2.35887748 3.95353478 3.56584011 3.42301965 3.80827777 73 74 75 76 77 78 3.23054317 3.00104439 1.28139646 3.04944895 3.40663182 2.65003373 79 80 81 82 83 84 3.45905292 2.98445278 3.45110300 3.29081376 3.93332965 3.37400265 85 86 87 88 89 90 2.93083633 3.07053121 2.32217962 2.88295733 2.62894905 3.94206203 91 92 93 94 95 96 3.31988701 3.13204971 3.32632659 0.33800389 3.41143662 2.92117265 97 98 99 100 101 102 3.01838829 2.28044263 3.11332020 3.64147909 3.37918066 3.35634201 103 104 105 106 107 108 3.93317093 3.23630048 4.17356402 3.55813938 3.65217902 2.82953093 109 110 111 112 113 114 3.60771446 3.12331009 3.79381728 3.31843076 3.25816322 3.96478080 115 116 117 118 119 120 3.53075456 3.31261395 3.48379925 3.75551840 4.14775731 3.83436637 121 122 123 124 125 126 3.99079972 3.60821027 4.00608317 3.85648895 3.22344002 3.37788523 127 128 129 130 131 132 3.01605641 3.39362123 2.96676027 3.55166188 3.68258795 4.01443067 133 134 135 136 137 138 3.45045677 3.80207235 3.18556213 3.39587139 3.80122836 3.31217177 139 140 141 142 143 144 3.03475703 3.13372672 3.42841494 3.19978367 3.49355364 3.59107815 145 146 147 148 149 150 3.48384067 3.30881113 3.15151923 3.24090258 3.55061702 2.94814846 151 152 153 154 155 156 3.16078289 3.72969456 3.33800371 3.57537857 3.26531050 3.39414574 157 158 159 160 161 162 3.21919619 3.23169781 3.17723233 3.52034435 3.31120389 4.09846936 163 164 165 166 167 168 3.87796073 3.81616492 4.39250321 3.88241628 3.84398513 3.92186207 169 170 171 172 173 174 3.86959885 3.53859746 3.15252423 3.68055345 3.47634051 3.39306457 175 176 177 178 179 180 3.67725332 3.96199712 3.17220857 4.09666757 3.70110149 3.71319002 181 182 3.67037653 3.37944807 > > 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 1.9444679 0.3428297 2.2158701 2.8034725 2.2041917 2.6816834 1.9468503 2.8038960 9 10 11 12 13 14 15 16 3.0878118 2.8071317 2.3730176 2.4283446 3.6973078 3.1045015 3.2226884 3.2371359 17 18 19 20 21 22 23 24 3.6799688 3.1281677 3.4369550 3.4583125 3.0713990 2.8348410 2.6741176 3.5551721 25 26 27 28 29 30 31 32 3.3590412 2.7989832 3.4735905 3.8276029 6.2889925 5.4055955 4.7521338 2.7258783 33 34 35 36 37 38 39 40 2.6925422 3.0195700 2.9699041 2.6456209 3.7851519 2.6476937 2.3986512 3.0693787 41 42 43 44 45 46 47 48 2.1768961 2.0646460 3.0214373 3.4898030 2.7139207 3.1142168 2.9001296 2.3729973 49 50 51 52 53 54 55 56 3.4777448 2.5400174 2.4988557 2.3124550 3.7159636 2.7902010 3.2491431 2.8369853 57 58 59 60 61 62 63 64 2.7335850 2.7719999 3.1545881 2.0109126 2.6097614 2.0626301 3.5256275 2.9277720 65 66 67 68 69 70 71 72 2.2420160 3.0838449 3.4558672 2.3241109 4.0548006 3.6117775 3.4551822 3.8709712 73 74 75 76 77 78 79 80 3.2464506 2.9598812 1.5937814 3.0832390 3.3022175 2.5673344 3.4883199 2.7224695 81 82 83 84 85 86 87 88 3.4126161 3.3694215 3.9169608 3.4144241 2.8101239 2.7270857 2.2486778 2.8188083 89 90 91 92 93 94 95 96 2.6733420 3.9190394 3.4022556 3.0826125 3.1139137 0.6219324 3.4594743 2.9664571 97 98 99 100 101 102 103 104 3.0817780 2.3197148 3.1283524 3.5627567 3.4946228 3.2731534 3.7874107 2.9113265 105 106 107 108 109 110 111 112 4.1889309 3.5801771 3.7034623 2.5740808 3.6478538 2.9904832 3.8188350 3.1944607 113 114 115 116 117 118 119 120 3.3325134 4.0199454 3.5149904 3.4080874 3.5855278 3.7693165 4.2050782 3.7823883 121 122 123 124 125 126 127 128 3.8529492 3.6835525 4.2099524 3.9846845 3.3246847 3.4012115 3.0922960 3.4123074 129 130 131 132 133 134 135 136 2.9609746 3.5971163 3.6862282 4.1249601 3.5465667 3.8432301 3.1759796 3.4731332 137 138 139 140 141 142 143 144 3.9355408 3.3192497 3.0490501 3.1670031 3.5253274 3.3077701 3.5921721 3.6413411 145 146 147 148 149 150 151 152 3.5869162 3.3391522 3.0590242 3.0951314 3.5706042 2.6252942 3.0839052 3.8003336 153 154 155 156 157 158 159 160 3.3494699 3.4390503 3.1343522 3.3704390 3.1734387 3.1264831 3.2840663 3.5856035 161 162 163 164 165 166 167 168 3.3447097 4.0992760 3.9853382 3.8547595 4.2860689 3.9607817 3.8719970 4.0213601 169 170 171 172 173 174 175 176 4.0359220 3.5958044 3.2138756 3.7662033 3.5194000 3.4083867 3.7142659 3.8944506 177 178 179 180 181 182 3.2157253 3.5812908 3.8509093 3.7405082 3.5903108 3.3506997 > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "EL")$w 1 2 3 4 5 6 7 8 2.0119886 0.7364158 2.1427821 2.7296629 2.1490492 2.6035885 1.9004362 2.7398136 9 10 11 12 13 14 15 16 2.9486057 2.7684473 2.2743241 2.4145243 3.5527465 3.0705729 3.2060781 3.2693374 17 18 19 20 21 22 23 24 3.5841894 3.1038572 3.4024568 3.4500871 3.0678486 2.8074501 2.4898301 3.5549445 25 26 27 28 29 30 31 32 3.3388548 2.7834614 3.4450038 3.6807288 6.3628594 6.5576742 4.8270682 2.6082976 33 34 35 36 37 38 39 40 2.3538195 2.7395644 2.9908778 2.7023138 3.3048879 2.3744109 2.3827523 3.1943506 41 42 43 44 45 46 47 48 2.1672493 2.1764367 2.8512045 3.5334600 2.8339831 3.2376733 3.0856994 2.4324813 49 50 51 52 53 54 55 56 3.5097798 2.5257059 2.5636647 2.2558379 3.7693393 2.7405913 3.2943532 2.7970574 57 58 59 60 61 62 63 64 2.6764165 2.8199627 3.1773207 2.0576893 2.3908569 2.2620792 3.6363346 2.8934796 65 66 67 68 69 70 71 72 2.0248914 2.9866225 3.5701120 2.1180317 4.1864836 3.6489515 3.5008914 3.8668732 73 74 75 76 77 78 79 80 3.2459733 2.9057921 1.7575777 3.0918496 3.1463240 2.4983148 3.4013292 2.5119018 81 82 83 84 85 86 87 88 3.3514879 3.4643065 3.9561295 3.4465355 2.6730583 2.4606823 2.2119335 2.6914427 89 90 91 92 93 94 95 96 2.6639613 3.7738727 3.4900589 3.0356454 2.8779465 0.9591397 3.5165993 3.0090538 97 98 99 100 101 102 103 104 3.1893231 2.5130315 3.1144692 3.4805937 3.6152494 3.1095291 3.5477077 2.5916922 105 106 107 108 109 110 111 112 4.2321676 3.6316283 3.6931326 2.3504445 3.6307544 2.9535158 3.8164958 3.2477303 113 114 115 116 117 118 119 120 3.3917437 4.1309170 3.3160559 3.5358719 3.8547626 3.8183718 4.3260483 3.8536758 121 122 123 124 125 126 127 128 4.0261164 3.7008758 4.3868630 4.0404279 3.4001799 3.3927704 3.1911561 3.3514717 129 130 131 132 133 134 135 136 2.9150015 3.5924368 3.6734237 4.2857922 3.6840034 4.0151529 3.1142087 3.6322302 137 138 139 140 141 142 143 144 4.0678837 3.3350511 3.0990226 3.1991705 3.6427310 3.4269336 3.6681158 3.6186479 145 146 147 148 149 150 151 152 3.6142652 3.3728685 2.9616675 3.0152857 3.6012501 2.4740391 3.0136751 3.8519138 153 154 155 156 157 158 159 160 3.4123412 3.3000804 2.9813081 3.2965109 3.1238128 3.0137914 3.2773182 3.6376657 161 162 163 164 165 166 167 168 3.3483374 4.1726934 4.0703546 3.8896836 4.2569397 4.0636976 3.8954461 4.1689053 169 170 171 172 173 174 175 176 4.1391237 3.6310497 3.1926432 3.8138349 3.5556313 3.4274658 3.7484585 3.9396282 177 178 179 180 181 182 3.2599084 2.8918837 4.0473012 3.7884247 3.6084170 3.3794070 > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "HD")$w 1 2 3 4 5 6 7 8 2.0108702 0.5499127 2.1823880 2.7619522 2.1608259 2.6194876 1.9154665 2.7580094 9 10 11 12 13 14 15 16 3.0126698 2.7780026 2.2984410 2.4175516 3.6462670 3.0778070 3.2164611 3.2459470 17 18 19 20 21 22 23 24 3.6416498 3.1249765 3.4012884 3.4560942 3.0722817 2.8214299 2.5566117 3.5393981 25 26 27 28 29 30 31 32 3.3331684 2.7891790 3.4791397 3.7615878 6.3129644 5.9391818 4.7908087 2.6713698 33 34 35 36 37 38 39 40 2.5455303 2.8652704 2.9672171 2.7058568 3.5517928 2.5079587 2.4281740 3.1152987 41 42 43 44 45 46 47 48 2.1699740 2.1617964 2.9297015 3.4949324 2.7965033 3.1963261 3.0284935 2.4347378 49 50 51 52 53 54 55 56 3.4931690 2.5747534 2.5701481 2.2665791 3.7352812 2.7668435 3.2515271 2.7918998 57 58 59 60 61 62 63 64 2.6892682 2.7871234 3.1408949 2.0570334 2.4610592 2.2376150 3.5639214 2.8995489 65 66 67 68 69 70 71 72 2.1259967 3.0267698 3.5025578 2.1792717 4.1505988 3.6246300 3.4859527 3.8452956 73 74 75 76 77 78 79 80 3.2403352 2.9300542 1.6770987 3.0874401 3.2279231 2.5308880 3.4137298 2.5981560 81 82 83 84 85 86 87 88 3.3618414 3.4219602 3.9547976 3.4360317 2.7259982 2.5607996 2.2202443 2.7335992 89 90 91 92 93 94 95 96 2.6461330 3.8576039 3.4343773 3.0571067 2.9715473 0.7991305 3.4885475 2.9951310 97 98 99 100 101 102 103 104 3.1389003 2.4680567 3.0958215 3.5404714 3.5422154 3.1633176 3.6805929 2.7388683 105 106 107 108 109 110 111 112 4.2459482 3.6001129 3.6798530 2.4431318 3.6553925 2.9634298 3.8319123 3.2441252 113 114 115 116 117 118 119 120 3.3570484 4.1278521 3.4430231 3.4210419 3.7246885 3.7997087 4.3170513 3.8419657 121 122 123 124 125 126 127 128 4.0101333 3.6698925 4.3105546 3.9906449 3.3337030 3.3811707 3.1472682 3.3602820 129 130 131 132 133 134 135 136 2.9192002 3.5812088 3.7081882 4.2359436 3.6234992 3.9783297 3.1343941 3.5640886 137 138 139 140 141 142 143 144 4.0122433 3.3267586 3.0842998 3.1822067 3.5854799 3.3597006 3.6094325 3.6250575 145 146 147 148 149 150 151 152 3.5614059 3.3558586 3.0066503 3.0577447 3.6032704 2.5164659 3.0410543 3.8442528 153 154 155 156 157 158 159 160 3.4013680 3.3683275 3.0456583 3.3237019 3.1502398 3.0465544 3.2114002 3.6072253 161 162 163 164 165 166 167 168 3.3205565 4.1577467 4.0333515 3.8687708 4.3286331 4.0200035 3.8966635 4.1190716 169 170 171 172 173 174 175 176 4.0748104 3.6138272 3.2016603 3.7745180 3.5281314 3.4175340 3.7279276 3.9462355 177 178 179 180 181 182 3.2367046 3.2407006 3.9542544 3.7683969 3.6177151 3.3724323 > > 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.0051822 0.5306601 2.2134569 2.7840461 2.2081593 2.6649472 1.9882236 2.7940984 9 10 11 12 13 14 15 16 3.0319713 2.8040754 2.3753642 2.4392963 3.6328479 3.0996441 3.2040574 3.2552075 17 18 19 20 21 22 23 24 3.6450260 3.1117398 3.4414746 3.4581774 3.0622795 2.8234386 2.6012880 3.5750370 25 26 27 28 29 30 31 32 3.3632249 2.8015857 3.4517632 3.8033926 6.3378976 5.9243714 4.9037030 2.6621488 33 34 35 36 37 38 39 40 2.4942273 2.8937422 2.9972648 2.6352522 3.5707624 2.5130246 2.3706163 3.1048260 41 42 43 44 45 46 47 48 2.1975495 2.1114956 2.9453200 3.5245158 2.7270432 3.1311448 2.9071595 2.3602566 49 50 51 52 53 54 55 56 3.4993881 2.4861945 2.4562722 2.2956317 3.7580035 2.7636763 3.2557173 2.8509845 57 58 59 60 61 62 63 64 2.7208789 2.7886212 3.1856132 2.0562179 2.5427915 2.0537435 3.5807170 2.9136847 65 66 67 68 69 70 71 72 2.0983335 3.0462646 3.5134312 2.2963222 4.1118955 3.6393429 3.4733049 3.9133485 73 74 75 76 77 78 79 80 3.2528365 2.9304109 1.6943967 3.0947564 3.2198425 2.5419853 3.4975277 2.6229785 81 82 83 84 85 86 87 88 3.4045577 3.4098100 3.9081636 3.4298031 2.7522393 2.5963201 2.2318288 2.7893624 89 90 91 92 93 94 95 96 2.7004902 3.8759405 3.4463406 3.0567872 2.9958046 0.7823828 3.4852953 2.9778978 97 98 99 100 101 102 103 104 3.1072933 2.3287754 3.1463821 3.4961014 3.5545777 3.2239342 3.6819024 2.7342701 105 106 107 108 109 110 111 112 4.1868435 3.5950025 3.7286066 2.4640993 3.6589600 2.9300389 3.8265167 3.1228123 113 114 115 116 117 118 119 120 3.3619843 4.0444571 3.4564691 3.4691095 3.6327394 3.7834022 4.2293906 3.7545663 121 122 123 124 125 126 127 128 3.7806912 3.7294575 4.3348014 4.0634907 3.3825869 3.4144058 3.1263497 3.4178325 129 130 131 132 133 134 135 136 2.9601173 3.6199655 3.6748647 4.1920437 3.5936331 3.8648146 3.1692989 3.5076377 137 138 139 140 141 142 143 144 4.0069061 3.3269091 3.0695014 3.1821026 3.5706546 3.3620125 3.6474392 3.6518401 145 146 147 148 149 150 151 152 3.6495910 3.3524476 3.0133434 3.0243553 3.5715347 2.4979955 3.0477682 3.8274809 153 154 155 156 157 158 159 160 3.3598569 3.3746340 3.0763047 3.3583964 3.1531515 3.0786620 3.3543509 3.6198242 161 162 163 164 165 166 167 168 3.3694072 4.1018644 4.0474054 3.8810560 4.1990834 4.0093592 3.8838964 4.0797749 169 170 171 172 173 174 175 176 4.1318062 3.6226613 3.2323046 3.8158849 3.5482513 3.4182035 3.7362980 3.8560779 177 178 179 180 181 182 3.2348013 3.2353477 3.9376856 3.7574848 3.5558472 3.3380851 > > proc.time() user system elapsed 0.20 0.07 0.23