R Under development (unstable) (2024-09-17 r87161 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.580291 2.206690 2.772009 2.768349 2.978941 2.095356 2.852709 2.734093 9 10 11 12 13 14 15 16 3.711613 3.327871 2.864174 2.236684 3.289368 2.399493 2.815071 2.059180 17 18 19 20 21 22 23 24 2.607443 2.941438 2.728440 2.726127 2.731323 2.574231 2.462656 2.320764 25 26 27 28 29 30 31 32 2.865366 4.241036 2.923262 2.582657 6.481521 3.689412 1.960742 2.693822 33 34 35 36 37 38 39 40 2.917894 1.471431 2.670209 1.872134 2.061210 2.607173 1.833991 2.221238 41 42 43 44 45 46 47 48 2.341327 2.334473 4.112804 2.430209 2.410248 2.795686 2.436314 1.883127 49 50 51 52 53 54 55 56 2.504192 2.932248 2.747961 2.642272 3.747267 2.732789 2.927643 2.749069 57 58 59 60 61 62 63 64 2.525239 2.848899 2.336528 2.695002 3.292528 3.039038 3.677735 2.794198 65 66 67 68 69 70 71 72 3.137109 2.914797 3.403422 2.505811 3.615126 3.517800 3.295730 3.673595 73 74 75 76 77 78 79 80 2.903430 4.038737 2.934091 3.168254 2.291518 2.644082 2.866550 2.955775 81 82 83 84 85 86 87 88 3.382350 2.484735 2.355696 3.256813 2.711112 3.401596 3.606226 2.695798 89 90 91 92 93 94 95 96 3.589868 2.855597 2.853473 4.092592 1.995904 3.302754 2.884638 3.274891 97 98 99 100 101 102 103 104 2.945467 3.796385 3.338181 3.095445 2.816480 2.804546 1.494855 2.728831 105 106 107 108 109 110 111 112 2.415697 3.165091 2.422663 3.269339 2.692449 2.841027 2.045059 3.192443 113 114 115 116 117 118 119 120 3.032137 3.073040 2.277715 4.133945 1.793446 3.623169 4.387742 4.265205 121 122 123 124 125 126 127 128 3.673873 1.741918 1.527557 2.680541 5.179753 3.702860 3.315357 4.149065 129 130 131 132 133 134 135 136 3.349306 2.834643 3.516578 3.628002 3.154672 3.206980 3.438012 2.554136 137 138 139 140 141 142 143 144 3.385290 3.659424 3.363477 3.608082 2.495015 2.973082 3.120270 3.062357 145 146 147 148 149 150 151 152 3.016249 3.281941 3.228676 3.694021 3.617356 2.923787 2.602857 3.598492 153 154 155 156 157 158 159 160 3.795855 2.653582 3.362638 3.170906 3.001334 4.004809 2.910214 2.537748 161 162 163 164 165 166 167 168 3.382499 3.250322 3.035020 2.537599 2.705059 3.467008 3.007078 3.902295 169 170 171 172 173 174 175 176 3.897607 3.528344 1.029831 4.756326 3.348062 4.999153 5.285317 3.685000 177 178 179 180 181 182 183 184 3.837316 3.289268 3.384191 3.502633 2.185293 1.333043 3.155962 3.566476 185 186 187 188 189 190 191 192 3.710980 3.497821 2.758930 2.867946 2.768718 3.163578 2.986497 4.489247 193 194 3.158289 3.732143 > > 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.575029 2.101554 2.813111 2.755138 2.923925 2.225978 2.830296 2.725884 9 10 11 12 13 14 15 16 3.781247 3.289585 2.856505 2.264611 3.222161 2.435198 2.781487 2.185743 17 18 19 20 21 22 23 24 2.630392 2.895502 2.715205 2.707723 2.709373 2.562634 2.515049 2.408383 25 26 27 28 29 30 31 32 2.841824 4.410910 2.681156 2.587810 6.780689 3.742717 2.155273 2.670644 33 34 35 36 37 38 39 40 2.868450 1.839731 2.688103 2.013176 2.212739 2.542643 2.012840 2.268222 41 42 43 44 45 46 47 48 2.380707 2.389880 3.954407 2.432918 2.387756 2.777568 2.459372 2.037192 49 50 51 52 53 54 55 56 2.463486 2.912863 2.682413 2.542792 3.716678 2.716987 2.946064 2.733345 57 58 59 60 61 62 63 64 2.570699 2.748624 2.362445 2.479982 3.120216 2.964379 3.632453 2.563212 65 66 67 68 69 70 71 72 3.048242 2.832833 3.208262 2.471957 3.599651 3.490450 3.243627 3.699488 73 74 75 76 77 78 79 80 2.856286 4.089811 2.865758 3.118819 2.363992 2.678548 2.847177 2.948843 81 82 83 84 85 86 87 88 3.350810 2.503368 2.395625 3.186370 2.625799 3.332482 3.556380 2.690350 89 90 91 92 93 94 95 96 3.495701 2.840973 2.840478 4.117136 2.111556 3.257608 2.865460 3.279355 97 98 99 100 101 102 103 104 2.936331 3.885870 3.332452 3.089128 2.760358 2.777625 1.755573 2.682904 105 106 107 108 109 110 111 112 2.477364 3.132535 2.437246 3.292336 2.693443 2.738055 2.162616 3.117612 113 114 115 116 117 118 119 120 2.939990 3.012320 2.327395 4.123074 1.798062 3.433838 4.467181 4.357923 121 122 123 124 125 126 127 128 3.652760 1.857466 1.664918 2.545424 5.634056 3.684245 3.274299 4.183766 129 130 131 132 133 134 135 136 3.243647 2.760513 3.473423 3.634934 3.133324 3.135886 3.409551 2.585784 137 138 139 140 141 142 143 144 3.330418 3.628473 3.287661 3.602363 2.408713 2.896971 3.013267 2.974648 145 146 147 148 149 150 151 152 2.970077 3.194084 3.155446 3.475270 3.589218 2.921431 2.564515 3.578883 153 154 155 156 157 158 159 160 3.818012 2.650724 3.358918 3.104393 2.959093 3.988589 2.869699 2.659457 161 162 163 164 165 166 167 168 3.355826 3.225353 2.970954 2.547648 2.686582 3.371642 2.994360 3.974384 169 170 171 172 173 174 175 176 3.889016 3.323049 1.521240 5.002973 3.258682 5.390903 6.041424 3.572341 177 178 179 180 181 182 183 184 3.846329 3.242938 3.342802 3.427458 2.284066 1.742818 3.127942 3.528239 185 186 187 188 189 190 191 192 3.664790 3.427972 2.759920 2.829961 2.754686 3.024794 2.912234 4.666893 193 194 3.031978 3.633981 > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "EL")$w 1 2 3 4 5 6 7 8 2.759156 2.052212 2.815887 2.730424 2.832251 2.173031 2.777131 2.675154 9 10 11 12 13 14 15 16 3.675782 3.050487 2.867362 2.320960 3.118345 2.404715 2.663998 2.368102 17 18 19 20 21 22 23 24 2.700196 2.776339 2.720376 2.695955 2.686658 2.495946 2.593468 2.520915 25 26 27 28 29 30 31 32 2.633134 4.430625 2.533695 2.527751 6.614132 3.609876 2.249166 2.546356 33 34 35 36 37 38 39 40 2.729574 1.952346 2.608309 2.116675 2.268006 2.798979 2.068246 2.343397 41 42 43 44 45 46 47 48 2.391840 2.525837 4.517111 2.430490 2.414418 2.735211 2.425745 2.140703 49 50 51 52 53 54 55 56 2.484282 2.893432 2.919937 2.865893 3.814674 2.679698 2.934108 2.678191 57 58 59 60 61 62 63 64 2.590043 2.611609 2.321068 2.610015 2.968547 2.812933 3.371715 2.631296 65 66 67 68 69 70 71 72 2.855758 2.794644 3.384616 2.437717 3.407117 3.384448 3.142469 3.640212 73 74 75 76 77 78 79 80 2.867767 4.020312 3.003289 3.117915 2.495688 2.692187 2.803219 2.897159 81 82 83 84 85 86 87 88 3.280393 2.496918 2.389576 3.199577 2.687075 3.339138 3.507252 2.709567 89 90 91 92 93 94 95 96 3.309265 2.860114 2.836608 4.248918 2.012353 3.207795 2.805868 3.200094 97 98 99 100 101 102 103 104 2.869928 3.796033 3.250478 3.007081 2.797137 2.702989 1.915152 2.618293 105 106 107 108 109 110 111 112 2.480180 3.106209 2.395784 3.195328 2.640190 2.702869 2.231557 2.977915 113 114 115 116 117 118 119 120 2.832757 2.757525 2.330002 4.086912 1.942232 3.513643 4.493420 3.979439 121 122 123 124 125 126 127 128 3.420330 2.092092 2.007175 2.620615 6.436634 3.463002 3.087342 4.019035 129 130 131 132 133 134 135 136 3.215695 2.768109 3.319730 3.543809 3.092323 2.966234 3.296123 2.590232 137 138 139 140 141 142 143 144 3.351391 3.518255 3.319134 3.449678 2.614264 2.779856 3.060165 2.918050 145 146 147 148 149 150 151 152 2.920077 3.319534 3.350106 3.612982 3.641700 2.986477 2.567507 3.547249 153 154 155 156 157 158 159 160 3.710100 2.712145 3.373474 3.165257 2.855596 3.920205 2.840514 2.620660 161 162 163 164 165 166 167 168 3.282709 3.213131 2.890287 2.675277 2.738645 3.284420 3.075618 3.911001 169 170 171 172 173 174 175 176 3.681596 3.272654 1.679522 4.606794 3.242618 5.833556 6.791427 3.629579 177 178 179 180 181 182 183 184 3.832872 3.214275 3.205919 3.497877 2.327952 1.864921 3.120320 3.426737 185 186 187 188 189 190 191 192 3.665715 3.512004 2.730490 2.746030 2.791917 3.016012 2.824805 5.459689 193 194 3.129839 3.798937 > > GEcalib(~ 0 + Xs, dweight = d, const = c(total), + method = "DS", entropy = "HD")$w 1 2 3 4 5 6 7 8 2.770851 2.083393 2.836179 2.723202 2.862921 2.185431 2.804422 2.692012 9 10 11 12 13 14 15 16 3.675125 3.095163 2.865458 2.287927 3.142542 2.404796 2.695065 2.301676 17 18 19 20 21 22 23 24 2.675335 2.792108 2.710609 2.689270 2.683828 2.520882 2.570640 2.480888 25 26 27 28 29 30 31 32 2.678613 4.347348 2.554496 2.527052 6.738180 3.664689 2.254492 2.563140 33 34 35 36 37 38 39 40 2.763773 1.936552 2.620867 2.117852 2.235725 2.801855 2.044807 2.361850 41 42 43 44 45 46 47 48 2.377377 2.536163 4.219410 2.452913 2.442325 2.737967 2.433170 2.137937 49 50 51 52 53 54 55 56 2.511120 2.892575 2.904826 2.855227 3.742327 2.674291 2.939429 2.687869 57 58 59 60 61 62 63 64 2.586272 2.648912 2.353342 2.663265 3.023229 2.847680 3.415925 2.689704 65 66 67 68 69 70 71 72 2.897537 2.796808 3.370276 2.459244 3.437018 3.400729 3.165456 3.644853 73 74 75 76 77 78 79 80 2.862063 4.036332 2.987405 3.123600 2.446356 2.684320 2.807265 2.906588 81 82 83 84 85 86 87 88 3.307049 2.484676 2.365031 3.208325 2.661039 3.358433 3.540016 2.695167 89 90 91 92 93 94 95 96 3.368118 2.853128 2.832956 4.259917 1.995979 3.229506 2.811436 3.215643 97 98 99 100 101 102 103 104 2.885365 3.815284 3.263762 3.024043 2.783646 2.711637 1.918581 2.654363 105 106 107 108 109 110 111 112 2.470771 3.111547 2.408106 3.223227 2.640967 2.710120 2.185351 3.007752 113 114 115 116 117 118 119 120 2.852640 2.803108 2.299229 4.198763 1.849716 3.565445 4.553954 4.035191 121 122 123 124 125 126 127 128 3.459845 1.988839 1.860671 2.592840 6.304099 3.509957 3.123656 4.083614 129 130 131 132 133 134 135 136 3.232499 2.758176 3.348966 3.559465 3.098769 2.994129 3.317084 2.590758 137 138 139 140 141 142 143 144 3.364249 3.553339 3.330529 3.471170 2.564015 2.797429 3.070055 2.941561 145 146 147 148 149 150 151 152 2.925695 3.314964 3.337957 3.650748 3.646641 2.973204 2.545542 3.565670 153 154 155 156 157 158 159 160 3.726161 2.690288 3.373828 3.163747 2.868566 3.890711 2.837184 2.625872 161 162 163 164 165 166 167 168 3.300863 3.216641 2.904299 2.641923 2.717949 3.313258 3.061186 3.908590 169 170 171 172 173 174 175 176 3.728954 3.317218 1.591975 4.660549 3.258456 5.762506 6.421098 3.659641 177 178 179 180 181 182 183 184 3.849648 3.225028 3.234667 3.511868 2.309928 1.801497 3.123414 3.452950 185 186 187 188 189 190 191 192 3.693479 3.524061 2.734194 2.759650 2.775840 3.037056 2.842801 4.936408 193 194 3.123271 3.827532 > > 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.574136 2.046965 2.832286 2.750874 2.901177 2.279522 2.821006 2.728381 9 10 11 12 13 14 15 16 3.799004 3.258574 2.855741 2.302594 3.191946 2.464101 2.771681 2.248411 17 18 19 20 21 22 23 24 2.645966 2.879959 2.715618 2.711473 2.708352 2.565682 2.546022 2.450349 25 26 27 28 29 30 31 32 2.835696 4.476349 2.625475 2.587198 6.861655 3.739620 2.224521 2.666497 33 34 35 36 37 38 39 40 2.850917 1.968794 2.696566 2.080725 2.273325 2.527700 2.091459 2.298165 41 42 43 44 45 46 47 48 2.401280 2.421267 3.825287 2.448264 2.396604 2.777020 2.483845 2.108033 49 50 51 52 53 54 55 56 2.463533 2.905542 2.663049 2.511902 3.693604 2.716676 2.950827 2.735894 57 58 59 60 61 62 63 64 2.594291 2.714549 2.389239 2.414537 3.034918 2.931255 3.588129 2.481861 65 66 67 68 69 70 71 72 3.008194 2.798392 3.118426 2.472152 3.571780 3.468911 3.218999 3.706217 73 74 75 76 77 78 79 80 2.843688 4.092868 2.838521 3.096586 2.401574 2.700231 2.842194 2.946608 81 82 83 84 85 86 87 88 3.335496 2.526318 2.422824 3.153096 2.606047 3.289924 3.516470 2.695819 89 90 91 92 93 94 95 96 3.449526 2.839303 2.841309 4.101533 2.162895 3.228752 2.857745 3.272388 97 98 99 100 101 102 103 104 2.932296 3.922636 3.323280 3.081936 2.740611 2.773934 1.858613 2.676246 105 106 107 108 109 110 111 112 2.510932 3.117433 2.459959 3.285718 2.698957 2.713384 2.219253 3.088031 113 114 115 116 117 118 119 120 2.897072 2.982302 2.360877 4.063208 1.855921 3.329681 4.472544 4.378340 121 122 123 124 125 126 127 128 3.621696 1.924796 1.744507 2.507999 5.924574 3.656111 3.242222 4.166222 129 130 131 132 133 134 135 136 3.189225 2.742505 3.440327 3.618317 3.116883 3.103068 3.386463 2.604468 137 138 139 140 141 142 143 144 3.300195 3.592279 3.243286 3.587730 2.385063 2.872148 2.965584 2.931294 145 146 147 148 149 150 151 152 2.952325 3.151352 3.119866 3.356301 3.560982 2.921229 2.562868 3.551885 153 154 155 156 157 158 159 160 3.808502 2.657984 3.345764 3.072183 2.940314 3.966427 2.856159 2.702027 161 162 163 164 165 166 167 168 3.338981 3.207701 2.945195 2.565800 2.686874 3.317224 2.994742 3.987174 169 170 171 172 173 174 175 176 3.863069 3.223374 1.673820 5.119190 3.210859 5.634379 6.634499 3.499433 177 178 179 180 181 182 183 184 3.828211 3.207140 3.307804 3.381860 2.330275 1.875490 3.106478 3.493221 185 186 187 188 189 190 191 192 3.616911 3.380078 2.764212 2.821210 2.756414 2.951367 2.884931 4.776017 193 194 2.978802 3.570297 > > proc.time() user system elapsed 0.12 0.09 0.21