-
Notifications
You must be signed in to change notification settings - Fork 0
/
regressionINTEXT.R
122 lines (95 loc) · 4.08 KB
/
regressionINTEXT.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
##########
cat("\n####################")
cat("\nLoading Nadya's in-text support for regressions from Github.")
cat("\n Version : 0.0.1.9003 (for R version 3.6.3)")
cat("\nLast update : 30 May 2021, 6:14am")
cat("\n")
library(dplyr)
##########
intext_regression = function(
regression.output, varname = NULL, round = 2, round_ci = round,
add_beta = TRUE, add_ci = TRUE, add_intercept = FALSE) {
##### sub-functions #####
intext_regCoeffs = function(b, se) {return(paste0("b = ", b, ", SE = ", se, sep = ""))}
intext_CI = function(cilower, ciupper) {return(ci = paste0("95% CI = [", cilower, ", ", ciupper, "]", sep = ""))}
intext_regression_single = function(
regression.output, varname = NULL, round = 2, round_ci = round,
add_beta = TRUE, add_ci = TRUE, add_intercept = FALSE) {
res = regression.output
if(!("stdcoeff" %in% colnames(res))) {
# there is no beta column
add_beta = FALSE
res = res %>% dplyr::mutate(betaholder = NA) %>% dplyr::select(variable, betaholder, everything())
}
# if varname is not set, assume first non-intercept term (usually second row) is desired
if(is.null(varname)) {rownum = 2}
# if varname is set, find it
else {
# if varname is not given as a character, stop function and tell user
if(!is.character(varname)) {stop("varname not recognised. Pass in the name of a term in character class.")}
else {
# find row number of the variable
rownum = match(varname, res$variable)
# if row number cannot be found, stop function and tell user
if(is.na(rownum)) {stop("varname not recognised. Pass in the name of a term in the equation.")}
}
}
# retrieve varname
varname = res[rownum, 1]
# function to force rounding later
forceround = function(thing_to_round, dp) {
number = round(as.numeric(thing_to_round), as.numeric(dp))
number = format(number, nsmall = dp)
return(number)
}
# check for std coeff
if(add_beta) {
beta = res[rownum, "stdcoeff"] %>% forceround(dp = round) %>% trimws()
beta_info = paste0("β = ", beta, ", ", sep = "")
}
else {beta_info = ""}
# get b and se
b = res[rownum, "coeff"] %>% forceround(dp = round) %>% trimws()
se = res[rownum, "se"] %>% forceround(dp = round) %>% trimws()
bs = intext_regCoeffs(b, se)
# get ci
if(add_ci) {
if(ncol(res %>% dplyr::select(contains("CI95"))) != 2) {
cat("\nNo 95% CIs found.\n")
ci95 = ""
}
else {
ci95 = res[rownum, c("CI95lower", "CI95upper")] %>% forceround(dp = round_ci) %>% trimws()
while(as.numeric(ci95[1]) == 0 | as.numeric(ci95[2]) == 0) {
round_ci = round_ci + 1
ci95 = res[rownum, c("CI95lower", "CI95upper")] %>% forceround(dp = round_ci) %>% trimws()
}
ci95 = paste0(intext_CI(ci95[1], ci95[2]), ", ", sep = "")
}
}
else {ci95 = ""}
# get pval
pval = res[rownum, "p"]; if(length(pval) == 0) pval = res[rownum, "pval"]
psegment = intext_p(pval)
# get intercept (used for simple slopes context)
if(add_intercept) {
intercept_row = match("(Intercept)", res$variable)
intercept_value = res[intercept_row, "coeff"] %>% forceround(dp = round) %>% trimws()
intercept_info = paste0(", intercept = ", intercept_value, sep = "")
}
else {intercept_info = ""}
# paste everything together and return
return(paste0(varname, " ", beta_info, bs, ", ", ci95, psegment, intercept_info, sep = ""))
}
##### start of main function #####
if(is.null(varname) | length(varname) == 1) {return(intext_regression_single(regression.output, varname = varname, round = round, add_beta = add_beta, add_ci = add_ci, add_intercept = add_intercept))}
else {
collated = list()
for(v in varname) {collated[[v]] = intext_regression_single(regression.output, varname = v, round = round, add_beta = add_beta, add_ci = add_ci, add_intercept = add_intercept)}
return(collated)
}
}
##########
cat("Success.")
cat("\n####################")
##########