Re: [R] How to estimate weekly Variance
Felipe Carrillo [EMAIL PROTECTED] wrote in news:[EMAIL PROTECTED]: Dear All: Using the Excel formula below I am able to estimate daily variance(Daily VarPD)for PD,but I can't figure out how to estimate the Weekly variance for PD. Any hints? Daily VarPD = (PD * (1 - TD))/TD + VarTD * ((PD*(1-TD)+ PD^2*TD))/TD^3 WK TD VARTD PD Daily VarPD 28 0.014735 0.449 0 0 28 0.012421 0.449 0 0 28 0.014396 0.449 71 7005 28 0.014898 0.449 132 14016 28 0.013512 0.449 223 32506 28 0.013886 0.449 224 31287 28 0.013778 0.449 218 30528 29 0.013274 0.449 228 34505 29 0.013072 0.449 224 34531 29 0.012518 0.449 488 117720 29 0.013264 0.449 525 119323 29 0.013233 0.449 80 9134 29 0.010194 0.449 417 133067 29 0.012567 0.449 82 10184 30 0.014695 0.449 413 68893 30 0.010634 0.449 373 103688 30 0.013934 0.449 914 272719 30 0.014107 0.449 213 28473 30 0.014339 0.449 651 147016 30 0.014375 0.449 521 102429 30 0.016373 0.449 979 229104 I am going to assume on the basis of your earlier posts that you have gotten this into a dataframe. You should know, since Prof Ripley told you so six months ago, that variable names cannot have spaces. The formula you offer makes no sense to me, and I could not see what bearing it has on calculating a variance of a week's worth of PD values. In the future, you should offer code to create a minimal dataset. Assuming your dataframe is fishwk, here is a method to get the variances of PD by week: by(data=fishwk, INDICES=fishwk$WK, function(x) var(x$PD)) -- David Winsemius __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to estimate weekly Variance
Here is one way of doing it: x - read.table(textConnection(WK TDVARTD PD DailyVarPD + 28 0.0147350.449 0 0 + 28 0.0124210.449 0 0 + 28 0.0143960.449 71 7005 + 28 0.0148980.449 132 14016 + 28 0.0135120.449 223 32506 + 28 0.0138860.449 224 31287 + 28 0.0137780.449 218 30528 + 29 0.0132740.449 228 34505 + 29 0.0130720.449 224 34531 + 29 0.0125180.449 488 117720 + 29 0.0132640.449 525 119323 + 29 0.0132330.449 80 9134 + 29 0.0101940.449 417 133067 + 29 0.0125670.449 82 10184 + 30 0.0146950.449 413 68893 + 30 0.0106340.449 373 103688 + 30 0.0139340.449 914 272719 + 30 0.0141070.449 213 28473 + 30 0.0143390.449 651 147016 + 30 0.0143750.449 521 102429 + 30 0.0163730.449 979 229104), header=TRUE) closeAllConnections() # daily using your formula x$newDaily - with(x, (PD * (1 - TD))/TD + VARTD * ((PD*(1-TD)+ PD^2*TD))/TD^3) x WK TDVARTD PD DailyVarPD newDaily 1 28 0.014735 4.49e-05 0 0 0.000 2 28 0.012421 4.49e-05 0 0 0.000 3 28 0.014396 4.49e-05 71 7005 7006.197 4 28 0.014898 4.49e-05 132 14016 14018.783 5 28 0.013512 4.49e-05 223 32506 32514.484 6 28 0.013886 4.49e-05 224 31287 31295.424 7 28 0.013778 4.49e-05 218 30528 30535.647 8 29 0.013274 4.49e-05 228 34505 34514.170 9 29 0.013072 4.49e-05 224 34531 34539.992 10 29 0.012518 4.49e-05 488 117720 117762.642 11 29 0.013264 4.49e-05 525 119323 119365.443 12 29 0.013233 4.49e-05 80 9134 9136.090 13 29 0.010194 4.49e-05 417 133067 133116.531 14 29 0.012567 4.49e-05 82 10184 10186.465 15 30 0.014695 4.49e-05 413 68893 68915.217 16 30 0.010634 4.49e-05 373 103688 103724.478 17 30 0.013934 4.49e-05 914 272719 272829.966 18 30 0.014107 4.49e-05 213 28473 28480.576 19 30 0.014339 4.49e-05 651 147016 147070.746 20 30 0.014375 4.49e-05 521 102429 102464.631 21 30 0.016373 4.49e-05 979 229104 229195.063 # not sure what you want for weekly, but we can take the averages of the values x.wk - aggregate(x, list(x$WK), mean) x.wk$var - with(x.wk, (PD * (1 - TD))/TD + VARTD * ((PD*(1-TD)+ PD^2*TD))/TD^3) x.wk Group.1 WK TDVARTD PD DailyVarPD newDaily var 1 28 28 0.01394657 4.49e-05 124. 16477.43 16481.51 14340.27 2 29 29 0.01258886 4.49e-05 292. 65494.86 65517.33 53548.78 3 30 30 0.01406529 4.49e-05 580.5714 136046.00 136097.24 126432.61 On Feb 16, 2008 11:17 AM, Felipe Carrillo [EMAIL PROTECTED] wrote: Jim: I actually would like to know how can I estimate Daily and Weekly Variance for PD using R and not Excel.I have read about R's variance function but I don't know how to apply it to PD in my dataset. I hope my question is clearer. Thanks Dear All: Using the Excel formula below I am able to estimate daily variance(Daily VarPD)for PD,but I can't figure out how to estimate the Weekly variance for PD. Any hints? Daily VarPD = (PD * (1 - TD))/TD + VarTD * ((PD*(1-TD)+ PD^2*TD))/TD^3 WK TDVARTD PD Daily VarPD 28 0.0147350.449 0 0 28 0.0124210.449 0 0 28 0.0143960.449 71 7005 28 0.0148980.449 132 14016 28 0.0135120.449 223 32506 28 0.0138860.449 224 31287 28 0.0137780.449 218 30528 29 0.0132740.449 228 34505 29 0.0130720.449 224 34531 29 0.0125180.449 488 117720 29 0.0132640.449 525 119323 29 0.0132330.449 80 9134 29 0.0101940.449 417 133067 29 0.0125670.449 82 10184 30 0.0146950.449 413 68893 30 0.0106340.449 373 103688 30 0.0139340.449 914 272719 30 0.0141070.449 213 28473 30 0.0143390.449 651 147016 30 0.0143750.449 521 102429 30 0.0163730.449 979 229104 Felipe D. Carrillo Fishery Biologist US Fish Wildlife Service California, USA