% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/mandd.R
\name{mandd}
\alias{mandd}
\title{Calculates mean vector and dispersion matrix with case weight}
\usage{
mandd(
  data,
  weight = 1,
  by = NULL,
  byvarname = NULL,
  vardef = "n",
  nocov = 0,
  cor = 0,
  print = 0,
  simplify = 0
)
}
\arguments{
\item{data}{data matrix or data frame.}

\item{weight}{case weight vector of length nrow(data) or 1.}

\item{by}{a factor or a list of factors, each of length nrow(data) \cr
to be used as the INDICES of by function.}

\item{byvarname}{vector containing the names of by varialbes to be used.
(Not Yet Implemented.)}

\item{vardef}{= "n-1" to use n-1 as denominator for variance.}

\item{nocov}{= 1 to skip the calculation of the variance/covariance matrix.
\cr \code{mands} function will be used.}

\item{cor}{= 1 to return the correlation matrix as well.}

\item{print}{= 1 to print the result}

\item{simplify}{= 1 to simplify the rewult when by= is given}
}
\value{
list of: \cr
mean vector, var vector, std vector, nobs vector, \cr
where nobs is sum(weights) for nonmissing obs and n=nrow(data). \cr
cov matrix,  nobspair matrix which contains # of obs. per pair
, or NULL, \cr
n (# of rows), and vardef.
}
\description{
Calculates mean vector and dispersion matrix with case weight
}
\details{
When the dispersion matrix is not needed (nocov=1) use \code{mands} function.
\cr
When simplified=0 is given, the result is a list consisting of \cr
vectors: mean, var, std, nobs \cr
matrix: cov, nobspair \cr
scalars: n, vardef \cr
where nobspair is a matrix of the same size as cov containing
the weighted # of observations used to calculate cov. \cr
When cor=1 is given, correlation matrix will also be returned.

When by is specified, the result is a class by list if simplify=0,
Otherwise, it is a list consisting of \cr
matrices: mean, var, std, nobs \cr
lists: cov, nobspair \cr
scalars: n, vardef \cr
\cr
To convert the list of covariance matrices to an array or stacked matrix,
 use:
 \cr
 \code{ library(lazy.mat) } \cr
 \code{ cov_array=l2a(res1$cov) } \cr
 \code{ cov_mat=a2m(cov_array,method=2) } \cr
}
\examples{
##################################
# data matrix with by group
##################################
nobs <- 1000
ndims <- 2

# covariance matrix
cov <- 0.2
C1 <- matrix(cov,ndims,ndims)+(1-cov)*diag(ndims)
C1[1,1] <- .5
C2 <- -C1; diag(C2) <- diag(C1)
C3 <- diag(ndims)
Printm(C1,C2,C3)

# random numbers
seed=1701
set.seed(seed)

# X1 ~ N(0,C1),  X2 ~ N(0,C2),   X3 ~ N(0,C3)
X1=matrix(rnorm(nobs*ndims),nobs)
ev <- eigen(C1); eva <- diag(ev$values); eve <- ev$vectors
X1 <- X1\%*\%diag(sqrt(ev$values))\%*\%t(ev$vectors)
X2 <- matrix(rnorm(nobs*ndims),nobs)
ev <- eigen(C2); eva <- diag(ev$values); eve <- ev$vectors
X2 <- X2\%*\%diag(sqrt(ev$values))\%*\%t(ev$vectors)
X3 <- matrix(rnorm(nobs*ndims),nobs)

X <- rbind(X1,X2,X3)
byvar <- rep(c("pos","neg","zero"), each=nobs)

# missing values
locmiss <- sample(1:length(X), 0.2*length(X))
X[locmiss]=NA

# statistics w/o by-group
res0 <- mandd( X )
# statistics by by-group
res1 <- mandd( X, 1, byvar, simplify=1 )
Printm(res1$mean, res1$std, res1$cov)


#############################
# grid points and weight
#############################
# C1, C2, C3 are defined above.
npoints <- 11; thmin <- -3; thmax <- 3; nobs2 <- 10000
theta <- seq(thmin,thmax,length.out=npoints)
theta <- cprod(theta,theta)
if( ndims > 2 )
 for( i in 2:ndims )
  theta <- cprod(theta,theta)

# weight:  pdf of N(0,C1)
thdist1 <- exp( -0.5*diag( theta\%*\%solve(C1)\%*\%t(theta) ) )
thdist1 <- thdist1/sum(thdist1)
n1 <- round(nobs2*thdist1+.5)

# statistics
res20 <- mandd( theta, n1 )
res200 <- mandd( theta )
Printm(res20$mean, res200$mean)
Printm(res20$var, res200$var)
Printm(C1, res20$cov, res200$cov)


# missing values
seed <- 1701
set.seed(seed)
locmiss1 <- sample( 1:(npoints^ndims), max(1,(npoints^ndims)/10) )
theta[locmiss1] <- NA

# statistics
res2 <- mandd( theta, n1 )
Printm(res2$mean, res2$std, res2$cov)


# grid points and weight with by group
# theta, nobs2, C1, C2, C3 are defined above.
# weight:  pdf of N(0,C2)
thdist2 <- exp( -0.5*diag( theta\%*\%solve(C2)\%*\%t(theta) ) )
thdist2[is.na(thdist2)] <- 0
thdist2 <- thdist2/sum(thdist2)
n2 <- round(nobs2*thdist2+.5)
# weight:  pdf of N(0,C3)
thdist3 <- exp( -0.5*diag( theta\%*\%solve(C3)\%*\%t(theta) ) )
thdist3[is.na(thdist3)] <- 0
thdist3 <- thdist3/sum(thdist3)
n3 <- round(nobs2*thdist3+.5)

Theta=rbind(theta,theta,theta)
n=c(n1,n2,n3)
byvar2=rep(c("pos","neg","zero"),each=nrow(theta))

# statistics
res3 <- mandd( Theta, n, byvar2, simplify=1 )
Printm(res3$mean, res3$std, res3$cov)

}
