Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cop_pse_auxiliary.R(F - FALSE) #78

Merged
merged 42 commits into from
Feb 19, 2024
Merged
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
2192fc1
Update cop_pse_auxiliary.R
wwang93 Feb 17, 2024
a51a414
Update cop_pse_auxiliary.R
wwang93 Feb 17, 2024
32ba34e
Update concord1.R
wwang93 Feb 17, 2024
ea2c3c5
Update cop_pse_auxiliary.R
wwang93 Feb 17, 2024
c44f509
Update helper_output_print.R
wwang93 Feb 17, 2024
b308662
Update helper_output_dataframe.R
wwang93 Feb 17, 2024
5475749
Update helper_plot_correlation.R
wwang93 Feb 17, 2024
a29c19e
Update helper_plot_threshold.R
wwang93 Feb 18, 2024
0024bd0
Update konfound-glm-dichotomous.R
wwang93 Feb 18, 2024
ded8119
Update konfound-glm.R
wwang93 Feb 18, 2024
953fbc7
Update konfound-lm.R
wwang93 Feb 18, 2024
f4c9426
Update konfound-lmer.R
wwang93 Feb 18, 2024
022adc7
Update konfound.R
wwang93 Feb 18, 2024
ea06db5
Update mkonfound-data.R
wwang93 Feb 18, 2024
3ae5f08
Update mkonfound.R
wwang93 Feb 18, 2024
90cbc7d
Update core-sensitivity-mkonfound.R
wwang93 Feb 18, 2024
d9a587f
Update pkonfound.R
wwang93 Feb 18, 2024
0a127cb
Update test_cop.R
wwang93 Feb 18, 2024
8ae3fef
Update test_cop.R
wwang93 Feb 18, 2024
99ee392
Update test_pse.R
wwang93 Feb 18, 2024
619827c
Update test_sensitivity.R
wwang93 Feb 18, 2024
c1a27e2
Update test_sensitivity_ln.R
wwang93 Feb 18, 2024
780921b
Update tkonfound.R
wwang93 Feb 18, 2024
fa4a05c
Update tkonfound_fig.R
wwang93 Feb 18, 2024
0c3aa11
Update zzz.R
wwang93 Feb 18, 2024
853ae6e
Update helper_output_table.R
wwang93 Feb 18, 2024
5064575
Update nonlinear_auxiliary.R
wwang93 Feb 18, 2024
2af153f
Update test_cop.R
wwang93 Feb 18, 2024
db41c99
Update test_pse.R
wwang93 Feb 18, 2024
8d1482f
Update test_sensitivity_ln.R
wwang93 Feb 18, 2024
04586eb
Update test_pse.R
wwang93 Feb 19, 2024
e761827
Update test_cop.R
wwang93 Feb 19, 2024
b6d5d13
Update cop_pse_auxiliary.R
wwang93 Feb 19, 2024
fee677d
Update concord1.R
wwang93 Feb 19, 2024
5e4a9d7
Update cop_pse_auxiliary.R
wwang93 Feb 19, 2024
486b54f
Update core-sensitivity-mkonfound.R
wwang93 Feb 19, 2024
fc0c914
Update konfound-glm.R
wwang93 Feb 19, 2024
fa26fab
Update konfound-lmer.R
wwang93 Feb 19, 2024
519609c
Update mkonfound.R
wwang93 Feb 19, 2024
bcc60d2
Update nonlinear_auxiliary.R
wwang93 Feb 19, 2024
6135139
Update zzz.R
wwang93 Feb 19, 2024
36f5fd0
Update DESCRIPTION
wwang93 Feb 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update test_sensitivity_ln.R
wwang93 authored Feb 18, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 8d1482fabbf57de38091c12b7d9b65e56bc6492b
8 changes: 4 additions & 4 deletions R/test_sensitivity_ln.R
Original file line number Diff line number Diff line change
@@ -17,22 +17,22 @@

