sort_missing_data {lazy.tools}R Documentation

Sort Matrix/Data Frame According to the Pattern of Missing Elements

Description

Sort Matrix/Data Frame According to the Pattern of Missing Elements

Usage

sort_missing_data(X, misslast = 1, nobs = 0)

Arguments

X

a data frame or matrix to be sorted.

misslast

= 1 to treat NA as the largest, = 1 to treat NA as the smallest.

nobs

= 1 to sort by the # of observed elements first
= -1 to sort by the descending # of observed elements first
= 0 not to pay any attention to the # of missing elements.

Value

A list of (sorted_data, freq, Nobs, class, od)
where sorted_data is of the same size as X,
freq is the vector consisting of the # of observatons for each pattern,
Nobs is the vector of length nrow(X) containing the # of observed elements for each row of X.
class is the interger in [1,length(freq)] indicating the classification of each obs according to the missing pattern.
od is the order to be used to sort X as X_sorted=X[od,].

Examples

set.seed(17029)
nn=50
dataframe=0

if( dataframe ){
 x=sample(0:9,nn,replace=1)
 x[runif(nn) < 0.5]=NA
 x2=sample(letters[1:10],nn,replace=1)
 x2[runif(nn) < 0.5]=NA
 x3=sample(LETTERS[1:10],nn,replace=1)
 x3[runif(nn) < 0.5]=NA
 X=data.frame(x,x2,x3, stringsAsFactors=0)
} else{
 x=sample(0:9,nn,replace=1)
 x[runif(nn) < 0.3]=NA
 x2=sample(1:10,nn,replace=1)
 x2[runif(nn) < 0.3]=NA
 x3=sample(1:10,nn,replace=1)
 x3[runif(nn) < 0.3]=NA
 X=cbind(x,x2,x3)
}
res=sort_missing_data( X, misslast=1, nobs=-1 )
Xs=res$sorted_data
freqs=res$freq
Nobss=res$Nobs
od=res$od

# Nobsss should be equal to Nobs, Xss, to Xs
Nobsss=ncol(X)-apply(Xs,1,function(x) sum(is.na(x)))
Xss=X[od,]
pat=rep(1:length(freqs),freqs)
Print(Xs,Xss,Nobss, pat,od)

misspat=Xs[cumsum(res$freq),]
nobs=ncol(misspat)-apply(misspat,1,function(x) sum(is.na(x)))
Print(misspat,nobs)

mands( Xs, by=pat )



[Package lazy.tools version 0.1.4 ]