Compute or scale data using (weighted) means, variances and covariances (possibly for the levels of a factor)
scalewt.Rd
These utility functions compute (weighted) means, variances and covariances for dataframe partitioned by a factor. The scale transforms a numeric matrix in a centred and scaled matrix for any weighting.
Usage
covwt(x, wt, na.rm = FALSE)
varwt(x, wt, na.rm = FALSE)
scalewt(df, wt = rep(1/nrow(df), nrow(df)), center = TRUE, scale = TRUE)
meanfacwt(df, fac = NULL, wt = rep(1/nrow(df), nrow(df)), drop = FALSE)
varfacwt(df, fac = NULL, wt = rep(1/nrow(df), nrow(df)), drop = FALSE)
covfacwt(df, fac = NULL, wt = rep(1/nrow(df), nrow(df)), drop = FALSE)
scalefacwt(df, fac = NULL, wt = rep(1/nrow(df), nrow(df)), scale = TRUE, drop = FALSE)
Arguments
- x
a numeric vector (
varwt
) or a matrix (covwt
) containg the data.- na.rm
a logical value indicating whether NA values should be stripped before the computation proceeds.
- df
a matrix or a dataframe containing the data.
- fac
a factor partitioning the data.
- wt
a numeric vector of weights.
- drop
a logical value indicating whether unused levels should be kept.
- scale
a logical value indicating whether data should be scaled or not.
- center
a logical value indicating whether data should be centered or not.
Details
Functions returns biased estimates of variances and covariances (i.e. divided by n and not n-1)
Value
For varwt
, the weighted variance. For covwt
,
the matrix of weighted co-variances. For scalewt
, the scaled
dataframe. For other function a list (if fac
is not null) of dataframes with approriate values
Author
Stéphane Dray stephane.dray@univ-lyon1.fr
Examples
data(meau)
w <- rowSums(meau$spe)
varwt(meau$env, w)
#> [1] 3380789
varfacwt(meau$env, wt = w)
#> Temp Flow pH Cond Oxyg Bdo5
#> 2.662792e+01 2.117085e+04 4.544898e-02 1.045356e+03 3.272200e+02 1.528326e+01
#> Oxyd Ammo Nitr Phos
#> 6.852473e-01 3.345562e+00 1.049381e+01 1.270055e+00
varfacwt(meau$env, wt = w, fac = meau$design$season)
#> Temp Flow pH Cond Oxyg Bdo5 Oxyd
#> spring 0.9757585 13294.597 0.004011005 516.5703 706.54116 2.242255 0.3379819
#> summer 1.5367837 8409.991 0.048821551 1075.9298 373.71187 31.593733 0.8087347
#> autumn 1.1853375 3094.825 0.029415810 711.2237 79.17508 7.137968 0.4286310
#> winter 0.1892814 20577.056 0.011215403 488.6816 0.00000 7.288562 0.3977386
#> Ammo Nitr Phos
#> spring 0.4966361 0.4542218 0.1785690
#> summer 9.7020787 9.1825417 2.3749067
#> autumn 1.2063193 11.8262243 1.2370015
#> winter 0.6453077 1.9045242 0.3247859
covfacwt(meau$env, wt = w, fac = meau$design$season)
#> $spring
#> Temp Flow pH Cond Oxyg
#> Temp 0.97575848 90.466910 0.011377156 -1.8526919 4.545732
#> Flow 90.46690958 13294.597375 1.511265616 -1620.8705756 314.602134
#> pH 0.01137716 1.511266 0.004011005 -0.4016582 1.570427
#> Cond -1.85269185 -1620.870576 -0.401658239 516.5703079 -125.144817
#> Oxyg 4.54573171 314.602134 1.570426829 -125.1448171 706.541159
#> Bdo5 0.44098751 -3.001428 -0.062016657 22.5017847 -27.841463
#> Oxyd 0.06375669 -15.808425 -0.026515467 10.0162478 -11.347866
#> Ammo 0.10882585 -2.617318 -0.035237284 8.5158741 -16.134192
#> Nitr 0.41044765 8.629361 0.017050119 7.0755317 9.766006
#> Phos 0.24995836 14.151278 -0.012478287 4.2221074 -6.216768
#> Bdo5 Oxyd Ammo Nitr Phos
#> Temp 0.44098751 0.06375669 0.10882585 0.41044765 0.24995836
#> Flow -3.00142772 -15.80842504 -2.61731819 8.62936124 14.15127751
#> pH -0.06201666 -0.02651547 -0.03523728 0.01705012 -0.01247829
#> Cond 22.50178465 10.01624777 8.51587411 7.07553168 4.22210738
#> Oxyg -27.84146341 -11.34786585 -16.13419207 9.76600610 -6.21676829
#> Bdo5 2.24225461 0.84671029 1.01756217 0.17412849 0.56264545
#> Oxyd 0.84671029 0.33798186 0.39054945 0.04348081 0.19928227
#> Ammo 1.01756217 0.39054945 0.49663609 -0.04203603 0.25098547
#> Nitr 0.17412849 0.04348081 -0.04203603 0.45422182 0.07014173
#> Phos 0.56264545 0.19928227 0.25098547 0.07014173 0.17856898
#>
#> $summer
#> Temp Flow pH Cond Oxyg Bdo5
#> Temp 1.53678367 -18.889535 -0.06181878 27.610449 -3.585045 1.612663
#> Flow -18.88953469 8409.991053 2.81696653 -2064.039510 147.001665 50.227154
#> pH -0.06181878 2.816967 0.04882155 -4.042155 4.007987 -1.203433
#> Cond 27.61044898 -2064.039510 -4.04215510 1075.929796 -353.512980 76.554743
#> Oxyg -3.58504490 147.001665 4.00798694 -353.512980 373.711869 -99.414560
#> Bdo5 1.61266286 50.227154 -1.20343314 76.554743 -99.414560 31.593733
#> Oxyd 0.18014694 -28.785502 -0.15567673 18.753388 -14.447624 3.675989
#> Ammo 1.63295151 -77.136716 -0.66360268 76.484553 -55.809638 15.923069
#> Nitr 3.37101714 -4.201051 0.07722057 48.549371 4.781234 -1.453511
#> Phos 1.45230824 -29.668794 -0.26324643 42.249215 -22.335561 6.463928
#> Oxyd Ammo Nitr Phos
#> Temp 0.1801469 1.6329515 3.37101714 1.4523082
#> Flow -28.7855020 -77.1367158 -4.20105143 -29.6687941
#> pH -0.1556767 -0.6636027 0.07722057 -0.2632464
#> Cond 18.7533878 76.4845535 48.54937143 42.2492147
#> Oxyg -14.4476245 -55.8096382 4.78123429 -22.3355615
#> Bdo5 3.6759886 15.9230695 -1.45351086 6.4639278
#> Oxyd 0.8087347 2.2725770 -0.31872000 0.8666007
#> Ammo 2.2725770 9.7020787 1.01641874 4.3063016
#> Nitr -0.3187200 1.0164187 9.18254171 2.4955079
#> Phos 0.8666007 4.3063016 2.49550789 2.3749067
#>
#> $autumn
#> Temp Flow pH Cond Oxyg
#> Temp 1.185337539 57.699272 0.05881731 -16.251789 7.0820760
#> Flow 57.699271592 3094.825442 4.13785120 -1045.593132 307.9664412
#> pH 0.058817313 4.137851 0.02941581 -4.006032 0.4842612
#> Cond -16.251788502 -1045.593132 -4.00603213 711.223660 -129.4426379
#> Oxyg 7.082075963 307.966441 0.48426119 -129.442638 79.1750780
#> Bdo5 0.289467352 14.197607 -0.22501789 30.327361 -11.9578564
#> Oxyd -0.014421826 -6.205749 -0.07457564 13.092774 -2.4766389
#> Ammo -0.066792566 -5.292391 -0.12500808 18.517809 -5.7026340
#> Nitr -1.219239399 -94.094443 -0.38882326 75.252397 -7.6557882
#> Phos -0.007556582 -5.387438 -0.12813768 20.837682 -4.1615973
#> Bdo5 Oxyd Ammo Nitr Phos
#> Temp 0.2894674 -0.01442183 -0.06679257 -1.2192394 -0.007556582
#> Flow 14.1976067 -6.20574922 -5.29239074 -94.0944433 -5.387437565
#> pH -0.2250179 -0.07457564 -0.12500808 -0.3888233 -0.128137682
#> Cond 30.3273608 13.09277445 18.51780860 75.2523966 20.837682102
#> Oxyg -11.9578564 -2.47663892 -5.70263398 -7.6557882 -4.161597294
#> Bdo5 7.1379676 1.43234326 2.83047506 0.9700170 2.054134365
#> Oxyd 1.4323433 0.42863098 0.64496973 1.3282607 0.633063345
#> Ammo 2.8304751 0.64496973 1.20631929 1.3100916 1.006350423
#> Nitr 0.9700170 1.32826073 1.31009156 11.8262243 2.822612916
#> Phos 2.0541344 0.63306335 1.00635042 2.8226129 1.237001509
#>
#> $winter
#> Temp Flow pH Cond Oxyg Bdo5
#> Temp 0.189281381 -9.118267 -0.003761361 -1.753279 0 0.3403425
#> Flow -9.118266523 20577.055562 11.201117191 -1359.032417 0 -29.4463108
#> pH -0.003761361 11.201117 0.011215403 0.524244 0 0.1705288
#> Cond -1.753279458 -1359.032417 0.524243950 488.681578 0 44.6978091
#> Oxyg 0.000000000 0.000000 0.000000000 0.000000 0 0.0000000
#> Bdo5 0.340342483 -29.446311 0.170528834 44.697809 0 7.2885621
#> Oxyd 0.045378998 -23.473735 0.030501133 12.117603 0 1.6532309
#> Ammo 0.030442755 -9.040768 0.051914585 14.757986 0 2.1112420
#> Nitr -0.386570820 66.143156 0.101312699 19.316934 0 1.7467691
#> Phos -0.003154690 -1.885732 0.036748495 10.343894 0 1.3745669
#> Oxyd Ammo Nitr Phos
#> Temp 0.04537900 0.03044276 -0.3865708 -0.00315469
#> Flow -23.47373458 -9.04076761 66.1431561 -1.88573224
#> pH 0.03050113 0.05191459 0.1013127 0.03674849
#> Cond 12.11760262 14.75798631 19.3169341 10.34389437
#> Oxyg 0.00000000 0.00000000 0.0000000 0.00000000
#> Bdo5 1.65323092 2.11124200 1.7467691 1.37456686
#> Oxyd 0.39773860 0.49614455 0.4354671 0.32754671
#> Ammo 0.49614455 0.64530765 0.6766280 0.43978768
#> Nitr 0.43546714 0.67662796 1.9045242 0.51786868
#> Phos 0.32754671 0.43978768 0.5178687 0.32478591
#>
scalewt(meau$env, wt = w)
#> Temp Flow pH Cond Oxyg Bdo5
#> sp_1 0.6032810 -1.083805751 0.8226409 -0.52688351 0.827417272 -0.41004323
#> sp_2 0.7970710 -0.279693478 -0.1154997 0.09169949 -4.534891137 0.94567023
#> sp_3 0.7970710 -0.004783299 0.8226409 -0.68152926 0.993261862 -0.15424824
#> sp_4 0.9908610 0.558782568 1.2917112 -0.68152926 1.711921752 -0.10308924
#> sp_5 1.1846510 0.847438255 0.8226409 -0.83617501 1.214387982 -0.07750974
#> sp_6 0.7970710 0.716855920 0.8226409 -2.07334102 0.274601972 -0.56352022
#> su_1 1.1846510 -0.939477907 -0.1154997 0.40099099 -0.001805678 -0.41004323
#> su_2 1.1846510 -0.815768326 -3.3989918 2.10209425 -4.147920427 4.37332313
#> su_3 1.5722310 -0.678313237 -2.4608512 2.25674000 -2.710600648 2.83855318
#> su_4 1.7660210 -0.403403058 -1.5227106 1.48351124 -1.052154748 2.07116820
#> su_5 1.5722310 -0.265947969 0.3535706 1.01957399 -0.222931798 -0.56352022
#> su_6 1.1846510 0.764965202 -0.5845700 -0.83617501 -0.720465568 1.17588572
#> au_1 -1.1408291 -1.193769822 0.3535706 0.09169949 -0.222931798 -0.58909972
#> au_2 -0.7532491 -0.932605152 -1.5227106 3.49390600 -3.152852888 8.21024802
#> au_3 -0.9470391 -0.822641081 -1.0536403 1.17421974 -0.609902508 0.81777274
#> au_4 -0.7532491 -0.781404554 -0.1154997 0.55563674 0.606291152 -0.48678173
#> au_5 -0.9470391 -0.870750362 1.2917112 -0.21759201 -0.222931798 -0.58909972
#> au_6 -0.5594591 -0.121620125 1.2917112 -1.30011226 0.551009622 -0.28214573
#> wi_1 -0.7532491 -0.554603656 -1.5227106 0.40099099 0.274601972 -0.58909972
#> wi_2 -0.7532491 0.366345442 -0.1154997 1.48351124 0.274601972 1.43168072
#> wi_3 -0.7532491 0.799328974 -0.1154997 1.79280275 0.274601972 1.22704472
#> wi_4 -0.7532491 2.057043042 -0.1154997 0.55563674 0.274601972 0.22944425
#> wi_5 -0.9470391 1.314785559 -0.5845700 0.55563674 0.274601972 -0.56352022
#> wi_6 -0.7532491 1.933333461 -0.5845700 -0.68152926 0.274601972 -0.66583822
#> Oxyd Ammo Nitr Phos
#> sp_1 -0.04158281 -0.44038484 -0.6323573 -0.7611370
#> sp_2 2.25366690 1.05216248 -0.8484458 0.4722619
#> sp_3 0.07921981 -0.28730307 -0.4471386 -0.7700104
#> sp_4 0.07921981 -0.25996703 -0.4471386 -0.2109879
#> sp_5 0.20002242 -0.24356541 -0.2619200 -0.1133808
#> sp_6 -0.64559589 -0.47865529 -0.8484458 -0.7167701
#> su_1 0.44162765 -0.44585205 -0.7558364 -0.7433902
#> su_2 5.15292969 4.85187087 -1.4349715 2.3800371
#> su_3 1.28724597 3.81310167 0.6950431 3.1342738
#> su_4 1.40804859 2.17293978 0.9111316 2.2025696
#> su_5 0.56243027 -0.38571278 1.4050480 0.6852228
#> su_6 0.20002242 -0.18342615 -0.5397480 -0.3263417
#> au_1 -1.12880636 -0.46772087 0.2937360 -0.8321240
#> au_2 7.93138986 6.32801657 -1.0027946 4.9089484
#> au_3 0.56243027 0.97015439 2.3928809 2.4244039
#> au_4 -0.04158281 -0.27636865 2.0224436 0.5609956
#> au_5 -0.64559589 -0.45131925 1.2506991 0.2504275
#> au_6 -1.12880636 -0.45131925 -0.5520959 -0.4771890
#> wi_1 -0.28318804 -0.41304881 -1.1262738 -0.6901500
#> wi_2 1.77045643 0.87174467 -0.2619200 0.5609956
#> wi_3 1.64965382 1.02482645 -0.2001804 1.6701672
#> wi_4 0.20002242 0.06259814 -0.3236595 -0.1311275
#> wi_5 -0.28318804 -0.19982776 -0.1384409 -0.3263417
#> wi_6 -0.76639851 -0.48412249 -1.0027946 -0.7433902
#> attr(,"scaled:center")
#> Temp Flow pH Cond Oxyg Bdo5
#> 6.8869347 198.6959799 8.3246231 312.0351759 95.0326633 3.9030151
#> Oxyd Ammo Nitr Phos
#> 1.4344221 0.9255025 5.4484673 0.9677764
#> attr(,"scaled:scale")
#> Temp Flow pH Cond Oxyg Bdo5
#> 5.1602248 145.5020697 0.2131877 32.3319586 18.0892244 3.9093807
#> Oxyd Ammo Nitr Phos
#> 0.8277966 1.8290877 3.2394143 1.1269672