| 1 |
setMethod("Table",signature(x="atomic"), |
| 2 |
function(x,weights=NULL,counts=TRUE,percentage=FALSE,...) { |
| 3 |
if(!(counts || percentage)) stop("either counts or percentage must be TRUE") |
| 4 |
if(!length(weights)){ |
| 5 |
tab <- drop(table(x)) |
| 6 |
} else { |
| 7 |
good <- is.finite(weights) & is.finite(x) |
| 8 |
tmp <- rowsum(weights[good],x[good]) |
| 9 |
tab <- drop(tmp) |
| 10 |
} |
| 11 |
if(percentage) { |
| 12 |
perc <- 100 * tab/sum(tab) |
| 13 |
} |
| 14 |
structure(if(counts && percentage) |
| 15 |
cbind(Counts=tab,Percent=perc) |
| 16 |
else if(percentage) |
| 17 |
perc |
| 18 |
else |
| 19 |
tab |
| 20 |
,class="table") |
| 21 |
}) |
| 22 |
|
| 23 |
setMethod("Table",signature(x="factor"), |
| 24 |
function(x,weights=NULL,counts=TRUE,percentage=FALSE,...) { |
| 25 |
if(!(counts || percentage)) stop("either counts or percentage must be TRUE") |
| 26 |
if(!length(weights)){ |
| 27 |
tab <- drop(table(x)) |
| 28 |
} else { |
| 29 |
good <- is.finite(weights) & is.finite(x) |
| 30 |
tmp <- rowsum(weights[good],x[good]) |
| 31 |
tab <- structure(rep(0,nlevels(x[good])),names=levels(x[good])) |
| 32 |
tab[rownames(tmp)] <- tmp[] |
| 33 |
} |
| 34 |
if(percentage) { |
| 35 |
perc <- 100 * tab/sum(tab) |
| 36 |
} |
| 37 |
structure(if(counts && percentage) |
| 38 |
cbind(Counts=tab,Percent=perc) |
| 39 |
else if(percentage) |
| 40 |
perc |
| 41 |
else |
| 42 |
tab |
| 43 |
,class="table") |
| 44 |
}) |
| 45 |
|
| 46 |
setMethod("Table",signature(x="item.vector"), |
| 47 |
function(x, |
| 48 |
weights=NULL, |
| 49 |
counts=TRUE, |
| 50 |
percentage=(style=="codebook"), |
| 51 |
style=c("table","codebook","nolabels"), |
| 52 |
include.missings=(style=="codebook"), |
| 53 |
missing.marker=if(style=="codebook") "M" else "*", |
| 54 |
...){ |
| 55 |
if(!(counts || percentage)) stop("either counts or percentage must be TRUE") |
| 56 |
is.m <- is.missing(x) |
| 57 |
isNA <- is.na(x) |
| 58 |
style <- match.arg(style) |
| 59 |
if (style %in% c("table","codebook")) { |
| 60 |
vl <- labels(x) |
| 61 |
if(length(vl)){ |
| 62 |
vvl <- vl@values |
| 63 |
lvl <- [email protected] |
| 64 |
valid <- !is.missing2(vvl,[email protected]) |
| 65 |
i <- match([email protected],vvl,nomatch=0L) |
| 66 |
if(!length(weights)){ |
| 67 |
tab <- tabulate(i,nbins=length(vvl)) |
| 68 |
names(tab) <- as.character(vl@values) |
| 69 |
} |
| 70 |
else { |
| 71 |
f <- factor([email protected],levels=vvl) |
| 72 |
good <- is.finite(weights) & is.finite(f) |
| 73 |
tmp <- rowsum(weights[good],f[good]) |
| 74 |
tab <- structure(rep(0,nlevels(f[good])),names=levels(f)) |
| 75 |
tab[rownames(tmp)] <- tmp[] |
| 76 |
} |
| 77 |
lab <- if(style=="codebook") sQuote([email protected]) else [email protected] |
| 78 |
} |
| 79 |
else { |
| 80 |
valid <- logical(0) |
| 81 |
tab <- c() |
| 82 |
lab <- c() |
| 83 |
i <- logical(length(x)) |
| 84 |
} |
| 85 |
if(!length(weights)){ |
| 86 |
ovld <- sum(!is.m & !i) |
| 87 |
omiss <- sum(is.m & !i & !isNA) |
| 88 |
NAs <- sum(isNA) |
| 89 |
} |
| 90 |
else { |
| 91 |
good <- is.finite(weights) |
| 92 |
weights <- weights[good] |
| 93 |
is.m <- is.m[good] |
| 94 |
i <- i[good] |
| 95 |
isNA <- isNA[good] |
| 96 |
ovld <- sum(weights*(!is.m & !i)) |
| 97 |
omiss <- sum(weights*(is.m & !i & !isNA)) |
| 98 |
NAs <- sum(weights*(isNA)) |
| 99 |
} |
| 100 |
#browser() |
| 101 |
if(ovld){ |
| 102 |
tab <- c(tab," "=ovld) |
| 103 |
if(style=="codebook") |
| 104 |
lab <- c(lab,"(unlab.val.)") |
| 105 |
else { |
| 106 |
lab <- if(length(vl)) c(lab,"Other valid") else c(lab,"Valid") |
| 107 |
} |
| 108 |
valid <- c(valid,TRUE) |
| 109 |
} |
| 110 |
if(include.missings){ |
| 111 |
if(omiss){ |
| 112 |
tab <- c(tab," "=omiss) |
| 113 |
if(style == "codebook") |
| 114 |
lab <- c(lab,"(unlab.mss.)") |
| 115 |
else { |
| 116 |
if(length(vl)){ |
| 117 |
lab <- c(lab,"Other missing") |
| 118 |
} else { |
| 119 |
lab <- c(lab,"Missing") |
| 120 |
missing.marker <- "" |
| 121 |
} |
| 122 |
} |
| 123 |
valid <- c(valid,FALSE) |
| 124 |
} |
| 125 |
if(NAs){ |
| 126 |
tab <- c(tab,"NA"=NAs) |
| 127 |
if(style == "codebook") |
| 128 |
lab <- c(lab,"") |
| 129 |
else { |
| 130 |
lab <- c(lab,"NA") |
| 131 |
if(!length(vl)){ |
| 132 |
missing.marker <- "" |
| 133 |
} |
| 134 |
} |
| 135 |
valid <- c(valid,FALSE) |
| 136 |
} |
| 137 |
if(length(missing.marker)){ |
| 138 |
missing.marker <- missing.marker[1] |
| 139 |
if(style=="codebook"){ |
| 140 |
valid.marker <- paste(rep(" ",nchar(missing.marker)),collapse="") |
| 141 |
lab <- paste(ifelse(valid,valid.marker,missing.marker),lab) |
| 142 |
names(tab) <- paste(format(names(tab),justify="right"),format(lab,justify="left")) |
| 143 |
} |
| 144 |
else { |
| 145 |
lab <- paste(ifelse(valid,"",missing.marker),lab,sep="") |
| 146 |
names(tab) <- lab |
| 147 |
} |
| 148 |
} |
| 149 |
} else { |
| 150 |
if(style=="codebook") |
| 151 |
names(tab) <- paste(format(names(tab),justify="right"),format(lab,justify="left")) |
| 152 |
else |
| 153 |
names(tab) <- lab |
| 154 |
tab <- tab[valid] |
| 155 |
} |
| 156 |
} |
| 157 |
else { # style == "nolabels" |
| 158 |
if(include.missings){ |
| 159 |
if(!length(weights)){ |
| 160 |
NAs <- sum(isNA) |
| 161 |
tab <- table([email protected]) |
| 162 |
} |
| 163 |
else { |
| 164 |
good <- is.finite(weights) |
| 165 |
weights <- weights[good] |
| 166 |
NAs <- sum(weights*isNA) |
| 167 |
tab <- rowsum(weights,[email protected][good]) |
| 168 |
} |
| 169 |
if(NAs) |
| 170 |
tab <- c(tab,"NA"=NAs) |
| 171 |
if(length(missing.marker)){ |
| 172 |
missing.marker <- missing.marker[1] |
| 173 |
valid <- !is.missing2(sort(unique([email protected])),[email protected]) |
| 174 |
if(NAs) |
| 175 |
valid <- c(valid,FALSE) |
| 176 |
lab <- paste(ifelse(valid,"",missing.marker),names(tab),sep="") |
| 177 |
names(tab) <- lab |
| 178 |
} |
| 179 |
} |
| 180 |
else if(!length(weights)){ |
| 181 |
tab <- table([email protected][!is.m]) |
| 182 |
} |
| 183 |
else { |
| 184 |
good <- is.finite(weights) & !is.m |
| 185 |
weights <- weights[good] |
| 186 |
tab <- rowsum(weights,[email protected][good]) |
| 187 |
} |
| 188 |
} |
| 189 |
if(include.missings){ |
| 190 |
if(percentage && counts) { |
| 191 |
vperc <- rep(NA,length(tab)) |
| 192 |
vtab <- tab[valid] |
| 193 |
Nvalid <- sum(vtab) |
| 194 |
if(Nvalid) vperc[valid] <- 100 * vtab/Nvalid |
| 195 |
else vperc[valid] <- 0 |
| 196 |
tperc <- 100 * tab/sum(tab) |
| 197 |
tab <- cbind(Counts=tab,Valid=vperc,Total=tperc) |
| 198 |
} |
| 199 |
else if(percentage) { |
| 200 |
vperc <- rep(NA,length(tab)) |
| 201 |
vtab <- tab[valid] |
| 202 |
Nvalid <- sum(vtab) |
| 203 |
if(Nvalid) vperc[valid] <- 100 * vtab/Nvalid |
| 204 |
else vperc[valid] <- 0 |
| 205 |
tperc <- 100 * tab/sum(tab) |
| 206 |
tab <- cbind(Valid=vperc,Total=tperc) |
| 207 |
rownames(tab) <- names(tperc) |
| 208 |
} |
| 209 |
} |
| 210 |
else { |
| 211 |
if(percentage && counts) { |
| 212 |
perc <- 100 * tab/sum(tab) |
| 213 |
tab <- cbind(Counts=tab,Percent=perc) |
| 214 |
} |
| 215 |
else if(percentage) { |
| 216 |
tab <- 100 * tab/sum(tab) |
| 217 |
} |
| 218 |
} |
| 219 |
structure(tab,class="table") |
| 220 |
}) |
| 221 |
|
| 222 |
|
| 223 |
wmean <- function(x,w){ |
| 224 |
if(length(w)>0) weighted.mean(x,w) |
| 225 |
else mean(x) |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
NULLfunc <- function(...) NULL |
| 230 |
|
| 231 |
Descriptives_numeric <- function(x,weights=NULL,...){ |
| 232 |
|
| 233 |
isna <- is.na(x) |
| 234 |
if(length(weights)>0) |
| 235 |
w <- weights[!isna] |
| 236 |
else |
| 237 |
w <- NULL |
| 238 |
x <- x[!isna] |
| 239 |
m.1 <- tryCatch(wmean(1L*x,w),error=NULLfunc) |
| 240 |
x.cent <- tryCatch(x-m.1,error=NULLfunc) |
| 241 |
m.2 <- tryCatch(wmean(x.cent^2,w),error=NULLfunc) |
| 242 |
m.3 <- tryCatch(wmean(x.cent^3,w),error=NULLfunc) |
| 243 |
m.4 <- tryCatch(wmean(x.cent^4,w),error=NULLfunc) |
| 244 |
c( |
| 245 |
Min=min(x,na.rm=TRUE), |
| 246 |
Max=max(x,na.rm=TRUE), |
| 247 |
Mean=m.1, |
| 248 |
"Std.Dev."=tryCatch(sqrt(m.2),error=NULLfunc), |
| 249 |
Skewness=tryCatch(m.3/m.2^(3/2),error=NULLfunc), |
| 250 |
Kurtosis=tryCatch(m.4/m.2^2-3,error=NULLfunc) |
| 251 |
) |
| 252 |
} |
| 253 |
|
| 254 |
setMethod("Descriptives",signature(x="atomic"), |
| 255 |
Descriptives_numeric) |
| 256 |
|
| 257 |
setMethod("Descriptives",signature(x="ANY"), |
| 258 |
function(x,weights=NULL,...){ |
| 259 |
if(mode(x)=="numeric") |
| 260 |
Descriptives_numeric(x,weights,...) |
| 261 |
else NULL |
| 262 |
}) |
| 263 |
|
| 264 |
|
| 265 |
setMethod("Descriptives",signature(x="item.vector"), |
| 266 |
function(x,weights=NULL,...){ |
| 267 |
miss <- is.missing(x) |
| 268 |
if(length(weights)>0) |
| 269 |
w <- weights[!miss] |
| 270 |
else |
| 271 |
w <- NULL |
| 272 |
x <- [email protected][!miss] |
| 273 |
m.1 <- wmean(x,w) |
| 274 |
x.cent <- x-m.1 |
| 275 |
m.2 <- wmean(x.cent^2,w) |
| 276 |
m.3 <- wmean(x.cent^3,w) |
| 277 |
m.4 <- wmean(x.cent^4,w) |
| 278 |
c( |
| 279 |
Min=min(x,na.rm=TRUE), |
| 280 |
Max=max(x,na.rm=TRUE), |
| 281 |
Mean=m.1, |
| 282 |
"Std.Dev."=sqrt(m.2), |
| 283 |
Skewness=m.3/m.2^(3/2), |
| 284 |
Kurtosis=m.4/m.2^2-3 |
| 285 |
) |
| 286 |
}) |
| 287 |
|
| 288 |
Moments <- function(x) |
| 289 |
c( |
| 290 |
mm.1 = mean(x), |
| 291 |
mm.2 = mean(x^2), |
| 292 |
mm.3 = mean(x^3), |
| 293 |
mm.4 = mean(x^4), |
| 294 |
N = length(x) |
| 295 |
) |
| 296 |
|