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

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

\item{sandk}{= 1 to calculate skewness and kurtosis}

\item{probs}{vector of probabilities to calculate quantiles}

\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{print}{= 1 to print the result}

\item{simplify}{= 0 to return class by object when by= is given.}
}
\value{
A matrix of statistics(mean, std, var, nobs, n)  x  variables \cr
where nobs is sum(weights) for nonmissing obs, and n is nrow(data). \cr
In addition, if \code{probs} is given, quantiles will be appended to
the above. \cr
\cr
When by is present, this function returns a class by object
consisting of above for each subgroup if simplify=0. \cr
If by is not a list and simplify=1,
the result is a statistics x variables x subgroups array. \cr
If by is a list of several factors, simplify=1 does not affet the result.
\cr
When X is univariate, the result is a statistics x subgroup matrix.
\cr
\cr
When data is NULL, NULL will be returned.
}
\description{
Calculates mean, std, variance and n with case weight
}
\details{
The means and vars will be calculated using native weighted.mean function.
\cr
The kurtosis = weigted.mean( scale(data, scale=FALSE)^4 ) / std^4 - 3
The quantiles will be calculated using wquantile function. \cr
}
\examples{
# univariate example
y <- 1:9
pdf <- exp( -0.5*(y-5)^2/(1.3^2) )
pdf <- pdf/sum(pdf)
group <- c(1,1,1,1,1,2,2,2,2)
mands( y )
mands( y, pdf, sandk=1 )
mands( y, by=group, sandk=1 )

# bivariate example
ndims <- 2; npoints <- 11; xmin <- -3; xmax <- 3; corrmat <- 0.2
nobs <- 100000

# dispersion matrix
Dmat <- matrix(corrmat,ndims,ndims)+(1-corrmat)*diag(ndims)
Dmat[1,1] <- .5

# grid X
X <- seq(xmin,xmax,length.out=npoints)
if( ndims >= 2 ){
 for( i in 2:ndims )
  X <- cprod(X,seq(xmin,xmax,length.out=npoints))
}

# weight
pdf <- exp( -0.5*diag( X\%*\%solve(Dmat)\%*\%t(X)) )
pdf <- pdf/sum(pdf)
freq <- round(nobs*pdf+.5)

# all data
mands( X )
mands( X, freq )
mands( X, freq, probs=c(0.25,0.5,0.75,0.975) )

# grouping variable
set.seed(1701)
if( is.matrix(X) ) n <- nrow(X) else n <- length(X)
byvar <- sample(1:3, n, replace=1)
byvar <- sort(byvar)

# for each subgroup
mands( X, by=byvar )
mands( X, freq, by=byvar )
mands( X, freq, by=byvar, probs=c(0.25,0.75) )

}
