-
Notifications
You must be signed in to change notification settings - Fork 0
/
GREG-function.R
166 lines (136 loc) · 4.18 KB
/
GREG-function.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
## inastall packages
library(statmod)
library(glmnet)
library(MASS)
library(MCMCpack)
library(olsrr)
Tr <- function(A){ sum(diag(A)) }
GREG <- function(sY, sX, sPi, mX, shrink="NO", samp="SRS"){
pp = dim(sX)[2]
nf = 10
if(shrink=="NO"){
fit = lm(sY~-1+sX,weights=1/sPi)
hbeta = coef(fit)
Resid = as.vector(sY-predict(fit))
est = sum(na.omit(mX*hbeta))+sum(Resid/sPi)/N
Aux = hbeta
names(Aux) = paste0("Beta",1:pp)
}
if(shrink=="Lasso"){
opt.lam = cv.glmnet(sX,sY,family="gaussian",alpha=1,weights=1/sPi,nfolds=nf)$lambda.min
Lasso = glmnet(sX,sY,family="gaussian",alpha=1,weights=1/sPi,lambda=opt.lam)
hbeta = coef(Lasso)
Resid = as.vector(sY-cbind(1,sX)%*%hbeta)
est = as.vector(c(1,mX)%*%hbeta+sum(Resid/sPi)/N)
Aux = c(opt.lam,as.vector(hbeta))
names(Aux) = c("Lam",paste0("Beta",0:pp))
}
if(shrink=="Ridge"){
opt.lam = cv.glmnet(sX,sY,family="gaussian",alpha=0,weights=1/sPi,nfolds=nf)$lambda.min
Lasso = glmnet(sX,sY,family="gaussian",alpha=0,weights=1/sPi,lambda=opt.lam)
hbeta = coef(Lasso)
Resid = as.vector(sY-cbind(1,sX)%*%hbeta)
est = as.vector(c(1,mX)%*%hbeta+sum(Resid/sPi)/N)
Aux = c(opt.lam,as.vector(hbeta))
names(Aux) = c("Lam", paste0("Beta",0:pp))
}
# variance estimation
if(samp=="Pois"){ VV = sum(Resid^2*(1-sPi)/sPi^2)/N^2 }
if(samp=="SRS"){
Delta = matrix(NA,n,n)
for(i in 1:n){
Delta[i,i] = 1-n/N
pp2 = n*(n-1)/(N*(N-1))
Delta[i,-i] = 1-(n/N)^2/pp2
}
VV = t(Resid/sPi)%*%Delta%*%(Resid/sPi)/N^2
}
if(samp=="PPS"){
VV = n*var(Resid/sPi)/N^2
}
# results
result=list(Greg=c(est,VV), Aux=Aux)
return(result)
}
# GREG: forward selection
GREG.forward <- function(sY, sX, sPi, mX, samp="SRS"){
p <- dim(sX)[2]
model <- lm(sY~., data=data.frame(sY, sX), weights=1/sPi)
VS <- ols_step_forward_p(model)
Ind <- sort( as.numeric( factor(VS$predictors, levels=paste0("X",1:p)) ) )
hbeta <- rep(0, p)
hbeta[Ind] <- coef(lm(sY~-1+sX[,Ind], weights=1/sPi))
pred <- as.vector(sX%*%hbeta)
Resid <- sY-pred
est <- sum(na.omit(mX*hbeta))+sum(Resid/sPi)/N
# variance estimation
if(samp=="Pois"){ VV = sum(Resid^2*(1-sPi)/sPi^2)/N^2 }
if(samp=="SRS"){
Delta = matrix(NA,n,n)
for(i in 1:n){
Delta[i,i] = 1-n/N
pp2 = n*(n-1)/(N*(N-1))
Delta[i,-i] = 1-(n/N)^2/pp2
}
VV = t(Resid/sPi)%*%Delta%*%(Resid/sPi)/N^2
}
if(samp=="PPS"){
VV = n*var(Resid/sPi)/N^2
}
# results
result <- list(Greg=c(est,VV), Aux=hbeta)
return(result)
}
# GREG: mixed model approach
GREG.mixed <- function(sY, sX, sPi, mX, samp="SRS"){
p <- dim(sX)[2]
DD <- diag(sPi)
invDD <- diag(1/sPi)
mat <- sX%*%t(sX)
Q <- function(vv){
psi <- vv[1]
sig2 <- vv[2]
Sig <- psi*mat + sig2*DD
val <- sum(log( eigen(Sig)$value )) + as.vector( t(sY)%*%solve(Sig)%*%sY )
return(val)
}
est.vv <- optim(par=c(0.1, mean(1/sPi)), fn=Q, method="L-BFGS-B", lower=rep(0.001, 2), upper=rep(1000, 2))$par
hpsi <- est.vv[1]/est.vv[2]
mat <- solve( t(sX)%*%invDD%*%sX+hpsi*diag(p) )
hbeta <- as.vector( mat%*%t(sX)%*%invDD%*%sY )
# point estimate
pred <- as.vector(sX%*%hbeta)
Resid <- sY-pred
est <- sum(mX*hbeta) + sum(Resid/sPi)/N
# variance estimation
H <- mat%*%t(sX)%*%invDD%*%sX
alpha <- 1/(N*sPi)
XHT <- apply(sX/sPi, 2, sum)/N
ww <- alpha + as.vector( t(mX-XHT)%*%H%*%solve(t(sX)%*%invDD%*%sX)%*%t(sX) )/sPi
if(samp=="SRS"){
Delta = matrix(NA,n,n)
for(i in 1:n){
Delta[i,i] = 1-n/N
pp2 = n*(n-1)/(N*(N-1))
Delta[i,-i] = 1-(n/N)^2/pp2
}
V1 <- t(Resid*ww)%*%Delta%*%(Resid*ww)
mat1 <- t(sX*Resid*ww)%*%Delta%*%(sX*Resid*ww)*N^2
VXH <- t(sX/sPi)%*%Delta%*%(sX/sPi)/N^2
}
if(samp=="PPS"){
V1 = n*var(Resid*ww)
UU <- sX*Resid*ww*N
sUU <- scale(t(UU), scale=F)
mat1 <- n*sUU%*%t(sUU)/(n-1)
VXH <- n*var(sX/sPi)/N^2
}
mat2 <- ginv( t(sX)%*%invDD%*%sX )
V.beta <- mat2%*%mat1%*%mat2
GG <- solve(t(sX)%*%invDD%*%sX + diag(p)/hpsi)%*%t(sX)%*%invDD%*%sX - diag(p)
V2 <- Tr( (hbeta%*%t(hbeta) - V.beta)%*%t(GG)%*%VXH%*%GG )
VV <- V1 + V2
# results
result <- list(Greg=c(est,VV), Aux=hbeta)
return(result)
}