mandd {lazy.tools} R Documentation

## Calculates mean vector and dispersion matrix with case weight

### Description

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

 `data` data matrix or data frame. `weight` case weight vector of length nrow(data) or 1. `by` a factor or a list of factors, each of length nrow(data) to be used as the INDICES of by function. `byvarname` vector containing the names of by varialbes to be used. (Not Yet Implemented.) `vardef` = "n-1" to use n-1 as denominator for variance. `nocov` = 1 to skip the calculation of the variance/covariance matrix. `mands` function will be used. `cor` = 1 to return the correlation matrix as well. `print` = 1 to print the result `simplify` = 1 to simplify the rewult when by= is given

### Details

When the dispersion matrix is not needed (nocov=1) use `mands` function.
When simplified=0 is given, the result is a list consisting of
vectors: mean, var, std, nobs
matrix: cov, nobspair
scalars: n, vardef
where nobspair is a matrix of the same size as cov containing the weighted # of observations used to calculate cov.
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
matrices: mean, var, std, nobs
lists: cov, nobspair
scalars: n, vardef

To convert the list of covariance matrices to an array or stacked matrix, use:
` library(lazy.mat) `
` cov_array=l2a(res1\$cov) `
` cov_mat=a2m(cov_array,method=2) `

### Value

list of:
mean vector, var vector, std vector, nobs vector,
where nobs is sum(weights) for nonmissing obs and n=nrow(data).
cov matrix, nobspair matrix which contains # of obs. per pair, or NULL,
n (# of rows), and vardef.

### 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)
Print(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 )
Print(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 )
Print(res20\$mean, res200\$mean)
Print(res20\$var, res200\$var)
Print(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 )
Print(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 )
Print(res3\$mean, res3\$std, res3\$cov)

```

[Package lazy.tools version 0.1.3 Index]