Cálculo de la media ponderada y la desviación estándar
Tengo una serie temporal x_0 ... x_t
. Me gustaría calcular la varianza ponderada exponencialmente de los datos. Es decir:
V = SUM{w_i*(x_i - x_bar)^2, i=1 to T} where SUM{w_i} = 1 and x_bar=SUM{w_i*x_i}
Ref: http://en.wikipedia.org/wiki/Weighted_mean#Weighted_sample_variance
El objetivo es básicamente ponderar las observaciones que están más atrás en el tiempo menos. Esto es muy simple de implementar, pero me gustaría utilizar tanto construido en funcitonality como sea posible. ¿Alguien sabe a qué corresponde esto en R?
Gracias
4 answers
R proporciona la media ponderada. De hecho,?ponderar.mean muestra este ejemplo:
## GPA from Siegel 1994
wt <- c(5, 5, 4, 1)/15
x <- c(3.7,3.3,3.5,2.8)
xm <- weighted.mean(x, wt)
Un paso más:
v <- sum(wt * (x - xm)^2)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-07 02:23:28
El paquete Hmisc contiene las funciones que necesita.
Así:
x <- c(3.7,3.3,3.5,2.8)
wt <- c(5, 5, 4, 1)/15
xm <- wtd.mean(x, wt)
var <- wtd.var(x, wt)
sd <- sqrt(var)
Desafortunadamente el autor del paquete Hmisc no incluyó una función wtd.sd
explícita. Tienes que raíz cuadrada wtd.var.
Charles Kangai
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-23 22:03:02
Yo también obtengo errores de Hmisc
cuando uso la función wtd.var()
. Afortunadamente, SDMTools
tiene una funcionalidad comparable, e incluso calcula SD directamente para usted, sin necesidad de tomar sqrt de varianza.
library(SDMTools)
x <- c(3.7,3.3,3.5,2.8)
wt <- c(5, 5, 4, 1)/15 ## Note: no actual need to normalize weights to sum to 1, this will be done automatically.
wt.mean(x, wt)
wt.sd(x,wt)
wt.var(x, wt)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-21 18:24:08
El paquete Hmisc proporciona esta funcionalidad:
Http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=Hmisc:wtd.stats
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-08 02:27:10