Skip to content

Commit 16ec027

Browse files
committed
fix NA bug in custom coder
allow more centerig in custom coders
1 parent 78dc83a commit 16ec027

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
# vtreat 1.4.3 2019/07/06
33

4+
* Fix bug in custom coder NA path and let numeric coder center if wanted.
45
* More tests.
56

67
# vtreat 1.4.2 2019/07/01

R/customCoder.R

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@ makeCustomCoder <- function(customCode, coder, codeSeq,
4949
}
5050
if((!is.numeric(scores)) || (length(scores)!=length(vcol))) {
5151
scores <- rep(0.0, length(vcol))
52-
} else {
53-
if('center' %in% codeSeq) {
54-
# shift scores to be mean zero with respect to weights
55-
scores <- scores - sum(scores*weights)/sum(weights)
56-
}
5752
}
58-
missingValueCode <- sum(scores * weights)/sum(weights)
53+
if('center' %in% codeSeq) {
54+
# shift scores to be mean zero with respect to weights
55+
scores <- scores - .wmean(scores, weights)
56+
}
57+
missingValueCode <- .wmean(scores, weights)
5958
d <- data.frame(x = vcol,
6059
pred = scores)
6160
# TODO: weighted version
@@ -155,11 +154,11 @@ makeCustomCoderNum <- function(customCode, coder, codeSeq,
155154
return(NULL)
156155
}
157156
cuts <- c(min(xNotNA), max(xNotNA))
158-
if(sum(napositions)>0) {
159-
missingValueCode <- .wmean(zoY[napositions], weights[napositions])
160-
} else {
161-
missingValueCode <- .wmean(yNotNa, wNotNa)
157+
if('center' %in% codeSeq) {
158+
# shift scores to be mean zero with respect to weights
159+
yNotNa <- yNotNa - .wmean(yNotNa, wNotNa)
162160
}
161+
missingValueCode <- .wmean(yNotNa, wNotNa)
163162
extraModelDegrees <- max(0,length(unique(xNotNA)))
164163

165164
scores <- NULL

0 commit comments

Comments
 (0)