forked from WinVector/WinVector.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVTreatExample.Rmd
More file actions
190 lines (159 loc) · 4.32 KB
/
VTreatExample.Rmd
File metadata and controls
190 lines (159 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
---
title: "VTreatExample"
author: "John Mount"
date: "September 12, 2015"
output: html_document
---
**Functions**
```{r functions}
library(ggplot2)
library(vtreat)
# ----
# for making data sets with both noise and weakly correlated variables
# ----
mkCoefs = function(ngood) {
nGoodN = ceiling(ngood/2)
nGoodC = ngood-nGoodN
coefs = list()
if(nGoodN>0) {
cN = lapply(seq_len(nGoodN),function(i) {
v = list()
v[[paste('gn', i, sep='_')]] = rnorm(1)
v
})
coefs = unlist(cN,recursive=FALSE)
}
if(nGoodN>0) {
cC = lapply(seq_len(nGoodC),function(i) {
v = list()
v[[paste('gc', i, sep='_')]] = list('a'=rnorm(1),
'b'=rnorm(1),
'c'=rnorm(1))
v
})
coefs = append(coefs,unlist(cC,recursive=FALSE))
}
coefs
}
# generate columns
genColumns = function(nrow,prefix,nNum,nCat) {
cols = list()
if(nNum>0) {
numCols = lapply(seq_len(nNum),function(i) rnorm(nrow))
names(numCols) = paste(prefix,'n_',seq_len(nNum),sep='')
cols = append(cols,numCols)
}
if(nCat>0) {
cCols = lapply(seq_len(nCat),function(i) {
sample(c('a','b','c'),nrow,replace=TRUE)
})
names(cCols) = paste(prefix,'c_',seq_len(nCat),sep='')
cols = append(cols,cCols)
}
data.frame(cols,stringsAsFactors=FALSE)
}
# evaluate coefs on a data frame
applyCoefs = function(coefs,d) {
v = numeric(nrow(d))
for(n in names(coefs)) {
cf = coefs[[n]]
if(is.list(cf)) {
# categorical
v = v + as.numeric(cf[d[,n,drop=TRUE]])
} else {
# numeric
v = v + cf[[1]]*d[,n,drop=TRUE]
}
}
v
}
# build a data frame with pure noise columns
# and columns weakly correlated with y
mkData = function(nrows, coefs, nnoise) {
noiseMagnitude = 1
d = data.frame(y = noiseMagnitude*rnorm(nrows),
stringsAsFactors = FALSE)
ngood = length(coefs)
if(ngood>0) {
ngC = sum(vapply(coefs,is.list,numeric(1)))
ngN = ngood - ngC
gd = genColumns(nrows,'g',ngN,ngC)
d = cbind(d,gd)
d$y = d$y + applyCoefs(coefs,d)
}
if(nnoise > 0) {
nnN = ceiling(nnoise/2)
nnC = nnoise-nnN
nd = genColumns(nrows,'n',nnN,nnC)
d = cbind(d,nd)
}
d$y = d$y > 0
d
}
# ------
# run vtreat variable scoring experiment and print/plot results
showVTreat <- function(dframe,yName,yTarget,parallelCluster) {
varnames <- setdiff(colnames(dframe),yName)
treatments = designTreatmentsC(dframe,
varnames,yName,yTarget,
verbose=FALSE,
parallelCluster=parallelCluster)
ord <- order(treatments$scoreFrame$csig)
sf <- treatments$scoreFrame[ord,]
print(sf[seq_len(min(20,nrow(sf))),])
goodIndices <- grep("^g",sf$origName)
print(goodIndices)
print(sf[goodIndices,])
sf$goodVar <- FALSE
sf$goodVar[goodIndices] <- TRUE
list(pd=ggplot(data=sf,aes(x=sig,color=goodVar,fill=goodVar)) +
geom_density(adjust=0.2,alpha=0.5) + scale_x_log10(),
ph=ggplot(data=sf,aes(x=sig,color=goodVar,fill=goodVar)) +
geom_histogram() + facet_wrap(~goodVar,ncol=1,scale="free_y") +
scale_x_log10())
}
```
```{r startclus}
nCoreEstimate <- parallel::detectCores()
print(paste('core estimate',nCoreEstimate))
parallelCluster = parallel::makeCluster(nCoreEstimate)
```
**Example 1**
Small example of evaluating a variable with signal, and without
```{r smallexample}
set.seed(3266)
N = 1000
g1 = rnorm(N)
n1 = rnorm(N)
y = 2*g1 + rnorm(N)
dframe = data.frame(y=y>0, g1=g1, n1=n1)
showVTreat(dframe,'y',TRUE,parallelCluster)[[2]]
```
**Example 2**
Small example of using chi-sq significance for variable filtering
```{r smallchiexample}
ngood = 5; nnoise = 5; N = 1000
coefs = mkCoefs(ngood)
dframe = mkData(N, coefs, nnoise)
summary(dframe)
print("True coefficients of signal variables")
print(coefs)
showVTreat(dframe,'y',TRUE,parallelCluster)
```
**Example 3**
Larger example; lots of noise
```{r bigchiexample}
ngood = 5; nnoise = 2000; N = 2500
coefs = mkCoefs(ngood)
dframe = mkData(N, coefs, nnoise)
varnames = setdiff(colnames(dframe), "y")
print("True coefficients of signal variables")
print(coefs)
showVTreat(dframe,'y',TRUE,parallelCluster)
```
```{r stopclus}
if(!is.null(parallelCluster)) {
parallel::stopCluster(parallelCluster)
parallelCluster <- NULL
}
```