Skip to content

Commit 5e8cede

Browse files
2 parents d84007c + aac2552 commit 5e8cede

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+7754
-3917
lines changed

R/add_ci_bounds_mc_power.R

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
#'@keywords internal
66
add_ci_bounds_mc_power = function(power_results, nsim, conf = 0.95) {
77
get_lcbs = function(power) {
8-
return(unlist(lapply(power, \(x) (binom.test(x*nsim, n = nsim, conf.level = conf)[["conf.int"]][1]))))
8+
return(unlist(lapply(
9+
power,
10+
\(x) (binom.test(x * nsim, n = nsim, conf.level = conf)[["conf.int"]][1])
11+
)))
912
}
1013
get_ucbs = function(power) {
11-
return(unlist(lapply(power, \(x) (binom.test(x*nsim, n = nsim, conf.level = conf)[["conf.int"]][2]))))
14+
return(unlist(lapply(
15+
power,
16+
\(x) (binom.test(x * nsim, n = nsim, conf.level = conf)[["conf.int"]][2])
17+
)))
1218
}
1319
lcb = get_lcbs(power_results[["power"]])
1420
ucb = get_ucbs(power_results[["power"]])

R/anticoef_from_delta.R

+21-22
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,45 @@ anticoef_from_delta = function(default_coef, delta, glmfamily) {
2020
if (glmfamily == "gaussian") {
2121
if (length(delta) == 1) {
2222
anticoef = default_coef * delta / 2
23-
}
24-
else {
23+
} else {
2524
anticoef = default_coef * (delta[2] - delta[1]) / 2
2625
}
27-
}
28-
else if (glmfamily == "exponential") {
26+
} else if (glmfamily == "exponential") {
2927
if (length(delta) == 1) {
3028
anticoef = default_coef * delta / 2
31-
warning("default or length 1 delta used with glmfamily == 'exponential'. This can lead to unrealistic effect sizes - make sure the generated anticipated coeffcients are appropriate.")
32-
}
33-
else {
29+
warning(
30+
"default or length 1 delta used with glmfamily == 'exponential'. This can lead to unrealistic effect sizes - make sure the generated anticipated coeffcients are appropriate."
31+
)
32+
} else {
3433
anticoef = gen_exponential_anticoef(default_coef, delta[1], delta[2])
3534
}
36-
}
37-
else if (glmfamily == "poisson") {
35+
} else if (glmfamily == "poisson") {
3836
if (length(delta) == 1) {
3937
anticoef = default_coef * delta / 2
40-
warning("default or length 1 delta used with glmfamily == 'poisson'. This can lead to unrealistic effect sizes - make sure the generated anticipated coeffcients are appropriate.")
41-
}
42-
else {
38+
warning(
39+
"default or length 1 delta used with glmfamily == 'poisson'. This can lead to unrealistic effect sizes - make sure the generated anticipated coeffcients are appropriate."
40+
)
41+
} else {
4342
anticoef = gen_exponential_anticoef(default_coef, delta[1], delta[2])
4443
}
45-
}
46-
else if (glmfamily == "binomial") {
44+
} else if (glmfamily == "binomial") {
4745
if (length(delta) == 1) {
4846
anticoef = default_coef * delta / 2
49-
warning("default or length 1 delta used with glmfamily == 'binomial'. This can lead to unrealistic effect sizes - make sure the generated anticipated coeffcients are appropriate.")
50-
}
51-
else {
47+
warning(
48+
"default or length 1 delta used with glmfamily == 'binomial'. This can lead to unrealistic effect sizes - make sure the generated anticipated coeffcients are appropriate."
49+
)
50+
} else {
5251
anticoef = gen_binomial_anticoef(default_coef, delta[1], delta[2])
5352
}
54-
}
55-
else {
53+
} else {
5654
if (length(delta) == 1) {
5755
anticoef = default_coef * delta / 2
58-
}
59-
else {
56+
} else {
6057
anticoef = gen_exponential_anticoef(default_coef, delta[1], delta[2])
6158
}
62-
warning("delta parameter used with unsupported glmfamily. Make sure the generated anticipated coefficients are appropriate.")
59+
warning(
60+
"delta parameter used with unsupported glmfamily. Make sure the generated anticipated coefficients are appropriate."
61+
)
6362
}
6463
anticoef
6564
}

R/anticoef_from_delta_surv.R

+16-17
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,36 @@ anticoef_from_delta_surv = function(default_coef, delta, distribution) {
2020
if (distribution == "gaussian") {
2121
if (length(delta) == 1) {
2222
anticoef = default_coef * delta / 2
23-
}
24-
else {
23+
} else {
2524
anticoef = default_coef * (delta[2] - delta[1]) / 2
2625
}
27-
}
28-
else if (distribution == "exponential") {
26+
} else if (distribution == "exponential") {
2927
if (length(delta) == 1) {
3028
anticoef = default_coef * delta / 2
31-
warning("default or length 1 delta used with distribution == 'exponential'. This can lead to unrealistic effect sizes - make sure the generated anticipated coeffcients are appropriate.")
32-
}
33-
else {
29+
warning(
30+
"default or length 1 delta used with distribution == 'exponential'. This can lead to unrealistic effect sizes - make sure the generated anticipated coeffcients are appropriate."
31+
)
32+
} else {
3433
anticoef = gen_exponential_anticoef(default_coef, delta[1], delta[2])
3534
}
36-
}
37-
else if (distribution == "lognormal") {
35+
} else if (distribution == "lognormal") {
3836
if (length(delta) == 1) {
3937
anticoef = default_coef * delta / 2
40-
warning("default or length 1 delta used with distribution == 'lognormal'. This can lead to unrealistic effect sizes - make sure the generated anticipated coeffcients are appropriate.")
41-
}
42-
else {
38+
warning(
39+
"default or length 1 delta used with distribution == 'lognormal'. This can lead to unrealistic effect sizes - make sure the generated anticipated coeffcients are appropriate."
40+
)
41+
} else {
4342
anticoef = gen_exponential_anticoef(default_coef, delta[1], delta[2])
4443
}
45-
}
46-
else {
44+
} else {
4745
if (length(delta) == 1) {
4846
anticoef = default_coef * delta / 2
49-
}
50-
else {
47+
} else {
5148
anticoef = gen_exponential_anticoef(default_coef, delta[1], delta[2])
5249
}
53-
warning("delta parameter used with unsupported distribution. Make sure the generated anticipated coefficients are appropriate.")
50+
warning(
51+
"delta parameter used with unsupported distribution. Make sure the generated anticipated coefficients are appropriate."
52+
)
5453
}
5554
anticoef
5655
}

R/calc_conservative_anticoef.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ calc_conservative_anticoef = function(results, effectsize) {
2727
if (length(which(abs(powers - min(powers)) < 1E-10)) > 1) {
2828
numberofequal = length(which(abs(powers - min(powers)) < 1E-10))
2929
exponents = 1:numberofequal + 1
30-
values = rep(-1, numberofequal) ^ exponents
30+
values = rep(-1, numberofequal)^exponents
3131
if (numberofequal > 2) {
3232
values[3:length(values)] = 0
3333
}

R/calc_interaction_degrees.R

+71-28
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@
99
#'@param split_degrees The number of degrees of freedom for each main effect term
1010
#'@return Degrees of freedom vector
1111
#'@keywords internal
12-
calc_interaction_degrees = function(design, model, contrast, nointercept,
13-
split_layers, split_degrees, split_plot_structure) {
12+
calc_interaction_degrees = function(
13+
design,
14+
model,
15+
contrast,
16+
nointercept,
17+
split_layers,
18+
split_degrees,
19+
split_plot_structure
20+
) {
1421
contrastslistspd = list()
15-
for (x in names(design[lapply(design, class) %in% c("factor", "character")])) {
22+
for (x in names(design[
23+
lapply(design, class) %in% c("factor", "character")
24+
])) {
1625
contrastslistspd[[x]] = contrast
1726
}
1827

@@ -22,82 +31,116 @@ calc_interaction_degrees = function(design, model, contrast, nointercept,
2231
contrastslistspd = NULL
2332
}
2433

25-
model = as.formula(paste0("~", paste(attr(terms.formula(model), "term.labels"), collapse = " + ")))
26-
if(!nointercept) {
34+
model = as.formula(paste0(
35+
"~",
36+
paste(attr(terms.formula(model), "term.labels"), collapse = " + ")
37+
))
38+
if (!nointercept) {
2739
character_model = as.character(model)[-1]
2840
} else {
29-
character_model = paste(c(as.character(model)[-1]," + -1"), collapse="")
41+
character_model = paste(c(as.character(model)[-1], " + -1"), collapse = "")
3042
}
3143
splitterms = unlist(strsplit(character_model, split = " + ", fixed = TRUE))
3244
ismaineffect = rep(FALSE, length(splitterms))
3345
ismaineffect[1:length(split_layers)] = TRUE
3446
names(ismaineffect) = splitterms
3547
interactions = list()
36-
if(max(split_layers) > 0) {
37-
for(i in 1:max(split_layers, na.rm = TRUE)) {
48+
if (max(split_layers) > 0) {
49+
for (i in 1:max(split_layers, na.rm = TRUE)) {
3850
wholeplotterms = colnames(design)[split_layers == i]
3951
wholeorwholeinteraction = rep(FALSE, length(splitterms))
4052
for (term in wholeplotterms) {
41-
regex = paste0("(\\b", term, "\\b)|(\\b", term, ":)|(:", term, "\\b)|(\\b", term, "\\s\\*)|(\\*\\s", term, "\\b)")
42-
wholeorwholeinteraction = wholeorwholeinteraction | grepl(regex, splitterms, perl = TRUE)
53+
regex = paste0(
54+
"(\\b",
55+
term,
56+
"\\b)|(\\b",
57+
term,
58+
":)|(:",
59+
term,
60+
"\\b)|(\\b",
61+
term,
62+
"\\s\\*)|(\\*\\s",
63+
term,
64+
"\\b)"
65+
)
66+
wholeorwholeinteraction = wholeorwholeinteraction |
67+
grepl(regex, splitterms, perl = TRUE)
4368
}
4469
interactions[[i]] = wholeorwholeinteraction
4570
}
4671
} else {
47-
for(i in seq_along(1:max(split_layers, na.rm = TRUE))) {
72+
for (i in seq_along(1:max(split_layers, na.rm = TRUE))) {
4873
interactions[[i]] = FALSE
4974
}
5075
}
51-
if(!nointercept) {
52-
degrees_of_freedom = rep(min(split_degrees), length(splitterms)+1)
53-
for(i in 1:length(split_layers)) {
54-
degrees_of_freedom[i+1] = split_degrees[split_layers[i]+1]
76+
if (!nointercept) {
77+
degrees_of_freedom = rep(min(split_degrees), length(splitterms) + 1)
78+
for (i in 1:length(split_layers)) {
79+
degrees_of_freedom[i + 1] = split_degrees[split_layers[i] + 1]
5580
}
5681
names(degrees_of_freedom) = c("(Intercept)", splitterms)
5782
} else {
5883
degrees_of_freedom = rep(min(split_degrees), length(splitterms))
59-
for(i in 1:length(split_layers)) {
84+
for (i in 1:length(split_layers)) {
6085
degrees_of_freedom[i] = split_degrees[split_layers[i]]
6186
}
6287
names(degrees_of_freedom) = c(splitterms)
6388
}
6489
#get higher order terms
65-
for(term in colnames(design)) {
66-
arith_terms = splitterms[grepl(paste0("(I\\(", term, "\\^.+\\))"), splitterms, perl = TRUE)]
67-
for(arith_term in arith_terms) {
90+
for (term in colnames(design)) {
91+
arith_terms = splitterms[grepl(
92+
paste0("(I\\(", term, "\\^.+\\))"),
93+
splitterms,
94+
perl = TRUE
95+
)]
96+
for (arith_term in arith_terms) {
6897
degrees_of_freedom[arith_term] = degrees_of_freedom[term]
6998
}
7099
}
71100
#Effect terms
72-
for(i in seq_along(1:max(split_layers, na.rm = TRUE))) {
101+
for (i in seq_along(1:max(split_layers, na.rm = TRUE))) {
73102
subplotterms = colnames(design)
74-
subplotterms = subplotterms[!(subplotterms %in% colnames(design)[split_layers >= i])]
103+
subplotterms = subplotterms[
104+
!(subplotterms %in% colnames(design)[split_layers >= i])
105+
]
75106
wholeplotterms = colnames(design)[split_layers < i]
76107

77108
regularmodel = rep(FALSE, length(splitterms))
78109
for (term in subplotterms) {
79-
regex = paste0("(\\b", term, "\\b)|(\\b", term, ":)|(:", term, "\\b)|(\\b", term, "\\s\\*)|(\\*\\s", term, "\\b)")
110+
regex = paste0(
111+
"(\\b",
112+
term,
113+
"\\b)|(\\b",
114+
term,
115+
":)|(:",
116+
term,
117+
"\\b)|(\\b",
118+
term,
119+
"\\s\\*)|(\\*\\s",
120+
term,
121+
"\\b)"
122+
)
80123
regularmodel = regularmodel | grepl(regex, splitterms, perl = TRUE)
81124
}
82125
#get whole:whole interaction terms
83126
wholewholeinteractionterms = splitterms[!ismaineffect & interactions[[i]]]
84-
for(term in wholewholeinteractionterms) {
127+
for (term in wholewholeinteractionterms) {
85128
subterms = unlist(strsplit(term, split = ":", fixed = TRUE))
86129
maxdegree = c()
87-
for(subterm in subterms) {
88-
if(!subterm %in% wholeplotterms) {
130+
for (subterm in subterms) {
131+
if (!subterm %in% wholeplotterms) {
89132
maxdegree = max(c(maxdegree, degrees_of_freedom[subterm]))
90133
}
91134
}
92135
degrees_of_freedom[term] = maxdegree
93136
}
94137
#get whole:non-whole interaction terms
95138
wholeinteractionterms = splitterms[regularmodel & interactions[[i]]]
96-
for(term in wholeinteractionterms) {
139+
for (term in wholeinteractionterms) {
97140
subterms = unlist(strsplit(term, split = ":", fixed = TRUE))
98141
maxdegree = c()
99-
for(subterm in subterms) {
100-
if(!subterm %in% wholeplotterms) {
142+
for (subterm in subterms) {
143+
if (!subterm %in% wholeplotterms) {
101144
maxdegree = max(c(maxdegree, degrees_of_freedom[subterm]))
102145
}
103146
}

R/calcnoncentralparam.R

+12-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@
88
#'@param vinv variance-covariance matrix
99
#'@return The non-centrality parameter for the F test
1010
#'@keywords internal
11-
calcnoncentralparam = function(X, L, b, vinv=NULL) {
11+
calcnoncentralparam = function(X, L, b, vinv = NULL) {
1212
if (is.null(vinv)) {
1313
vinv = diag(nrow(X))
1414
}
15-
matrix_solve = tryCatch({solve(t(X) %*% vinv %*% X)}, error = function(e) e)
16-
if(inherits(matrix_solve,"error")) {
17-
stop(sprintf("skpr: Can't calculate non-centrality parameter with given design matrix (X) and variance-covariance (Vinv) matrix due to `t(X) %*% Vinv %*% X` being singular. `solve()` error message: '%s'",
18-
as.character(matrix_solve)))
15+
matrix_solve = tryCatch(
16+
{
17+
solve(t(X) %*% vinv %*% X)
18+
},
19+
error = function(e) e
20+
)
21+
if (inherits(matrix_solve, "error")) {
22+
stop(sprintf(
23+
"skpr: Can't calculate non-centrality parameter with given design matrix (X) and variance-covariance (Vinv) matrix due to `t(X) %*% Vinv %*% X` being singular. `solve()` error message: '%s'",
24+
as.character(matrix_solve)
25+
))
1926
}
2027
return(t(L %*% b) %*% solve(L %*% matrix_solve %*% t(L)) %*% L %*% b)
2128
}

R/calculate_block_nesting.R

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,26 @@
99
calculate_block_nesting = function(blockgroups, blockstructure) {
1010
#`nested_level` is a label for which is the highest blocking level (excluding the lowest level)
1111
# the column is nested within
12-
nested_level = rep(1,length(blockgroups))
12+
nested_level = rep(1, length(blockgroups))
1313
for (i in seq_len(length(blockgroups))) {
14-
isblockcol = rep(TRUE,ncol(blockstructure))
14+
isblockcol = rep(TRUE, ncol(blockstructure))
1515
temp_block_str = blockgroups[[i]]
1616
temp_block_str_names = names(temp_block_str)
1717
for (j in seq_len(length(temp_block_str))) {
1818
block_name = temp_block_str_names[j]
1919
if (temp_block_str[j] - 1 > 0) {
20-
block_idx = which(blockstructure[,i] == block_name)
21-
is_constant = lapply(apply(blockstructure[block_idx, ], 2, unique), length) == 1
20+
block_idx = which(blockstructure[, i] == block_name)
21+
is_constant = lapply(
22+
apply(blockstructure[block_idx, ], 2, unique),
23+
length
24+
) ==
25+
1
2226
isblockcol = isblockcol & is_constant
2327
}
2428
}
2529
isblockcol[i] = FALSE
2630
block_nested_within = which(isblockcol)
27-
if(length(block_nested_within) != 0) {
31+
if (length(block_nested_within) != 0) {
2832
nested_level[i] = max(block_nested_within) + 1
2933
}
3034
}

0 commit comments

Comments
 (0)