This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
forked from MathiasHarrer/Doing-Meta-Analysis-in-R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmreg.multimodel.inference.R
88 lines (71 loc) · 2.95 KB
/
mreg.multimodel.inference.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
mreg.multimodel.inference = function(TE,
seTE,
data,
predictors,
method="REML",
test="knha",
eval.criterion="aicc",
level=1){
# Parts of the computatations for this function are based on:
# http://www.metafor-project.org/doku.php/tips:model_selection_with_glmulti
library(metafor)
library(glmulti)
# Change supplied df to conform to glmulti
TE = data[TE]
seTE = data[seTE]
preds = data[predictors]
glm.data = data.frame("TE"=TE, "seTE"=seTE)
colnames(glm.data) = c("TE", "seTE")
glm.data = cbind(glm.data, preds)
# Build the formula
predictor.string = paste(predictors, collapse="+")
form = paste("TE ~", predictor.string, collapse = "")
# Set up function
rma.glmulti = function(formula, data, ...)
rma(formula, seTE, data=data, method=method, test=test)
# Loop over all possible models
result = glmulti(y="TE", xr=predictors, data=glm.data,
level=level,
fitfunction=rma.glmulti,
crit=eval.criterion,
confsetsize=1000)
# Save results for all models: all.models, top5.models
all.models = weightable(result)
top5.models = weightable(result)[1:5,]
# Create Multimodel Inference Coeffient Table and save: multimodel.coef
setOldClass("rma.uni")
setMethod('getfit', 'rma.uni', function(object, ...) {
if (object$test==test) {
cbind(estimate=coef(object), se=sqrt(diag(vcov(object))), df=Inf)
} else {
cbind(estimate=coef(object), se=sqrt(diag(vcov(object))), df=object$k-object$p)
}
})
multimodel.coef = coef(result)
# Create List with model results for all models: model.details
model.details = list()
for (x in 1:length(result@objects)){
model.details[[x]] = result@objects[x]
}
# Print out results
cat("\n", "Multimodel Inference: Final Results", "--------------------------", sep="\n")
cat("\n", "- Number of fitted models:", nrow(all.models))
cat("\n", "- Full formula:", form)
cat("\n", "- Coefficient significance test:", test)
cat("\n", "- Modeled interaction level:", level)
cat("\n", "- Evaluation criterion:", eval.criterion, "\n")
cat("\n", "Best 5 Models", "--------------------------", "\n", sep="\n")
print(top5.models)
cat("\n", "Multimodel Inference Coefficients", "--------------------------", "\n", sep="\n")
print(multimodel.coef)
# Print graph
plot(result, type="s")
# Return results
invisible(list("all.models"=all.models,
"top5.models"=top5.models,
"multimodel.coef"=multimodel.coef,
"model.details"=model.details,
"formula"=form,
"fitted.models"=nrow(all.models),
"eval.criterion"=eval.criterion))
}