Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sdcTools/sdcMicro
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhard-da committed May 3, 2019
2 parents d8d0f54 + 9c5b841 commit 5b97708
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion R/mafast.R
Expand Up @@ -134,6 +134,6 @@ mafastWORK <- function(x, variables=colnames(x), by=NULL, aggr=3, measure=mean)
eval(parse(text=cmd))
x <- x[, !colnames(x) %in% c("BYVARIABLEFORSPLIT", "idvariableforresorting"), drop=FALSE]
setkey(erg, "idvariableforresorting")
x[, variables] <- data.frame(erg[, by=idvariableforresorting])[, variables]
x[, variables] <- data.frame(erg)[, variables]
return(x)
}
13 changes: 6 additions & 7 deletions R/mvTopCoding.R
Expand Up @@ -8,7 +8,7 @@
#' @param x object of class matrix with numeric entries
#' @param maha squared mahalanobis distance of each observation
#' @param center center of data, needed for calcualtion of mahalanobis distance (if not provide)
#' @param sigma covariance matrix of data, needed for calcualtion of mahalanobis distance (if not provide)
#' @param cov covariance matrix of data, needed for calcualtion of mahalanobis distance (if not provide)
#' @param alpha significance level, determining the ellipsoide to which outliers should be placed upon
#' @return the imputed winsorized data
#' @importFrom robustbase covMcd
Expand Down Expand Up @@ -50,13 +50,12 @@
#'
mvTopCoding <- function(x, maha=NULL,center=NULL,
cov=NULL, alpha=0.025){
stopifnot(is.numeric(x) || is.logical(x), is.atomic(x))

if(is.data.frame(x) | is.data.table(x)){
stop("x must be of class matrix")
}
# if(!is.data.table(x))x <- as.data.table(x)
# stopifnot(is.numeric(x) || is.logical(x), is.atomic(x))

# if(is.data.frame(x) | is.data.table(x)){
# stop("x must be of class matrix")
# }
if(!is.data.table(x)) x <- as.data.table(x)
p <- ncol(x)
d <- qchisq(1-alpha,df=p)

Expand Down
4 changes: 2 additions & 2 deletions man/mvTopCoding.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions tests/microaggregation_test.R

This file was deleted.

27 changes: 27 additions & 0 deletions tests/testthat/microaggregation_test.R
@@ -0,0 +1,27 @@
require(sdcMicro)
### for a data.frame
test_that("pram on a factor", {
dat <- data.frame(x = rnorm(100))
datout <- microaggregation(obj = dat, variables = "x", method = "mdav", aggr = 3, measure = "mean")
expect_identical(dat,datout$x)
expect_identical(dim(dat),dim(datout$mx))

dat <- data.frame(x = rnorm(100), y = rnorm(100))
datout <- microaggregation(obj = dat, variables = "x", method = "mdav", aggr = 3, measure = "mean")
expect_identical(dat,datout$x)
expect_identical(dim(dat),dim(datout$mx))

datout <- microaggregation(obj = dat, variables = c("x", "y"), method = "mdav", aggr = 3, measure = "mean")
expect_identical(dat,datout$x)
expect_identical(dim(dat),dim(datout$mx))

# for a sdcObj
set.seed(199723)
activedataset <- testdata
sdcObject <- createSdcObj(activedataset, keyVars = c("urbrur", "roof", "sex", "age"),
numVars = c("expend", "income", "savings"),
weightVar = c("sampling_weight"), hhId = c("ori_hid"))
expect_message(microaggregation(sdcObject, aggr = c(3), method = c("mdav"),
variables = c("expend"), strata_variables = c("sex")))

})

0 comments on commit 5b97708

Please sign in to comment.