## error message if input is inappropriate
if (!(std_err > 0)) {
stop("Did not run! Standard error needs to be greater than zero.")}

Check warning on line 20 in R/test_sensitivity_ln.R

Codecov / codecov/patch

R/test_sensitivity_ln.R#L20

Added line #L20 was not covered by tests
if (!(n_obs > n_covariates + 3)) {
stop("Did not run! There are too few observations relative to
the number of observations and covariates. Please specify a
less complex model to use KonFound-It.")}

Check warning on line 24 in R/test_sensitivity_ln.R

Codecov / codecov/patch

R/test_sensitivity_ln.R#L22-L24

Added lines #L22 - L24 were not covered by tests

thr_t <- cal_thr_t(est_eff, alpha, tails, n_obs, n_covariates)

# stop message
if (n_obs <= 0 || n_treat <= 0) {
stop("Please enter positive integers for sample size
and number of treatment group cases.")

Check warning on line 31 in R/test_sensitivity_ln.R

Codecov / codecov/patch

R/test_sensitivity_ln.R#L30-L31

Added lines #L30 - L31 were not covered by tests
}
if (n_obs <= n_treat) {
stop("The total sample size should be larger than
the number of treatment group cases.")

Check warning on line 35 in R/test_sensitivity_ln.R

Codecov / codecov/patch

R/test_sensitivity_ln.R#L34-L35

Added lines #L34 - L35 were not covered by tests
}

odds_ratio <- exp(est_eff)
@@ -63,18 +63,18 @@
# to record the original treatment cases in case we need to adjust it
# user_ntrm <- n_treat
# check if the implied table solution may contain imaginary numbers
# haveimaginary <- F
# changepi <- F
# haveimaginary <- FALSE
# changepi <- FALSE
# set the default value for whether we need and can adjust pi (ratio of treatment cases)
# to remove the imaginary part
# keyimagin <- (4 + 4 * odds_ratio^2 + odds_ratio *
# (-8 + 4 * n_obs * std_err^2 - n_obs * n_treat * std_err^4 + n_treat^2 * std_err^4))
# minimgain <- 4 + 4 * odds_ratio^2 + odds_ratio * (-8 + n_obs * std_err^2 * (4 - 0.25 * n_obs * std_err^2))
# keyx1 <- 4 + 4 * odds_ratio^2 + odds_ratio * (-8 + 4 * n_obs * std_err^2)
# if (keyimagin > 0) {
# haveimaginary <- T
# haveimaginary <- TRUE
# if (minimgain <= 0 && keyx1 > 0) {
# changepi <- T
# changepi <- TRUE
# n_treat <- n_obs * get_pi(odds_ratio, std_err, n_obs, n_treat)
# n_cnt <- n_obs - n_treat
# } else {
@@ -222,11 +222,11 @@
change, sprintf("you would need to replace %d", RIR), RIRway, "cases")

if (replace == "control") {
conclusion1a <- sprintf("with cases for which the probability of
failure in the control group applies (RIR = %d).", RIR)

Check warning on line 226 in R/test_sensitivity_ln.R

Codecov / codecov/patch

R/test_sensitivity_ln.R#L225-L226

Added lines #L225 - L226 were not covered by tests
} else {
conclusion1a <- sprintf("with cases for which the probability of failure
in the entire sample applies (RIR = %d).", RIR)

Check warning on line 229 in R/test_sensitivity_ln.R

Codecov / codecov/patch

R/test_sensitivity_ln.R#L228-L229

Added lines #L228 - L229 were not covered by tests
}

conclusion1b <- paste(
@@ -239,14 +239,14 @@
conclusion1 <- paste(
change, c("only transferring cases from"), transferway, "is not enough.")

conclusion1b <- paste(sprintf("We also need to transfer %d cases from",
final_solution$final_extra),
transferway_extra, c("as shown, from the User-entered
Table to the Transfer Table. "))

Check warning on line 245 in R/test_sensitivity_ln.R

Codecov / codecov/patch

R/test_sensitivity_ln.R#L242-L245

Added lines #L242 - L245 were not covered by tests

conclusion1c <- paste(sprintf("This means we need to replace %d of", RIR), RIRway,
sprintf("with null hypothesis cases; and replace %d",
RIR_extra), RIRway_extra,

Check warning on line 249 in R/test_sensitivity_ln.R

Codecov / codecov/patch

R/test_sensitivity_ln.R#L248-L249

Added lines #L248 - L249 were not covered by tests
c("with null hypothesis cases to change the inference.")
)
}

Unchanged files with check annotations Beta

sample.nobs = n_obs)
},
error = function(e){
flag_cov = FALSE

Check warning on line 141 in R/cop_pse_auxiliary.R

Codecov / codecov/patch

R/cop_pse_auxiliary.R#L141

Added line #L141 was not covered by tests
return(flag_cov)
},
warning = function(w){
flag_cov = FALSE

Check warning on line 145 in R/cop_pse_auxiliary.R

Codecov / codecov/patch

R/cop_pse_auxiliary.R#L145

Added line #L145 was not covered by tests
return(flag_cov)
}
)
sample.nobs = n_obs)
},
error = function(e){
flag_cor = FALSE

Check warning on line 185 in R/cop_pse_auxiliary.R

Codecov / codecov/patch

R/cop_pse_auxiliary.R#L185

Added line #L185 was not covered by tests
return(flag_cor)
},
warning = function(w){
flag_cor = FALSE

Check warning on line 189 in R/cop_pse_auxiliary.R

Codecov / codecov/patch

R/cop_pse_auxiliary.R#L189

Added line #L189 was not covered by tests
return(flag_cor)
}
)
sample.nobs = n_obs)
},
error = function(e){
flag_cov = FALSE

Check warning on line 285 in R/cop_pse_auxiliary.R

Codecov / codecov/patch

R/cop_pse_auxiliary.R#L285

Added line #L285 was not covered by tests
return(flag_cov)
},
warning = function(w){
flag_cov = FALSE

Check warning on line 289 in R/cop_pse_auxiliary.R

Codecov / codecov/patch

R/cop_pse_auxiliary.R#L289

Added line #L289 was not covered by tests
return(flag_cov)
}
)
sample.nobs = n_obs)
},
error = function(e){
flag_cov = FALSE

Check warning on line 344 in R/cop_pse_auxiliary.R

Codecov / codecov/patch

R/cop_pse_auxiliary.R#L344

Added line #L344 was not covered by tests
return(flag_cov)
},
warning = function(w){
flag_cov = FALSE

Check warning on line 348 in R/cop_pse_auxiliary.R

Codecov / codecov/patch

R/cop_pse_auxiliary.R#L348

Added line #L348 was not covered by tests
return(flag_cov)
}
)
# Stop messages
if (!(class(model_object)[1] %in% c("lm", "glm", "lmerMod"))) {
stop("konfound() is currently implemented for models estimated with
lm(), glm(), and lme4::lmer(); consider using pkonfound() instead")

Check warning on line 73 in R/konfound.R

Codecov / codecov/patch

R/konfound.R#L72-L73

Added lines #L72 - L73 were not covered by tests
}
# Dealing with non-standard evaluation
if (inherits(model_object, "glm") & two_by_two == TRUE) {
if(is.null(n_treat)) stop("Please provide a value for n_treat to use
this functionality with a dichotomous predictor")

Check warning on line 119 in R/konfound.R

Codecov / codecov/patch

R/konfound.R#L118-L119

Added lines #L118 - L119 were not covered by tests
output <- konfound_glm_dichotomous(
model_object = model_object,
df_vec <- pull(select(d, !!df_enquo))
if (length(t_vec) <= 1) {
stop("To carry out sensitivity analysis
for a single study, use pkonfound()")

Check warning on line 41 in R/mkonfound.R

Codecov / codecov/patch

R/mkonfound.R#L40-L41

Added lines #L40 - L41 were not covered by tests
}
results_df <- purrr::map2_dfr(.x = t_vec, .y = df_vec,
FR2max,
FR2max_multiplier = 1.3,
to_return = "print") {
if ("table" %in% to_return) stop("a table can only be
output when using konfound")

Check warning on line 93 in R/pkonfound.R

Codecov / codecov/patch

R/pkonfound.R#L92-L93

Added lines #L92 - L93 were not covered by tests
if (index == "COP") {
} else if(!is.null(a)) {
# error handling
if (is.null(a) | is.null(b) | is.null(c) | is.null(d)) {
stop("Please enter values for a, b, c,
and d to use the 2 x 2 table functionality")

Check warning on line 142 in R/pkonfound.R

Codecov / codecov/patch

R/pkonfound.R#L141-L142

Added lines #L141 - L142 were not covered by tests
}
out <- tkonfound(a = a,
var_z <- sdz <- 1
## error message if input is inappropriate
if (!(std_err > 0)) {stop("Did not run! Standard error needs to be
greater than zero.")}
if (!(sdx > 0)) {stop("Did not run! Standard deviation of x needs to be
greater than zero.")}
if (!(sdy > 0)) {stop("Did not run! Standard deviation of y needs to be
greater than zero.")}

Check warning on line 46 in R/test_cop.R

Codecov / codecov/patch

R/test_cop.R#L41-L46

Added lines #L41 - L46 were not covered by tests
if (!(n_obs > n_covariates + 3)) {
stop("Did not run! There are too few observations relative to the
number of observations and covariates. Please specify a less
complex model to use KonFound-It.")}

Check warning on line 50 in R/test_cop.R

Codecov / codecov/patch

R/test_cop.R#L48-L50

Added lines #L48 - L50 were not covered by tests
if (!(R2 < FR2max)) {stop("Did not run! R2 Max needs to be greater than R2.")}
if (!(FR2max < 1)) {stop("Did not run! R2 Max needs to be less than 1.")}
if (!(1-((sdy^2/sdx^2)*(1-R2)/((df+1) * std_err^2))>0)) {
stop("Did not run! Entered values produced Rxz^2 <=0,
consider adding more significant digits to your entered values.")}

Check warning on line 55 in R/test_cop.R

Codecov / codecov/patch

R/test_cop.R#L54-L55

Added lines #L54 - L55 were not covered by tests
# an indicator of whether the use specified est_eff is negative,
# 1 is yes negative
cat("This function calculates delta* and the exact value of delta.")
cat("\n")
if (negest == 1) {
cat("Using the absolute value of the estimated effect,
results can be interpreted by symmetry.")

Check warning on line 336 in R/test_cop.R

Codecov / codecov/patch

R/test_cop.R#L335-L336

Added lines #L335 - L336 were not covered by tests
cat("\n")
}
cat(sprintf("delta* is %.3f (assuming no covariates in the baseline model M1),
the exact delta is %.3f, with a bias of %.3f%%.",
delta_star, delta_exact, delta_pctbias))

Check warning on line 341 in R/test_cop.R

Codecov / codecov/patch

R/test_cop.R#L339-L341

Added lines #L339 - L341 were not covered by tests
cat("\n")
cat(sprintf("With delta*, the coefficient in the final model will be %.3f.
With the exact delta, the coefficient will be %.3f.",

Check warning on line 344 in R/test_cop.R

Codecov / codecov/patch

R/test_cop.R#L343-L344

Added lines #L343 - L344 were not covered by tests
eff_x_M3_oster,eff_x_M3))
cat("\n")
cat("Use to_return = raw_ouput to see more specific results
and graphic presentation of the result.")

Check warning on line 348 in R/test_cop.R

Codecov / codecov/patch

R/test_cop.R#L347-L348

Added lines #L347 - L348 were not covered by tests
cat("\n")
cat("\n")
cat("This function also calculates conditional RIR that
invalidates the statistical inference.")

Check warning on line 352 in R/test_cop.R

Codecov / codecov/patch

R/test_cop.R#L351-L352

Added lines #L351 - L352 were not covered by tests
cat("\n")
cat("If the replacement cases have a fixed value, then RIR =", cond_RIR_fixedY, ".")
cat("\n")
cat("If the replacement cases follow a null distribution,
then RIR =", cond_RIR_null, ".")

Check warning on line 357 in R/test_cop.R

Codecov / codecov/patch

R/test_cop.R#L356-L357

Added lines #L356 - L357 were not covered by tests
cat("\n")
cat("If the replacement cases satisfy rxy|Z = 0, then RIR =", cond_RIR_rxyz, ".")
cat("\n")
df = n_obs - n_covariates - 3
## error message if input is inappropriate
if (!(std_err > 0)) {stop("Did not run! Standard error needs
to be greater than zero.")}
if (!(sdx > 0)) {stop("Did not run! Standard deviation of
x needs to be greater than zero.")}
if (!(sdy > 0)) {stop("Did not run! Standard deviation of
y needs to be greater than zero.")}

Check warning on line 25 in R/test_pse.R

Codecov / codecov/patch

R/test_pse.R#L20-L25

Added lines #L20 - L25 were not covered by tests
if (!(n_obs > n_covariates + 3)) {
stop("Did not run! There are too few observations relative
to the number of observations and covariates.
Please specify a less complex model to use KonFound-It.")}

Check warning on line 29 in R/test_pse.R

Codecov / codecov/patch

R/test_pse.R#L27-L29

Added lines #L27 - L29 were not covered by tests
if (!(0 < R2)) {stop("Did not run! R2 needs to be greater than zero.")}
if (!(R2 < 1)) {stop("Did not run! R2 needs to be less than one.")}
if (!(1-((sdy^2/sdx^2)*(1-R2)/((df+1) * std_err^2))>0)) {
stop("Did not run! Entered values produced Rxz^2 <=0,
consider adding more significant digits to your entered values.")}

Check warning on line 34 in R/test_pse.R

Codecov / codecov/patch

R/test_pse.R#L33-L34

Added lines #L33 - L34 were not covered by tests
## now standardize
beta_thr <- eff_thr * sdx / sdy
}
if (to_return == "print") {
cat("This function calculates the conditions that set the
estimated effect approximately equal to the threshold
while preserving the standard error.")

Check warning on line 148 in R/test_pse.R

Codecov / codecov/patch

R/test_pse.R#L146-L148

Added lines #L146 - L148 were not covered by tests
cat("\n")
cat(sprintf("The correlation between X and CV is %.3f,
and the correlation between Y and CV is %.3f.", rxcv, rycv))

Check warning on line 151 in R/test_pse.R

Codecov / codecov/patch

R/test_pse.R#L150-L151

Added lines #L150 - L151 were not covered by tests
cat("\n")
cat(sprintf("Conditional on the covariates, the correlation between X
and CV is %.3f, and the correlation between Y and CV
is %.3f.", rxcvGz, rycvGz))

Check warning on line 155 in R/test_pse.R

Codecov / codecov/patch

R/test_pse.R#L153-L155

Added lines #L153 - L155 were not covered by tests
cat("\n")
cat(sprintf("Including such CV, the coefficient changes to %.3f,
and standard error is %.3f.", eff_x_M3, se_x_M3))

Check warning on line 158 in R/test_pse.R

Codecov / codecov/patch

R/test_pse.R#L157-L158

Added lines #L157 - L158 were not covered by tests
cat("\n")
cat("Use to_return = raw_ouput to see more specific results.")
}
to_return,
model_object,
tested_variable) {
if (nu != 0) warning("You entered a non-zero null hypothesis about an effect;
this is being interpreted in terms of a partial correlation.
Sampling variability is not accounted for.")

Check warning on line 39 in R/test_sensitivity.R

Codecov / codecov/patch

R/test_sensitivity.R#L37-L39

Added lines #L37 - L39 were not covered by tests
## error message if input is inappropriate
if (!(std_err > 0)) {
stop("Did not run! Standard error needs to be greater than zero.")}

Check warning on line 43 in R/test_sensitivity.R

Codecov / codecov/patch

R/test_sensitivity.R#L43

Added line #L43 was not covered by tests
if (!(n_obs > n_covariates + 3)) {
stop("Did not run! There are too few observations relative to the number
of observations and covariates. Please specify a less complex model
to use KonFound-It.")}

Check warning on line 47 in R/test_sensitivity.R

Codecov / codecov/patch

R/test_sensitivity.R#L45-L47

Added lines #L45 - L47 were not covered by tests
# calculating statistics used in every case
if (est_eff < 0) {
} else if (to_return == "table") {
return(output_table(model_object, tested_variable))
} else {
stop("to_return must be set to 'raw_output', 'print', 'table',
'thresh_plot', or 'corr_plot' or some combination thereof")

Check warning on line 113 in R/test_sensitivity.R

Codecov / codecov/patch

R/test_sensitivity.R#L112-L113

Added lines #L112 - L113 were not covered by tests
}
}
the control group applies (RIR = %d). ", RIR))
} else {
conclusion1b <- paste0(
sprintf(" cases for which the probability of failure in
the entire group applies (RIR = %d). ", RIR))

Check warning on line 159 in R/tkonfound.R

Codecov / codecov/patch

R/tkonfound.R#L158-L159

Added lines #L158 - L159 were not covered by tests
}
conclusion1c <- paste0(
if (replace == "control") {
conclusion1b <- paste0(
sprintf(" cases for which the probability of failure in
the control group applies (RIR = %d). ", RIR))

Check warning on line 175 in R/tkonfound.R

Codecov / codecov/patch

R/tkonfound.R#L174-L175

Added lines #L174 - L175 were not covered by tests
} else {
conclusion1b <- paste0(
sprintf(" cases for which the probability of failure in
the entire group applies (RIR = %d). ", RIR))

Check warning on line 179 in R/tkonfound.R

Codecov / codecov/patch

R/tkonfound.R#L178-L179

Added lines #L178 - L179 were not covered by tests
}
conclusion1c <- paste0(
if (test == "chisq"){
conclusion2 <- sprintf(
"For the User-entered Table, we have a Pearson's chi square of %.3f,
with p-value of %.3f:", chisq_ob, p_ob)

Check warning on line 206 in R/tkonfound.R

Codecov / codecov/patch

R/tkonfound.R#L205-L206

Added lines #L205 - L206 were not covered by tests
conclusion3 <- sprintf(
"For the Transfer Table, we have a Pearson's chi square of %.3f,
with p-value of %.3f:", chisq_final, p_final)

Check warning on line 209 in R/tkonfound.R

Codecov / codecov/patch

R/tkonfound.R#L208-L209

Added lines #L208 - L209 were not covered by tests
}
if (test == "fisher"){
}
if (neg_thr_pdif <= max(zoom$pdif) && neg_thr_pdif >= min(zoom$pdif)) {
fig2 <- fig2 + ggplot2::geom_hline(yintercept = neg_thr_pdif,
linetype = "dashed", color="red", size = 1)

Check warning on line 318 in R/tkonfound_fig.R

Codecov / codecov/patch

R/tkonfound_fig.R#L317-L318

Added lines #L317 - L318 were not covered by tests
}
###plot figure 3 RIS% as sample size gets larger, using t statistic as the criterion