-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR functions: EQ for MR.Rmd
402 lines (348 loc) · 28.3 KB
/
R functions: EQ for MR.Rmd
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
---
title: "Thesis R Functions Udi Alter 2021"
author: "Udi Alter"
date: "30/04/2021"
---
```{r packages}
library(haven)
library(psych)
library(car)
library(psychometric)
library(QuantPsyc)
library(lsr)
library(apaTables)
library(dplyr)
library(broom)
```
# The *reg.equiv.fd* Function
### (when full data is available)
```{r reg.equiv.fd function}
reg.equiv.fd <- function(datfra, model, predictor, delta, alpha=.05, plot=TRUE, test="TOST", std=TRUE) {
# reg. coefficient point estimate and ci's
modres <- summary(model)
b.num <- grep(predictor, attr(model$terms , "term.labels"))+1 # this will indicate the place/number of the predictor in the model (e.g., 1st pred.) accounts for the intercept as well
b <- model$coefficients[b.num] # extract raw coefficient value
l.ci <- confint(model)[predictor,][1] # extract raw coefficient value, lower bound 95%
u.ci <- confint(model)[predictor,][2] # extract raw coefficient value, upper bound 95%
l.ci90 <- confint(model, level = .9)[predictor,][1] # extract raw coefficient value, lower bound 90%
u.ci90 <- confint(model, level = .9)[predictor,][2] # extract raw coefficient value, upper bound 90%
# std.error, degrees of freedom, and prerequisites
err <- modres$coefficients[b.num,2] # extract standard error for predictor
df <- model$df.residual # extract degrees of freedom for model
depname <- attr(model$terms, "variables")[[2]] # extract name of outcome variable
dep <- as.double(unlist(datfra[,depname])) # extract raw dependent variable values
pred <- as.double(unlist(datfra[,predictor])) # extract raw (tested) predictor variable values
#delta
l.delta <- -abs(delta) # lower equivalence interval bound (consider to make it as input in the function)
u.delta <- abs(delta) # upper equivalence interval bound
# standardized forms (beta, variables, delta, ci's)
beta <- data.frame(lm.beta(model))[predictor,] # extract standardized coefficient value
beta.err <- err*beta/b
dep.z <- (dep-mean(dep))/sd(dep) # constructing z scores for the dependent variable
pred.z <- (pred-mean(pred))/sd(pred) # constructing z scores for the predictor variable
std.delta <- delta*sd(pred)/sd(dep) # constructing a standardized form of delta
u.std.delta <- abs(std.delta) # standardized delta, upper
l.std.delta <- -abs(std.delta) # standardized delta, lower
l.std.ci <- (confint(model)[predictor,][1])*sd(pred)/sd(dep) # converting lower 95% ci to standardized form
u.std.ci <- (confint(model)[predictor,][2])*sd(pred)/sd(dep) # converting upper 95% ci to standardized form
l.std.ci90 <- (confint(model, level = .9)[predictor,][1])*sd(pred)/sd(dep) # converting lower 90% ci to standardized form
u.std.ci90 <- (confint(model, level = .9)[predictor,][2])*sd(pred)/sd(dep) # converting upper 90% ci to standardized form
# RESULTS SECTION
# standardized section
if (std==TRUE) {
std.delta <- delta
u.std.delta <- abs(std.delta)
l.std.delta <- -abs(std.delta)
raw.delta <- delta*sd(dep)/sd(pred)
u.raw.delta <- abs(raw.delta)
l.raw.delta <- -abs(raw.delta)
if (test=="AH") {
# AH
t.std.ah <- beta/beta.err
p.std.ah <- pt((abs(beta)-std.delta)/(sqrt(beta.err^2)),df) - pt((-abs(beta)-std.delta)/(sqrt(beta.err^2) ), df)
#cat("standardized regression coefficient for", predictor,":", "\n",
#"β = ",beta," , 95% CI [",l.std.ci,",",u.std.ci,"]" ,"\n",
#"associated std. error = ", beta.err, "\n", "\n",
#"std. equivalence interval =","[",l.std.delta,",",u.std.delta,"]", "\n",
#"\n",
#
cat("unstandardized regression coefficient for", predictor,":", "\n",
"b = ",b," , 95% CI [",l.ci,",",u.ci,"]" ,"\n",
"std. error = ", err, "\n", "\n",
"standarized regression coefficient for", predictor,":","\n",
"β = ", beta,", 95% CI [",l.std.ci,",",u.std.ci,"]", "\n",
"associated std. error = ", beta.err, "\n", "\n",
"raw equivalence interval =","[",l.raw.delta,",",u.raw.delta,"]", "\n",
"std. equivalence interval =","[",l.std.delta,",",u.std.delta,"]", "\n",
"\n",
"Anderson-Hauck (AH) Equivalence Test Results:", "\n",
"-------------------------------------------------", "\n",
"t(",df,") = ",t.std.ah,"\n",
"p = ",p.std.ah,"\n",
ifelse(p.std.ah < alpha, "AH: Negligible effects concluded" , 'AH: Insufficient evidence for negligible effects'))
} else {
# TOST
t1.std.tost<- (beta-(-std.delta))/(sqrt(beta.err^2))
t2.std.tost<- (std.delta-beta)/(sqrt(beta.err^2))
p1.std.tost<- 1-pt(t1.std.tost, df)
p2.std.tost<- 1-pt(t2.std.tost, df)
#cat("standardized regression coefficient for", predictor,":", "\n",
#"β = ",beta," , 95% CI [",l.std.ci,",",u.std.ci,"]" ,"\n",
#"associated std. error = ", beta.err, "\n", "\n",
#"std. equivalence interval =","[",l.std.delta,",",u.std.delta,"]", "\n",
cat("unstandardized regression coefficient for", predictor,":", "\n",
"b = ",b," , 95% CI [",l.ci,",",u.ci,"]" ,"\n",
"std. error = ", err, "\n", "\n",
"standarized regression coefficient for", predictor,":","\n",
"β = ", beta,", 95% CI [",l.std.ci,",",u.std.ci,"]", "\n",
"associated std. error = ", beta.err, "\n", "\n",
"raw equivalence interval =","[",l.raw.delta,",",u.raw.delta,"]", "\n",
"std. equivalence interval =","[",l.std.delta,",",u.std.delta,"]", "\n",
"\n",
"Two One-Sided Test (TOST) Equivalence Test Results:", "\n",
"-------------------------------------------------", "\n",
"t1(",df,") = ",t1.std.tost,"\n",
"t2(",df,") = ", t2.std.tost, "\n",
"p1 = ",p1.std.tost,"\n",
"p2 = ",p2.std.tost,"\n",
ifelse(p1.std.tost < alpha & p2.std.tost < alpha, 'TOST: Negligible effects concluded', 'TOST: Insufficient evidence for negligible effects'))
}
# plot
if (plot==TRUE) {
plot(NA, axes=F,
xlim = c(min(l.std.ci,l.std.delta)-max(u.std.ci-l.std.ci, u.std.delta-l.std.delta)/10, max(u.std.ci,u.std.delta)+max(u.std.ci-l.std.ci, u.std.delta-l.std.delta)/10),
ylim = c(0,1),
yaxt='n',
ylab="",
xlab = "Std. Regression Coefficient Estimate",
main = "Symmetric CI Approach \n 90% CI")
abline(v =0 , lty = 2, col= "light grey") # vertical line in the middle of the eq. interval, right now it's always 0, can be changed
points(x=beta, y=.5, pch=8, cex=2) # point at the estimated predictor value
abline(v=u.std.delta, lty=2, col = "red") # mark the upper eq. bound
abline(v=l.std.delta, lty=2, col = "red") # mark the lower eq. bound
segments(l.std.ci90,0.5,u.std.ci90,0.5, lwd=3) # plotting the 90% CI for the predictor estimate
text(u.std.delta*1.01,.5,"upper equivalence bound",srt=270,pos=3, offset = .5, col = "red") # text for eq. bound line (upper)
text(l.std.delta*1.01,.5,"lower equivalence bound",srt=90,pos=3, offset = .5, col = "red") # text for eq. bound line (lower)
text(u.std.delta*.9,-0.01,u.std.delta,srt=0,pos=3, offset = .5, col = "red") # writing the eq. interval bound value (upper)
text(l.std.delta*0.88,-.01,l.std.delta,srt=0,pos=3, offset = .5, col = "red") # writing the eq. interval bound value (lower)
text(beta,.57,round(beta, digits = 3),srt=0) # writing the predictor point estimate value
text(beta,.57,"β=",srt=0, pos = 2, offset = 1.5) # adding text to indicate above line
text(u.std.ci90,.45,round(u.std.ci90, digits = 3),pos =1, offset = .1, col = "black") # writing the 90% CI upper limit value for the predictor estimate
text(l.std.ci90,.45,round(l.std.ci90, digits = 3), pos =1, offset = .1,col = "black") # writing the 90% CI lower limit value for the predictor estimate
axis(side=1, pos=0, lwd.ticks=0)
}
} else {
# unstandardized section
if (test=="AH") {
# AH
t.ah <- modres$coefficients[b.num,3]
p.ah <- pt((abs(b)-delta)/(sqrt(err^2)),df) - pt((-abs(b)-delta)/(sqrt(err^2) ), df)
cat("unstandardized regression coefficient for", predictor,":", "\n",
"b = ",b," , 95% CI [",l.ci,",",u.ci,"]" ,"\n",
"std. error = ", err, "\n", "\n",
"standarized regression coefficient for", predictor,":","\n",
"β = ", beta,", 95% CI [",l.std.ci,",",u.std.ci,"]", "\n", "\n",
"raw equivalence interval =","[",l.delta,",",u.delta,"]", "\n",
"std. equivalence interval =","[",l.std.delta,",",u.std.delta,"]", "\n",
"\n",
"Anderson-Hauck (AH) Equivalence Test Results:", "\n",
"-------------------------------------------------", "\n",
"t(",df,") = ",t.ah,"\n",
"p = ",p.ah,"\n",
ifelse(p.ah < alpha, "AH: Negligible effects concluded" , 'AH: Insufficient evidence for negligible effects'))
} else {
# TOST
t1.tost<- (b-(-delta))/(sqrt(err^2))
t2.tost<- (delta-b)/(sqrt(err^2))
p1.tost<- 1-pt(t1.tost, df)
p2.tost<- 1-pt(t2.tost, df)
cat("unstandardized regression coefficient for", predictor,":", "\n",
"b = ",b," , 95% CI [",l.ci,",",u.ci,"]" ,"\n",
"std. error = ", err, "\n", "\n",
"standarized regression coefficient for", predictor,":","\n",
"β = ", beta,", 95% CI [",l.std.ci,",",u.std.ci,"]", "\n", "\n",
"raw equivalence interval =","[",l.delta,",",u.delta,"]", "\n",
"std. equivalence interval =","[",l.std.delta,",",u.std.delta,"]", "\n",
"\n",
"Two One-Sided Test (TOST) Equivalence Test Results:", "\n",
"-------------------------------------------------", "\n",
"t1(",df,") = ",t1.tost,"\n",
"t2(",df,") = ", t2.tost, "\n",
"p1 = ",p1.tost,"\n",
"p2 = ",p2.tost,"\n",
ifelse(p1.tost < alpha & p2.tost < alpha, 'TOST: Negligible effects concluded', 'TOST: Insufficient evidence for negligible effects'))
}
# plot
if (plot==TRUE) {
plot(NA, axes=F,
xlim = c(min(l.ci,l.delta)-max(u.ci-l.ci, u.delta-l.delta)/10, max(u.ci,u.delta)+max(u.ci-l.ci, u.delta-l.delta)/10),
ylim = c(0,1),
yaxt='n',
ylab="",
xlab = "Regression Coefficient Estimate",
main = "Symmetric CI Approach \n 90% CI")
abline(v =0 , lty = 2, col= "light grey") # vertical line in the middle of the eq. interval, right now it's always 0, can be changed
points(x=b, y=.5, pch=8, cex=2) # point at the estimated predictor value
abline(v=u.delta, lty=2, col = "red") # mark the upper eq. bound
abline(v=l.delta, lty=2, col = "red") # mark the lower eq. bound
segments(l.ci90,0.5,u.ci90,0.5, lwd=3) # plotting the 90% CI for the predictor estimate
text(u.delta*1.01,.5,"upper equivalence bound",srt=270,pos=3, offset = .5, col = "red") # text for eq. bound line (upper)
text(l.delta*1.01,.5,"lower equivalence bound",srt=90,pos=3, offset = .5, col = "red") # text for eq. bound line (lower)
text(u.delta*.9,-0.01,u.delta,srt=0,pos=3, offset = .5, col = "red") # writing the eq. interval bound value (upper)
text(l.delta*0.88,-.01,l.delta,srt=0,pos=3, offset = .5, col = "red") # writing the eq. interval bound value (lower)
text(b,.57,round(b, digits = 3),srt=0) # writing the predictor point estimate value
text(b,.57,"b=",srt=0, pos = 2, offset = 1.5) # adding text to indicate above line
text(u.ci90,.45,round(u.ci90, digits = 3),pos =1, offset = .1, col = "black") # writing the 90% CI upper limit value for the predictor estimate
text(l.ci90,.45,round(l.ci90, digits = 3), pos =1, offset = .1,col = "black") # writing the 90% CI lower limit value for the predictor estimate
axis(side=1, pos=0, lwd.ticks=0)
}
}
} # end of function
```
To use the function, fill in the missing input:
```{r using reg.equiv.fd}
reg.equiv.fd(datfra= , model= , predictor= , delta= , alpha=.05, plot=TRUE, test="TOST", std=TRUE)
```
# The *reg.equiv* Function
### (full data *not* necessary)
```{r reg.equiv function}
reg.equiv <- function(b, se, p, n, delta, alpha=.05, plot=TRUE, test="TOST", std=TRUE) {
# b = regression coefficient, std or raw
# se = std. error associated with the point estimate, b
# p = no. of predictors in the model
# n = sample size
# delta = SESOI, std or raw
df <- n-1-p
l.ci90 <- b+qt(.1/2,df, lower.tail = T)*se
u.ci90 <- b+qt(.1/2,df, lower.tail = F)*se
l.ci <- b+qt(alpha/2,df, lower.tail = T)*se
u.ci <- b+qt(alpha/2,df, lower.tail = F)*se
#delta
l.delta <- -abs(delta) # lower equivalence interval bound (consider to make it as input in the function)
u.delta <- abs(delta) # upper equivalence interval bound
# RESULTS SECTION
# standardized section
if (std==TRUE) {
if (test=="AH") {
# AH
t.ah <- b/se
p.ah <- pt((abs(b)-delta)/(sqrt(se^2)),df) - pt((-abs(b)-delta)/(sqrt(se^2) ), df)
cat("standardized regression coefficient is ","\n",
"β = ",b," ,", (1-alpha)*100, "% CI [",l.ci,",",u.ci,"]" ,"\n",
"associated std. error = ", se, "\n", "\n",
"std. equivalence interval =","[",l.delta,",",u.delta,"]", "\n",
"\n",
"Anderson-Hauck (AH) Equivalence Test Results:", "\n",
"-------------------------------------------------", "\n",
"t(",df,") = ",t.ah,"\n",
"p = ",p.ah,"\n",
ifelse(p.ah < alpha, "AH: Negligible effects concluded" , 'AH: Insufficient evidence for negligible effects'))
} else {
# TOST
t1.tost<- (b-(-delta))/(sqrt(se^2))
t2.tost<- (delta-b)/(sqrt(se^2))
p1.tost<- 1-pt(t1.tost, df)
p2.tost<- 1-pt(t2.tost, df)
cat("standardized regression coefficient is ","\n",
"β = ",b," ,", (1-alpha)*100, "% CI [",l.ci,",",u.ci,"]" ,"\n",
"associated std. error = ", se, "\n", "\n",
"std. equivalence interval =","[",l.delta,",",u.delta,"]", "\n",
"\n",
"Two One-Sided Test (TOST) Equivalence Test Results:", "\n",
"-------------------------------------------------", "\n",
"t1(",df,") = ",t1.tost,"\n",
"t2(",df,") = ", t2.tost, "\n",
"p1 = ",p1.tost,"\n",
"p2 = ",p2.tost,"\n",
ifelse(p1.tost < alpha & p2.tost < alpha, 'TOST: Negligible effects concluded', 'TOST: Insufficient evidence for negligible effects'))
}
# plot
if (plot==TRUE) {
plot(NA, axes=F,
xlim = c(min(l.ci,l.delta)-max(u.ci-l.ci, u.delta-l.delta)/10, max(u.ci,u.delta)+max(u.ci-l.ci, u.delta-l.delta)/10),
ylim = c(0,1),
yaxt='n',
ylab="",
xlab = "Std. Regression Coefficient Estimate",
main = "Symmetric CI Approach \n 90% CI")
abline(v =0 , lty = 2, col= "light grey") # vertical line in the middle of the eq. interval, right now it's always 0, can be changed
points(x=b, y=.5, pch=8, cex=2) # point at the estimated predictor value
abline(v=u.delta, lty=2, col = "red") # mark the upper eq. bound
abline(v=l.delta, lty=2, col = "red") # mark the lower eq. bound
segments(l.ci90,0.5,u.ci90,0.5, lwd=3) # plotting the 90% CI for the predictor estimate
text(u.delta*1.01,.5,"upper equivalence bound",srt=270,pos=3, offset = .5, col = "red") # text for eq. bound line (upper)
text(l.delta*1.01,.5,"lower equivalence bound",srt=90,pos=3, offset = .5, col = "red") # text for eq. bound line (lower)
text(u.delta*.9,-0.01,u.delta,srt=0,pos=3, offset = .5, col = "red") # writing the eq. interval bound value (upper)
text(l.delta*0.88,-.01,l.delta,srt=0,pos=3, offset = .5, col = "red") # writing the eq. interval bound value (lower)
text(b,.57,round(b, digits = 3),srt=0) # writing the predictor point estimate value
text(b,.57,"β=",srt=0, pos = 2, offset = 1.5) # adding text to indicate above line
text(u.ci90,.45,round(u.ci90, digits = 3),pos =1, offset = .1, col = "black") # writing the 90% CI upper limit value for the predictor estimate
text(l.ci90,.45,round(l.ci90, digits = 3), pos =1, offset = .1,col = "black") # writing the 90% CI lower limit value for the predictor estimate
axis(side=1, pos=0, lwd.ticks=0)
}
} else {
# unstandardized section
if (test=="AH") {
# AH
t.ah <- b/se
p.ah <- pt((abs(b)-delta)/(sqrt(se^2)),df) - pt((-abs(b)-delta)/(sqrt(se^2) ), df)
cat("unstandardized regression coefficient is ", "\n",
"b = ",b," ,", (1-alpha)*100, "% CI [",l.ci,",",u.ci,"]" ,"\n",
"std. error = ", se, "\n", "\n",
"raw equivalence interval =","[",l.delta,",",u.delta,"]", "\n",
"\n",
"Anderson-Hauck (AH) Equivalence Test Results:", "\n",
"-------------------------------------------------", "\n",
"t(",df,") = ",t.ah,"\n",
"p = ",p.ah,"\n",
ifelse(p.ah < alpha, "AH: Negligible effects concluded" , 'AH: Insufficient evidence for negligible effects'))
} else {
# TOST
t1.tost<- (b-(-delta))/(sqrt(se^2))
t2.tost<- (delta-b)/(sqrt(se^2))
p1.tost<- 1-pt(t1.tost, df)
p2.tost<- 1-pt(t2.tost, df)
cat("unstandardized regression coefficient is ","\n",
"b = ",b," ,", (1-alpha)*100, "% CI [",l.ci,",",u.ci,"]" ,"\n",
"std. error = ", se, "\n", "\n",
"raw equivalence interval =","[",l.delta,",",u.delta,"]", "\n",
"\n",
"Two One-Sided Test (TOST) Equivalence Test Results:", "\n",
"-------------------------------------------------", "\n",
"t1(",df,") = ",t1.tost,"\n",
"t2(",df,") = ", t2.tost, "\n",
"p1 = ",p1.tost,"\n",
"p2 = ",p2.tost,"\n",
ifelse(p1.tost < alpha & p2.tost < alpha, 'TOST: Negligible effects concluded', 'TOST: Insufficient evidence for negligible effects'))
}
# plot
if (plot==TRUE) {
plot(NA, axes=F,
xlim = c(min(l.ci,l.delta)- max(u.ci-l.ci, u.delta-l.delta)/10, max(u.ci,u.delta)+max(u.ci-l.ci, u.delta-l.delta)/10),
ylim = c(0,1),
yaxt='n',
ylab="",
xlab = "Regression Coefficient Estimate",
main = "Symmetric CI Approach \n 90% CI")
abline(v =0 , lty = 2, col= "light grey") # vertical line in the middle of the eq. interval, right now it's always 0, can be changed
points(x=b, y=.5, pch=8, cex=2) # point at the estimated predictor value
abline(v=u.delta, lty=2, col = "red") # mark the upper eq. bound
abline(v=l.delta, lty=2, col = "red") # mark the lower eq. bound
segments(l.ci90,0.5,u.ci90,0.5, lwd=3) # plotting the 90% CI for the predictor estimate
text(u.delta*1.01,.5,"upper equivalence bound",srt=270,pos=3, offset = .5, col = "red") # text for eq. bound line (upper)
text(l.delta*1.01,.5,"lower equivalence bound",srt=90,pos=3, offset = .5, col = "red") # text for eq. bound line (lower)
text(u.delta*.9,-0.01,u.delta,srt=0,pos=3, offset = .5, col = "red") # writing the eq. interval bound value (upper)
text(l.delta*0.88,-.01,l.delta,srt=0,pos=3, offset = .5, col = "red") # writing the eq. interval bound value (lower)
text(b,.57,round(b, digits = 3),srt=0) # writing the predictor point estimate value
text(b,.57,"b=",srt=0, pos = 2, offset = 1.5) # adding text to indicate above line
text(u.ci90,.45,round(u.ci90, digits = 3),pos =1, offset = .1, col = "black") # writing the 90% CI upper limit value for the predictor estimate
text(l.ci90,.45,round(l.ci90, digits = 3), pos =1, offset = .1,col = "black") # writing the 90% CI lower limit value for the predictor estimate
axis(side=1, pos=0, lwd.ticks=0)
}
}
} # end of function
```
To use the function, fill in the missing input:
```{r using reg.equiv}
reg.equiv(b= , se= , p= , n= , delta= , alpha=.05, plot=TRUE, test="TOST", std=TRUE)
```