I have data (below table) on trend (% over 10 years) in units of a whole. I also know the proportion of the whole each unit makes up. I want to do three things.
I thought there might be formulas for these, but I haven't found one (I asked the Mathematics community and got no response here). Maybe it's not that simple, and I really need to code it out so that I can manipulate annual indices until I get the appropriate trend. Any packages out there that might do that in R?
Unit | Proportion of total | 10yrT |
---|---|---|
1 | 0.073 | 0.29 |
2 | 0.009 | 1.13 |
3 | 0.003 | 0.29 |
4 | 0.003 | -0.46 |
5 | 0.315 | -1.77 |
6 | 0.013 | -0.26 |
7 | 0.007 | 1.54 |
8 | 0.003 | 4.57 |
9 | 0.227 | -2.35 |
10 | 0.202 | -1.45 |
11 | 0.008 | -0.13 |
12 | 0.139 | -0.79 |
1) If you are asking how to calculate a weighted mean in R it is the weighted.mean
function. Using dat
defined reproducibly in the Note at the end
prop <- dat[[2]]
trend <- dat[[3]]
wm <- weighted.mean(trend, prop); wm
## [1] -1.43993
2) weighted.mean
is linear in the first argument and the weighted mean of a constant is that constant so:
weighted.mean(trend + x, prop)
= weighted.mean(trend, prop) + weighted.mean(x, prop)
= weighted.mean(trend, prop) + x
= wm + x
and if that is to equal to 2 then x = 2 - wm
x <- 2 - wm; x
## [1] 3.43993
weighted.mean(trend + x, prop) # check
## [1] 2
3) Now if e12 is a vector of 11 zeros followed by a scalar 1 and y is a scalar then again by linearity
weighted.mean(trend + y * e12, prop)
= weighted.mean(trend, prop) + y * weighted.mean(e12, prop)
= wm + y * weighted.mean(e12, prop)
and if that is to equal 2 then solving we have y = (2 - wm)/weighted.mean(e12, prop) so
e12 <- c(numeric(11), 1); e12
## [1] 0 0 0 0 0 0 0 0 0 0 0 1
y <- (2 - wm)/weighted.mean(e12, prop); y
## [1] 24.79719
weighted.mean(trend + y * e12, prop) # check
## [1] 2
4) Alternately use uniroot
to solve these equations instead of using elementary algebra.
uniroot(\(x) weighted.mean(trend + x, prop) - 2, c(-100, 100))
## $root
## [1] 3.43993
## ...snip...
uniroot(\(y) weighted.mean(trend + y * e12, prop) - 2, c(-100, 100))
## $root
## [1] 24.79719
## ...snip...
dat <- data.frame(
Unit = 1:12,
`Proportion of total` = c(0.073, 0.009, 0.003, 0.003, 0.315, 0.013,
0.007, 0.003, 0.227, 0.202, 0.008, 0.139),
`10yrT` = c(0.29, 1.13, 0.29, -0.46, -1.77, -0.26, 1.54, 4.57, -2.35,
-1.45, -0.13, -0.79),
check.names = FALSE
)