-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
152 lines (152 loc) · 3.72 KB
/
.Rhistory
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
library(renv)
renv::init()
renv::snapshot()
usethis::use_package(package = "MASS", type = "Imports")
usethis::use_readme_rmd()
file.create("R/EM_miss.R")
install.packages("xfun")
(1454/75) - (377/37)*e^(-1/10)
(1454/75) - (377/37)*expit(-1/10)
(1454/75) - (377/37)*exp(-1/10)
48*5/14
3261/79954
52487/252
151/6
2/63
10/21
(77252-75578)/79954
52487/252
1674/77252
841.624 -(25.166)^2
file.create("R/EM_miss_var.R")
devtools::document()
devtools::document()
devtools::document()
devtools::document()
library(MRME)
?EM_miss
library(MRME)
??EM_miss
library(MRME)
??EM_miss
devtools::document()
devtools::document()
devtools::document()
library(MRME)
??EM_miss
library(MRME)
??EM_Miss
devtools::document()
library(MRME)
devtools::document()
library(MRME)
??EM_miss
devtools::document()
library(MRME)
??EM_miss
library(MRME)
library(MRME)
library(MRME)
devtools::document()
library(MRME)
usethis::use_package(package = "MASS", type = "Suggests")
help(usethis::use_package)
help(use_package)
devtools::document()
devtools::document()
library(MRME)
devtools::document()
library(MRME)
devtools::document()
library(MRME)
usethis::use_package(package = "MASS", type = "Suggests")
devtools::document()
library(MRME)
library(MRME)
library(MASS)
N = 10000 #the sample size
K = 1 #the number of instrumental variables
n = 2
p = 0.3
#Sigma matrix
Sigma = matrix(c(0.58,0.35,0.35,0.58),2,2)
######parameter set
beta0 = 0.8
beta1 = 0.2
#alpha_vec = as.vector(runif(6, 0.05, 0.15))
alpha0 = 0.13
alphav = 0.2
epsilon = mvrnorm(N,rep(0,2),Sigma)
#########genetic matrix
G<-rbinom(N*K,n,p)
######## generate X and Y
X = alpha0 + as.matrix(G)%*%alphav + epsilon[,1]
h2G_X = var(as.matrix(G)%*%alphav)/var(X)
print(h2G_X)
Y = beta0 + beta1 *X +epsilon[,2]
h2G_Y = var(beta1*(as.matrix(G)%*%alphav))/var(Y)
#data = data.frame(Y = Y,X = X,G = G)
#res_true = summary(ivreg(Y ~X|G, data = data))
#est_true = res_true$coefficients[2,1]
#se_true = res_true$coefficients[2,2]
########generate missing X, missing rate 30%
p_miss = exp(0.25 + 0.6*Y + as.matrix(G)%*%rep(0.05,1))/(1 + exp(0.25 +0.6*Y + as.matrix(G)%*%rep(0.05,1)))
#summary(p_miss)
#print(1-mean(p_miss))
#R= rbinom(N,1,p_miss)
#R_miss = which(R == 0)
#X[R_miss]=NA
maxit = 1000
tol = 1e-8
Sigma_start= matrix(c(0.4,0.2,0.2,0.4),2,2)
#Sigma = Sigma_start
para_start = c(0.6,0.1,rep(0.05,2))
#para = para_start
result = EM_miss(para_start,Sigma_start,G,Y,X,maxit,tol)
par_est = result$para
Sigma_est = result$Sigma
result_var = EM_miss_var(Sigma_est, par_est, G,Y,X)
library(MRME)
library(MASS)
N = 10000 #the sample size
K = 1 #the number of instrumental variables
n = 2
p = 0.3
#Sigma matrix
Sigma = matrix(c(0.58,0.35,0.35,0.58),2,2)
######parameter set
beta0 = 0.8
beta1 = 0.2
#alpha_vec = as.vector(runif(6, 0.05, 0.15))
alpha0 = 0.13
alphav = 0.2
epsilon = mvrnorm(N,rep(0,2),Sigma)
#########genetic matrix
G<-rbinom(N*K,n,p)
######## generate X and Y
X = alpha0 + as.matrix(G)%*%alphav + epsilon[,1]
h2G_X = var(as.matrix(G)%*%alphav)/var(X)
print(h2G_X)
Y = beta0 + beta1 *X +epsilon[,2]
h2G_Y = var(beta1*(as.matrix(G)%*%alphav))/var(Y)
#data = data.frame(Y = Y,X = X,G = G)
#res_true = summary(ivreg(Y ~X|G, data = data))
#est_true = res_true$coefficients[2,1]
#se_true = res_true$coefficients[2,2]
########generate missing X, missing rate 30%
p_miss = exp(0.25 + 0.6*Y + as.matrix(G)%*%rep(0.05,1))/(1 + exp(0.25 +0.6*Y + as.matrix(G)%*%rep(0.05,1)))
#summary(p_miss)
#print(1-mean(p_miss))
#R= rbinom(N,1,p_miss)
#R_miss = which(R == 0)
#X[R_miss]=NA
maxit = 1000
tol = 1e-8
Sigma_start= matrix(c(0.4,0.2,0.2,0.4),2,2)
#Sigma = Sigma_start
para_start = c(0.6,0.1,rep(0.05,2))
#para = para_start
result = EM_miss(para_start,Sigma_start,G,Y,X,maxit,tol)
par_est = result$para
Sigma_est = result$Sigma
result_var = EM_miss_var(Sigma_est, par_est, G,Y,X)