-
Notifications
You must be signed in to change notification settings - Fork 2
/
lmf_impute.R
151 lines (127 loc) · 3.46 KB
/
lmf_impute.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
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
## SVD Imputation Approaches
# Initialize using column or row averages
# Compute SVD
# Regress missing values on SVD components
lmf.impute <- function (x,y,z,r) {
# 1) Initialize using row and column averages
mindex <- is.na(x)
mcol <- colMeans(x, na.rm=T)
mrow <- rowMeans(x, na.rm=T)
mtot <- mean(x, na.rm=T)
last <- 0
xtemp <- x
for (i in 1:nrow(x)) {
for (j in 1:ncol(x)) {
# For single missing values, average col and row means
xtemp[i,j] <- (mrow[i] + mcol[j]) / 2
}
}
for (i in 1:nrow(x)) {
if (is.na(mrow[i])) {
xtemp[i,] <- mcol
}
}
for (j in 1:ncol(x)) {
if (is.na(mcol[j])) {
xtemp[,j] <- mrow
}
}
for (i in 1:nrow(x)) {
for (j in 1:ncol(x)) {
if (is.na(mcol[j]) & is.na(mrow[i])) {
xtemp[i,j] <- mtot
}
}
}
xtemp[!mindex] <- x[!mindex]
## Replace missing values with average of row and column mean
#rcmeans <- (matrix(rep(mcol,nrow(x)), nrow(x), ncol(x)) +
# matrix(rep(mcol,ncol(x)), nrow(x), ncol(x))) / 2
#xtemp[mindex] <- rcmeans[mindex]
#colmiss <- is.na(mcol)
#rowmiss <- is.na{mrow}
conv <- F
while(!conv) {
# 2) Compute rank r Joint LMF
stemp <- lmfJ(xtemp, y, z, r, scale=F)
# 3) Impute missing values using LMF in (2) (EM algorithm)
last <- xtemp
xfull <- stemp$Jx
xtemp[mindex] <- xfull[mindex]
# Check for convergence
if (norm(xtemp - last, type='f')^2 / norm(xtemp, type='f')^2 < .0001) { conv <- T }
}
return(xtemp)
}
# Try first coding these up for a single matrix (SVD)
svd.impute <- function (x, r) {
# 1) Initialize using column averages
mindex <- is.na(x)
mcol <- colMeans(x, na.rm=T)
mrow <- rowMeans(x, na.rm=T)
mtot <- mean(x, na.rm=T)
last <- 0
xtemp <- x
for (i in 1:nrow(x)) {
for (j in 1:ncol(x)) {
# For single missing values, average col and row means
xtemp[i,j] <- (mrow[i] + mcol[j]) / 2
}
}
for (i in 1:nrow(x)) {
if (is.na(mrow[i])) {
xtemp[i,] <- mcol
}
}
for (j in 1:ncol(x)) {
if (is.na(mcol[j])) {
xtemp[,j] <- mrow
}
}
for (i in 1:nrow(x)) {
for (j in 1:ncol(x)) {
if (is.na(mcol[j]) & is.na(mrow[i])) {
xtemp[i,j] <- mtot
}
}
}
xtemp[!mindex] <- x[!mindex]
### LOOP 2 and 3 until convergence
conv <- F
while(!conv) {
# 2) Compute rank r SVD
stemp <- svd(xtemp, nu=r, nv=r)
# 3) Impute missing values using SVD in (2) (EM algorithm)
if (r>0) {
xfull <- stemp$u[,1:r] %*% diag(x=stemp$d[1:r], nrow=r) %*% t(stemp$v[,1:r])
} else if (r==0) {
xfull <- matrix(0,nrow(x),ncol(x))
}
last <- xtemp
xtemp[mindex] <- xfull[mindex]
# Check for convergence
if (norm(xtemp - last, type='f')^2 / norm(xtemp, type='f')^2 < .0001) { conv <- T }
}
return(xtemp)
}
# SVD only (no regression iteration)
svd.only.impute <- function (x, r) {
# 1) Initialize using column averages
mindex <- is.na(x)
mcol <- colMeans(x, na.rm=T)
last <- 0
xtemp <- x
for (i in 1:ncol(x)) {
xtemp[which(mindex[,i]),i] <- mcol[i]
}
# 2) Compute rank r SVD
stemp <- svd(xtemp, nu=r, nv=r)
# 3) Impute missing values using SVD in (2) (EM algorithm)
if (r>0) {
xfull <- stemp$u[,1:r] %*% diag(x=stemp$d[1:r], nrow=r) %*% t(stemp$v[,1:r])
} else if (r==0) {
xfull <- matrix(0,nrow(x),ncol(x))
}
xtemp[mindex] <- xfull[mindex]
return(xtemp)
}