# NOT RUN {
#############################################################################
# EXAMPLE 1: Toy example for weighted_var function
#############################################################################
set.seed(9897)
# simulate data
N <- 10
x <- stats::rnorm(N)
w <- stats::runif(N)
#---- variance
# use weighted_var
weighted_var( x=x, w=w )
# use cov.wt
stats::cov.wt( data.frame(x=x), w=w )$cov[1,1]
# }
# NOT RUN {
# use wtd.var from Hmisc package
Hmisc::wtd.var(x=x, weights=w, normwt=TRUE, method="unbiased")
#---- standard deviation
weighted_sd( x=x, w=w )
#---- mean
weighted_mean( x=x, w=w )
stats::weighted.mean( x=x, w=w )
#---- weighted quantiles for unweighted data
pvec <- c(.23, .53, .78, .99 ) # choose probabilities
type <- 7
# quantiles for unweighted data
weighted_quantile( x, probs=pvec, type=type)
quantile( x, probs=pvec, type=type)
Hmisc::wtd.quantile(x,probs=pvec, type=type)
# quantiles for weighted data
pvec <- c(.23, .53, .78, .99 ) # probabilities
weighted_quantile( x, w=w, probs=pvec)
Hmisc::wtd.quantile(x, weights=w, probs=pvec)
#--- weighted skewness and curtosis
weighted_skewness(x=x, w=w)
weighted_curtosis(x=x, w=w)
#############################################################################
# EXAMPLE 2: Descriptive statistics normally distributed data
#############################################################################
# simulate some normally distributed data
set.seed(7768)
x <- stats::rnorm( 10000, mean=1.7, sd=1.2)
# some statistics
weighted_mean(x=x)
weighted_sd(x=x)
weighted_skewness(x=x)
weighted_curtosis(x=x)
#############################################################################
# EXAMPLE 3: Frequency tables
#############################################################################
#*****
# simulate data for weighted frequency tables
y <- scan()
1 0 1 1 1 2 1 3 1 4
2 0 2 1 2 2 2 3 2 4
y <- matrix( y, ncol=2, byrow=TRUE)
# define probabilities
set.seed(976)
pr <- stats::runif(10)
pr <- pr / sum(pr)
# sample data
N <- 300
x <- y[ sample( 1:10, size=300, prob=pr, replace=TRUE ), ]
w <- stats::runif( N, 0.5, 1.5 )
# frequency table unweighted data
weighted_table(x[,2] )
table( x[,2] )
# weighted data and proportions
weighted_table(x[,2], w=w, props=TRUE)
#*** contingency table
table( x[,1], x[,2] )
weighted_table( x )
# table using weights
weighted_table( x, w=w )
# }
Run the code above in your browser using DataLab