R Under development (unstable) (2024-09-23 r87189 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.0930709 3.0052064 2.4329013 1.4333151 2.7056431 2.7275187 3.0286618 2.2465587 9 10 11 12 13 14 15 16 1.5966567 3.2489487 2.0673327 2.9358016 3.4981428 2.9671311 3.5247403 3.2172541 17 18 19 20 21 22 23 24 2.7282447 3.2011559 3.9197163 3.2659342 3.8406490 2.5191073 3.7693776 3.1263322 25 26 27 28 29 30 31 32 2.3874250 3.7377878 4.4039362 1.9080970 3.0986180 2.2099813 1.9360556 6.7423994 33 34 35 36 37 38 39 40 1.0282183 0.7519563 2.5421522 2.3756533 2.6278951 1.9102872 2.5600144 2.4776134 41 42 43 44 45 46 47 48 2.2527490 1.9276147 2.1879650 2.3481200 2.6013669 1.1999596 1.7060782 2.1816120 49 50 51 52 53 54 55 56 3.0770919 2.0395267 2.2333870 1.8527591 2.5258089 1.5896175 2.8419002 2.7727219 57 58 59 60 61 62 63 64 1.8482759 1.8296883 3.2908316 2.2556730 2.1434283 2.6335097 1.4578207 2.4055302 65 66 67 68 69 70 71 72 2.3900541 2.1273382 2.3660709 2.6365271 2.2671919 2.5841301 2.5672175 2.5267264 73 74 75 76 77 78 79 80 2.7784575 3.3388760 1.4365910 3.2409008 3.2576948 3.9488295 3.4138393 3.6036929 81 82 83 84 85 86 87 88 3.0090703 3.2755982 2.9297531 3.2811671 3.3348847 3.1271316 3.0595343 4.0866860 89 90 91 92 93 94 95 96 3.1539639 3.5993569 3.0245687 1.8284574 3.2142312 2.5593128 3.1191956 3.7073271 97 98 99 100 101 102 103 104 3.1051928 2.3177668 3.1147542 3.2968151 3.1040377 3.1721072 3.3591601 2.2483454 105 106 107 108 109 110 111 112 3.2152876 2.6017371 2.6893868 3.0144561 1.0572865 3.0385994 2.9780773 2.8734996 113 114 115 116 117 118 119 120 2.8013181 2.7953767 2.9502148 2.9020235 2.8932257 2.6094390 2.3255705 2.2195690 121 122 123 124 125 126 127 128 3.8640899 2.8781060 2.5623874 2.6558975 2.8008859 2.7369632 1.8531270 1.9398175 129 130 131 132 133 134 135 136 2.6798211 3.0332911 2.9629607 2.9277415 2.7271918 3.1563810 2.7925574 3.4570233 137 138 139 140 141 142 143 144 3.0215668 3.0266372 2.9031593 3.0638812 3.3711128 2.6723929 2.6908461 2.6861106 145 146 147 148 149 150 151 152 2.7824663 3.0738544 2.3189332 3.1955695 2.8392732 3.1419438 2.8270118 3.2704338 153 154 155 156 157 158 159 160 2.9251918 3.4512287 2.9947745 2.8124282 3.2571104 2.6575802 2.6323911 3.4049065 161 162 163 164 165 166 167 168 3.2498752 3.1140857 3.4762374 3.1401139 2.7542077 2.6069942 2.6029221 2.6522144 169 170 171 172 173 174 175 176 3.8503554 3.9084183 3.3834204 3.1804845 3.7705184 3.8838228 3.6373114 3.4341207 177 178 179 180 181 182 183 184 3.3642694 3.5507243 2.1060160 3.0009422 4.0071732 3.3093497 3.3703336 3.3556843 185 186 187 188 189 190 191 192 3.1040951 3.4104252 3.2687821 3.3683230 3.4141450 3.2118833 3.1398036 3.2746212 193 194 195 196 197 198 199 200 1.8818214 2.7887755 1.8596043 2.9552314 3.5695751 3.2191167 3.3457829 2.8300192 201 202 203 204 205 206 2.9584519 3.2435543 2.4854630 3.3452850 3.7375913 3.3508553 > > 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.080215 2.880703 2.359348 1.598016 2.600175 2.648530 2.912001 2.310117 9 10 11 12 13 14 15 16 1.690641 3.226554 2.026871 2.886704 3.474044 2.751137 3.477631 3.209068 17 18 19 20 21 22 23 24 2.677264 3.141675 4.057658 3.288198 3.905612 2.520846 3.903502 3.182538 25 26 27 28 29 30 31 32 2.378808 3.843414 3.943872 1.888304 2.881443 2.025345 1.960273 7.258847 33 34 35 36 37 38 39 40 1.411553 1.040151 2.273906 2.372510 2.591724 1.947606 2.498635 2.422569 41 42 43 44 45 46 47 48 2.244882 1.791625 2.151979 2.320038 2.585680 1.424936 1.695449 2.178581 49 50 51 52 53 54 55 56 3.073775 2.036916 2.164338 1.867215 2.505069 1.644161 2.580940 2.754518 57 58 59 60 61 62 63 64 1.833846 1.727185 3.254003 2.220962 1.998757 2.559353 1.592430 2.302324 65 66 67 68 69 70 71 72 2.353936 2.119221 2.337387 2.538648 2.224241 2.552682 2.558808 2.439660 73 74 75 76 77 78 79 80 2.717498 3.265636 1.676209 3.224212 3.363869 4.115015 3.420632 3.552684 81 82 83 84 85 86 87 88 3.111025 3.356523 2.929974 3.212211 3.334311 3.156344 3.070654 4.135155 89 90 91 92 93 94 95 96 3.171143 3.631318 3.004425 1.976850 3.214002 2.559970 3.067840 3.708964 97 98 99 100 101 102 103 104 3.069704 2.259425 3.133202 3.279231 3.126930 3.129460 3.340164 2.205454 105 106 107 108 109 110 111 112 3.134750 2.574203 2.600031 3.031677 1.367308 3.090216 2.907090 2.856246 113 114 115 116 117 118 119 120 2.826540 2.779844 2.882057 2.877383 2.864271 2.598546 2.359345 2.232851 121 122 123 124 125 126 127 128 3.960038 2.823814 2.531161 2.628952 2.712636 2.727556 1.806155 2.082534 129 130 131 132 133 134 135 136 2.668422 2.961076 2.919703 2.881688 2.646326 3.088307 2.767985 3.424870 137 138 139 140 141 142 143 144 3.005982 2.999519 2.879441 3.048692 3.221279 2.634768 2.683830 2.670102 145 146 147 148 149 150 151 152 2.719877 3.084091 2.358273 3.171491 2.737610 3.117387 2.826832 3.262642 153 154 155 156 157 158 159 160 2.920301 3.512286 2.929244 2.792975 3.285637 2.641205 2.647810 3.453611 161 162 163 164 165 166 167 168 3.240587 3.090551 3.499237 3.140840 2.746816 2.609923 2.602398 2.604053 169 170 171 172 173 174 175 176 4.107097 4.112460 3.460952 3.198359 3.970355 4.041506 3.746238 3.551719 177 178 179 180 181 182 183 184 3.414287 3.596278 2.143950 2.951192 4.195772 3.318173 3.328238 3.320433 185 186 187 188 189 190 191 192 3.022380 3.309000 3.271966 3.373332 3.410770 3.196948 3.175053 3.255861 193 194 195 196 197 198 199 200 2.110968 2.840506 2.143225 2.952150 3.589787 3.203111 3.354650 2.814346 201 202 203 204 205 206 2.955446 3.194443 2.458906 3.344775 3.762884 3.348021 > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "EL")$w 1 2 3 4 5 6 7 8 2.194218 2.861144 2.353118 1.707089 2.577286 2.565879 2.873290 2.315232 9 10 11 12 13 14 15 16 1.714757 3.235085 1.972970 2.823674 3.436819 2.579122 3.344624 3.167484 17 18 19 20 21 22 23 24 2.639581 3.056761 4.425169 3.284355 4.014908 2.527946 4.154031 3.174844 25 26 27 28 29 30 31 32 2.377826 4.117455 3.359345 1.863487 2.686139 2.003324 2.010668 7.646806 33 34 35 36 37 38 39 40 1.645720 1.183629 2.216027 2.338141 2.541031 1.949366 2.438094 2.405353 41 42 43 44 45 46 47 48 2.237866 1.774523 2.207756 2.328709 2.588817 1.533856 1.772245 2.200419 49 50 51 52 53 54 55 56 3.027994 2.069981 2.234413 1.920973 2.532505 1.713429 2.663866 2.720235 57 58 59 60 61 62 63 64 2.000545 1.752865 3.169416 2.174918 1.942910 2.453401 1.691051 2.237427 65 66 67 68 69 70 71 72 2.296823 2.078795 2.272620 2.453603 2.207773 2.464303 2.513898 2.366053 73 74 75 76 77 78 79 80 2.696562 3.191735 1.704990 3.157110 3.465384 4.220095 3.396919 3.384951 81 82 83 84 85 86 87 88 3.168723 3.386775 2.872899 3.250238 3.332266 3.159421 3.052973 4.165221 89 90 91 92 93 94 95 96 3.162217 3.649798 3.039222 1.972094 3.222893 2.536945 3.052233 3.686347 97 98 99 100 101 102 103 104 3.023179 2.191343 3.121366 3.228660 3.106459 3.114964 3.245408 2.190192 105 106 107 108 109 110 111 112 3.098281 2.582916 2.562807 3.062998 1.507144 3.087749 2.814152 2.833853 113 114 115 116 117 118 119 120 2.805432 2.746347 3.005699 2.828014 2.793857 2.656981 2.604421 2.174365 121 122 123 124 125 126 127 128 3.959711 2.718079 2.447024 2.876171 2.795398 2.885719 1.907483 2.272777 129 130 131 132 133 134 135 136 2.680746 2.873426 2.867444 2.794119 2.546849 2.999214 2.731829 3.362667 137 138 139 140 141 142 143 144 2.949392 2.948761 2.806345 3.005903 3.040272 2.553791 2.641853 2.678579 145 146 147 148 149 150 151 152 2.621132 3.093146 2.376520 3.121368 2.623558 3.075706 2.812717 3.179121 153 154 155 156 157 158 159 160 2.919144 3.603102 2.839279 2.803284 3.306143 2.610224 2.604152 3.446625 161 162 163 164 165 166 167 168 3.196980 3.013117 3.504339 3.115517 2.729769 2.585478 2.558263 2.538817 169 170 171 172 173 174 175 176 4.298357 4.318381 3.517561 3.200591 4.242353 4.215582 3.897015 3.599002 177 178 179 180 181 182 183 184 3.434424 3.559786 2.075144 2.849329 4.261242 3.260640 3.365016 3.278794 185 186 187 188 189 190 191 192 3.015899 3.561284 3.249636 3.360436 3.504747 3.163188 3.164796 3.189392 193 194 195 196 197 198 199 200 2.187952 2.837182 2.243573 2.915309 3.652739 3.221221 3.338655 2.805433 201 202 203 204 205 206 2.936480 3.075509 2.433175 3.316654 3.753914 3.378171 > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "HD")$w 1 2 3 4 5 6 7 8 2.191226 2.862068 2.373223 1.694907 2.590127 2.593328 2.883184 2.304725 9 10 11 12 13 14 15 16 1.699780 3.239057 1.972919 2.840213 3.457749 2.645222 3.419019 3.168351 17 18 19 20 21 22 23 24 2.649293 3.088821 4.216205 3.278782 3.930648 2.520439 4.030819 3.185197 25 26 27 28 29 30 31 32 2.372110 3.989626 3.635831 1.852943 2.754035 1.988684 1.977989 7.462631 33 34 35 36 37 38 39 40 1.541238 1.109606 2.287015 2.334335 2.546345 1.943861 2.451746 2.417324 41 42 43 44 45 46 47 48 2.244397 1.797626 2.214216 2.338073 2.575824 1.518294 1.783197 2.201181 49 50 51 52 53 54 55 56 3.043914 2.074230 2.241790 1.928504 2.538285 1.710662 2.689859 2.731700 57 58 59 60 61 62 63 64 2.002649 1.773333 3.199126 2.175458 1.975911 2.464589 1.680166 2.253812 65 66 67 68 69 70 71 72 2.300380 2.077324 2.273339 2.470209 2.216179 2.462775 2.514654 2.413435 73 74 75 76 77 78 79 80 2.700638 3.214044 1.659151 3.173035 3.457122 4.212416 3.418857 3.439447 81 82 83 84 85 86 87 88 3.154545 3.366269 2.881674 3.261098 3.318207 3.158822 3.060639 4.133656 89 90 91 92 93 94 95 96 3.174151 3.674839 3.040238 1.934787 3.216386 2.546620 3.048360 3.697218 97 98 99 100 101 102 103 104 3.032102 2.194192 3.115567 3.259444 3.112534 3.117102 3.276112 2.198033 105 106 107 108 109 110 111 112 3.130138 2.595662 2.589383 3.058670 1.483852 3.084636 2.824630 2.841190 113 114 115 116 117 118 119 120 2.806264 2.751323 2.981574 2.829412 2.801329 2.636922 2.532190 2.161984 121 122 123 124 125 126 127 128 3.984020 2.734638 2.440489 2.819995 2.758367 2.846815 1.849598 2.182751 129 130 131 132 133 134 135 136 2.683244 2.906426 2.874173 2.807545 2.563534 3.029649 2.723684 3.406925 137 138 139 140 141 142 143 144 2.960532 2.967271 2.812709 3.019767 3.102035 2.559475 2.645402 2.679199 145 146 147 148 149 150 151 152 2.637966 3.101758 2.361732 3.146449 2.651266 3.085449 2.816916 3.195022 153 154 155 156 157 158 159 160 2.927136 3.618131 2.864424 2.806931 3.315073 2.610755 2.600269 3.452548 161 162 163 164 165 166 167 168 3.215985 3.026776 3.526883 3.127301 2.737355 2.585796 2.558089 2.553647 169 170 171 172 173 174 175 176 4.212600 4.242192 3.510016 3.210177 4.128092 4.109567 3.822967 3.567196 177 178 179 180 181 182 183 184 3.430020 3.554572 2.045898 2.862724 4.244122 3.269874 3.380554 3.296731 185 186 187 188 189 190 191 192 3.010412 3.565871 3.263980 3.384220 3.527264 3.179028 3.165740 3.210743 193 194 195 196 197 198 199 200 2.147936 2.832715 2.192189 2.915261 3.679065 3.240664 3.347465 2.802908 201 202 203 204 205 206 2.949225 3.099536 2.439091 3.338513 3.799273 3.397519 > > 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.064071 2.815561 2.324935 1.651004 2.549103 2.609012 2.852301 2.333302 9 10 11 12 13 14 15 16 1.728529 3.209479 2.023848 2.859050 3.456546 2.666856 3.429479 3.199961 17 18 19 20 21 22 23 24 2.651754 3.108127 4.138437 3.295261 3.935126 2.514466 3.981669 3.198498 25 26 27 28 29 30 31 32 2.370557 3.897807 3.685263 1.906671 2.791108 2.004175 1.988440 7.453095 33 34 35 36 37 38 39 40 1.536110 1.169451 2.185486 2.370221 2.575555 1.969070 2.473699 2.399819 41 42 43 44 45 46 47 48 2.245069 1.775896 2.136116 2.306561 2.583241 1.500442 1.705779 2.176004 49 50 51 52 53 54 55 56 3.063522 2.038775 2.137165 1.876239 2.490987 1.673572 2.465643 2.742053 57 58 59 60 61 62 63 64 1.826666 1.720142 3.223660 2.215604 1.969867 2.533576 1.639578 2.272534 65 66 67 68 69 70 71 72 2.345196 2.127332 2.332563 2.503445 2.213819 2.546186 2.556477 2.399025 73 74 75 76 77 78 79 80 2.685964 3.223659 1.755815 3.215824 3.410034 4.219083 3.421862 3.501695 81 82 83 84 85 86 87 88 3.150739 3.390788 2.926634 3.174106 3.328981 3.165910 3.073004 4.153311 89 90 91 92 93 94 95 96 3.175469 3.645099 2.991222 2.031331 3.204938 2.552450 3.031871 3.693320 97 98 99 100 101 102 103 104 3.047020 2.243712 3.134896 3.267903 3.135239 3.101551 3.323799 2.190506 105 106 107 108 109 110 111 112 3.087608 2.556982 2.557956 3.031831 1.456657 3.109714 2.874654 2.844691 113 114 115 116 117 118 119 120 2.833191 2.769490 2.849009 2.867323 2.852525 2.599486 2.373702 2.245257 121 122 123 124 125 126 127 128 4.017440 2.798589 2.525881 2.613823 2.679187 2.716348 1.830552 2.135953 129 130 131 132 133 134 135 136 2.665266 2.929825 2.902332 2.864214 2.614064 3.056165 2.760465 3.406567 137 138 139 140 141 142 143 144 2.998345 2.985925 2.870568 3.039441 3.152584 2.623639 2.680660 2.657523 145 146 147 148 149 150 151 152 2.696838 3.084938 2.374709 3.159202 2.696027 3.100537 2.822403 3.254801 153 154 155 156 157 158 159 160 2.914011 3.538883 2.898538 2.778951 3.295369 2.638216 2.655135 3.477367 161 162 163 164 165 166 167 168 3.234959 3.079421 3.510008 3.136623 2.741161 2.609172 2.603523 2.588041 169 170 171 172 173 174 175 176 4.282141 4.243019 3.497856 3.203687 4.096876 4.143002 3.806881 3.616509 177 178 179 180 181 182 183 184 3.438515 3.619100 2.169978 2.929202 4.326434 3.323604 3.301718 3.301089 185 186 187 188 189 190 191 192 2.985196 3.244282 3.270587 3.373280 3.399760 3.187956 3.190015 3.245808 193 194 195 196 197 198 199 200 2.181679 2.858279 2.227785 2.950550 3.592265 3.191357 3.356451 2.809234 201 202 203 204 205 206 2.952400 3.171153 2.448442 3.342223 3.773056 3.340991 > > proc.time() user system elapsed 0.21 0.03 0.23