-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmm_R.R
260 lines (176 loc) · 5.83 KB
/
fmm_R.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
rm(list=ls())
setwd("C:/Users/syedm/Desktop/Distance paper")
#Test on Lubbock data
data <- read.table("HHdata.csv", header=TRUE, sep=",")
Y <- I(data$Price)/100000
#house attributes
X <- cbind(1,I(data$SquareFoot)/1000, I(data$Lot)/1000, data$HouseAge, data$Garage, data$ExpBird)
#demographic variables/mixing variables
Z <- cbind(1,data$Educ, I(data$Inc)/10000, data$Age, data$HHSize)
ols_agg <- lm(Y~X-1);
summary(ols_agg)
#starting values for the hedonic estimates/betas for each type i.e. for mixing algorithm
types <- 3;
beta_start <- matrix(ols_agg$coef,(types*ncol(X)),1);
#starting values for the gamma estimates for the demographic variables
gamma_start <- matrix(0.01,(1*ncol(Z)),1);
#starting values for sigma
sigma_start <- matrix(sqrt(mean(ols_agg$residuals^2)),types,1)
#collecting initializing values
val_start <- c(beta_start,gamma_start,sigma_start);
vals <- val_start;
#convergence criteria comparing new and old estimates:
Iter_conv <- 0.0001;
j <- types;
#number of independent variables or beta estimates we need to keep track of - so to use when indexing
niv <- ncol(X);
#number of demographic variables to use when indexing
gvs <- ncol(Z);
#row dim of aggregate
n <- nrow(X);
conv_cg = 5000;
conv_cb = 5000;
par <- val_start
#FnOne prob density of observing prices given mean of cross product of house attributes and current
#iteration of hedonic estimates and sigma
FnOne <- function(par,x,y)
{
dnorm(y, mean=x%*%par[-1], sd = par[1], log=FALSE)
}
#FnTwo max prob densities over type probabilities
FnTwo <- function(par,d,x,y)
{
pdy <- matrix(0,n,j)
b <- par[1:(niv*j)]
s <- par[(niv*j+1):((niv+1)*j)]
for (i in 1:j)
{
pdy[,i] <- FnOne(c(s[i],b[((i-1)*niv+1):(i*niv)]),X,Y)
}
sum(d*log(pdy))
}
#FnThree logit for gamma estimates
FnThree <- function(g,z)
{
L <- exp(z%*%g)
}
#FnFour max gamma estimates, type probabilities
FnFour <- function(par, d, z, y, n, j, gvs) {
n <- nrow(X)
#j <- types
gvs <- ncol(Z)
L <- matrix(0, n, j)
L[,1] <- 1
# Set columns to 1 based on the value of types
if (j >= 3) {
L[,3] <- 1
}
if (j >= 4) {
L[,4] <- 1
}
# Continue with the original functionality
minus <- j - 1
for (m in 1:(j - minus)) {
L[, (m + 1)] <- FnThree(par[((m - 1) * gvs + 1):(m * gvs)], z)
}
Pi <- L / apply(L, 1, sum)
sum(apply(d * log(Pi), 1, sum))
}
#mixing algorithm
FMM <- function(par,X,Z,y)
{
b <- par[1:(j*niv)];
if (types == 2) {
g <- par[(j * niv + 1):(j * (niv + gvs) - gvs)]
s <- par[-(1:(j * (niv + gvs) - gvs))]
} else {
g <- par[(j * niv + 1):(length(par) - types)]
s <- par[-(1:(length(par) - types))]
}
L <- matrix(0,n,j);
f <- L;
d <- L;
b <- matrix(b,niv,j);
iter <- 0
while (abs(conv_cg) + abs(conv_cb) > Iter_conv) {
#store parameter estimates of preceding iteration of mix through loop
beta_old <- b;
gamma_old <- g;
#counter for while loop
iter <- iter+1
for (i in 1:j)
{
f[,i] <- FnOne(c(s[i],b[,i]),X,Y)
}
minus <- types - 1
for (i in 1:(j - minus)) {
L[,1] <- 0
if (types > 2) {
L[, (i + 1):(types - 1)] <- 0 # This sets the range of columns to 0 based on types
}
L[,(i + 1)] <- Z %*% g[((i - 1) * gvs + 1):(i * gvs)]
}
#estimate Pi (P) and individual probabilities of belonging to a certain type (d):
P <- exp(L)/(1+apply(exp(L[,(1:j)]),1,sum))
for (i in 1:n)
{
d[i,] <- P[i,]*f[i,]/sum(P[i,]*f[i,])
}
#use individual probs (d) to estimate beta (b), gamma (g)
b1 <- matrix(b,(niv*j),1); par1 <- c(b1,s);
beta_m <- optim(par1,FnTwo,d=d,x=X,y=Y,control=list(fnscale=-1,maxit=100000))
b <- matrix(beta_m$par[1:(j*niv)],niv,j)
s <- beta_m$par[(j*niv+1):(j*(niv+1))]
types <- j
gam_m <- optim(g,FnFour,j=types,d,z=Z,Y,control=list(fnscale=-1,maxit=100000))
g <- gam_m$par
#setting up convergence check
conv_cg <- sum(abs(g-gamma_old))
conv_cb <- sum(abs(b-beta_old))
#collecting parameter estimates to use to impute LL
par2 <- matrix(b,(niv*j),1)
par2 <- c(par2,s)
#types <- j
LL <- FnTwo(par2,d=d,x=X,y=Y) + FnFour(g,d=d,z=Z,y=Y, j=types);
#storing
bvector <- matrix(b,j*niv,1)
vals_fin <- c(bvector,g,s)
dvector <- d
}
#collecting parameters for output
out_pars <- list("vals_fin" = vals_fin, "i_type" = d)
print(b)
print(g)
print(iter)
#return list of estimates - index for subsetting in final updating
return(out_pars)
}
#calling:
mix <- FMM(val_start,X=X,Z=Z,y=Y)
end.time <- Sys.time()
start.time-end.time
#send_message(message_body=paste("Operation complete", Sys.time(), start.time-end.time))
#final updating:
d <- mix$i_type
b <- mix$vals_fin[1:(j*niv)]; #probabilities
#g <- mix$vals_fin[(j*niv+1):((j*(niv+gvs)-gvs))];
g <- mix$vals_fin[(j*niv+1):(length(par)-types)];
#s <- mix$vals_fin[-(1:(j*(niv+gvs)-gvs))];
s <- mix$vals_fin[-(1:(length(par)-types))];
b <- matrix(b,niv,j); #betas
b1 <- matrix(b,(niv*j),1);
par3 <- c(b1,s);
#standard errors #needs works
beta_opt <- optim(par3,FnTwo,d=d,x=X,y=Y,control=list(fnscale=-1,maxit=10000),hessian=TRUE)
b <- matrix(beta_opt$par[1:(j*niv)],niv,j);
bse1 <- sqrt(-diag(solve(beta_opt$hessian[1:niv,1:niv])))
bse2 <- sqrt(-diag(solve(beta_opt$hessian[(niv+1):(2*niv),(niv+1):(2*niv)])))
bse3 <- sqrt(-diag(solve(beta_opt$hessian[(niv*2+1):(3*niv),(niv*2+1):(3*niv)])))
s <- beta_opt$par[(j*niv+1):(j*(niv+1))]
gamma_opt <- optim(g,FnFour,d=d,z=Z,y=Y,control=list(fnscale=-1,maxit=100000),hessian=TRUE)
g <- gamma_opt$par
gse1 <- sqrt(-diag(solve(gamma_opt$hessian[1:gvs,1:gvs])))
par2 <- matrix(b,(niv*j),1);
par2 <- c(par2,s)
LL <- FnTwo(par3,d=d,x=X,y=Y) + FnFour(g,d=d,z=Z,y=Y);
AIC <- -2*LL+2*niv