-
Notifications
You must be signed in to change notification settings - Fork 2
/
06-newtargets.r
48 lines (30 loc) · 1.28 KB
/
06-newtargets.r
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
install.packages("yaImpute")
library(yaImpute)
library(randomForest)
library(dplyr)
library(magrittr)
data(iris)
# set the random number seed so that example results are consistant
# normally, leave out this command
set.seed(12345)
# form some test data
refs=sample(rownames(iris),50) # just the reference observations
x <- iris[refs,1:3] # Sepal.Length Sepal.Width Petal.Length
y <- iris[refs,4:5] # Petal.Width Species
# build a yai object using mahalanobis
mal <- yai(x=x,y=y,method="mahalanobis")
which(rownames(iris) %in% rownames(x))
iris[!(rownames(iris) %in% rownames(x)),] %>% dim
# get imputations for the target observations (not references)
malNew <- newtargets(mal,iris[!(rownames(iris) %in% rownames(x)),])
# output a data frame of observed and imputed values for
# the observations that are not in the original yai object
myImpute <- impute(malNew,vars=yvars(malNew))
# in this example, Y is not specified (not required for mahalanobis).
mal2 <- yai(x=x,method="mahalanobis")
identical(foruse(mal),foruse(mal2))
# here, method randomForest's unsupervised classification is used (no Y).
rf <- yai(x=x,method="randomForest")
# now get imputations for the targets in the iris data (those that are
# not references).
rfNew <- newtargets(rf,iris[!(rownames(iris) %in% rownames(x)),])