View source: R/MIDIAweightingFUNC.R
MDIAweights | R Documentation |
Method is from Haberman, 1984.
MDIAweights(datamatrix, targets, tol = 1e-06, maxiter = 100, starttheta = NA)
datamatrix |
Numeric matrix (or something that can be quickly converted into one e.g. a data frame). |
targets |
Vector indicating the desired mean value for each column after weighting. |
tol |
Numeric input relating to required proximity of weighted means to target means in order for algorithm to be considered converged (default 1e-6). |
maxiter |
Numeric input denoting the maximum number of iterations that will be applied to find the weights (default 100). |
starttheta |
Starting parameter vector to begin iterative process (by default a vector of zeros). |
The function returns a list with the following elements:
A vector of weights.
The number of iterations taken to converge. If this is equal to maxiter then convergenece may not have been achieved.
The weighted means on each column in the data matrix.
Parameters used for weighting.
Haberman, S. J. (1984). Adjustment by minimum discriminant information. The Annals of Statistics, 12(3), 971-988.
#try weighting one population (with a logistic distribution)
#to match E(x), E(x^2),E(x^3),E(x^4), E(x^5), E(x^6) and E(x^7) of another (with a normal distribution)
x1=rnorm(50000,100,30)
summary(x1)
x2=rlogis(50000,120,25)
summary(x2)
MDIAx2=MDIAweights(cbind(x2,x2^2,x2^3,x2^4,x2^5,x2^6,x2^7),c(mean(x1),mean(x1^2),mean(x1^3),mean(x1^4),mean(x1^5),mean(x1^6),mean(x1^7)))
MDIAx2$iter
sum(MDIAx2$wts)
summary(MDIAx2$wts)
summary(MDIAx2$wts/mean(MDIAx2$wts))
c(mean(x1),sum(MDIAx2$wts*x2))
c(mean(x1^2),sum(MDIAx2$wts*x2^2))
c(mean(x1^3),sum(MDIAx2$wts*x2^3))
c(mean(x1^4),sum(MDIAx2$wts*x2^4))
c(mean(x1^5),sum(MDIAx2$wts*x2^5))
c(mean(x1^6),sum(MDIAx2$wts*x2^6))
c(mean(x1^7),sum(MDIAx2$wts*x2^7))
#see if it worked visually
dat1=data.frame(x=c(x1,x2)
,pop=as.factor(c(rep(1,length(x1)),rep(2,length(x2))))
,wts=c(rep(1/length(x1),length(x1)),MDIAx2$wts))
library(ggplot2)
ggplot(data=dat1,aes(x=x,fill=pop))+geom_density(alpha=0.5)#unweighted
ggplot(data=dat1,aes(x=x,fill=pop,weight=wts))+geom_density(alpha=0.5)#weighted
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.