-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added getAttenuatedR() and tweakes to whichSetOfN().
- Loading branch information
Showing
11 changed files
with
180 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Package: CECPfuns | ||
Type: Package | ||
Title: Package of Utility Functions for Psychological Therapies, Mental Health and Well-being Work (Created by Chris Evans and Clara Paz) | ||
Version: 0.0.0.9051 | ||
Version: 0.0.0.9052 | ||
Maintainer: Chris Evans <[email protected]> | ||
Description: This should evolve into a repository of all the functions that I (CE) | ||
and Clara Paz (CP) have created (so far only CE!) and tested enough to | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#' Function that gives the attenuated R for an unattenuated R and two reliability values | ||
#' @description | ||
#' This is just the conventional formula for the attenuation of a (Pearson) correlation by unreliability. | ||
#' | ||
#' @param unattR unattenuated R | ||
#' @param rel1 reliability of first of the variables (order of variables is arbitrary) | ||
#' @param rel2 reliability of second of the variables (order of variables is arbitrary) | ||
#' | ||
#' @return numeric: attenuated correlation | ||
#' | ||
#' @family utility functions | ||
#' | ||
#' @section Background: | ||
#' This is ancient psychometrics but still of some use. For more information, see: | ||
#' \href{https://www.psyctc.org/psyctc/glossary2/attenuation-by-unreliability-of-measurement/}{OMbook glossary entry for attenuation} | ||
#' The formula is simple: | ||
#' \loadmathjax{} | ||
#' | ||
#' \mjdeqn{correctedCorr=observedCorr*\sqrt{rel_{1}*rel_{2}}}{} | ||
#' | ||
#' The short summary is that unreliability in the measurement of both variables involved in a correlation | ||
#' always reduces the observed correlation between the variables from what it would have been had the | ||
#' variables been measured with no unreliability (which is essentially impossible for any self-report measures | ||
#' and pretty much any measures used in our fields. | ||
#' | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' getAttenuatedR(.9, .7, .8) | ||
#' | ||
#' | ||
#' | ||
#' @author Chris Evans | ||
#' @section History/development log: | ||
#' Started 12.x.24 | ||
#' | ||
getAttenuatedR <- function(unattR, rel1, rel2) { | ||
### sanity checking | ||
if (!is.numeric(unattR)) { | ||
stop(paste0("You input ", | ||
unattR, | ||
" for unattR, it must be numeric. Fix it!!" | ||
)) | ||
} | ||
if (!is.numeric(rel1)) { | ||
stop(paste0("You input ", | ||
rel1, | ||
" for rel1, it must be numeric. Fix it!!" | ||
)) | ||
} | ||
if (!is.numeric(rel2)) { | ||
stop(paste0("You input ", | ||
rel2, | ||
" for rel2, it must be numeric. Fix it!!" | ||
)) | ||
} | ||
if (unattR < -1 | unattR > 1) { | ||
stop("unattr must be between -1 and +1.") | ||
} | ||
if (rel1 < .01 | rel1 >= 1) { | ||
stop("For this function rel1 must be between .01 and 1.0.") | ||
} | ||
if (rel2 < .01 | rel2 >= 1) { | ||
stop("For this function rel2 must be between .01 and 1.0.") | ||
} | ||
if (length(unattR) > 1) { | ||
stop("Sorry, you entered more than one value for unattr, this function only handles single values.") | ||
} | ||
if (length(rel1) > 1) { | ||
stop("Sorry, you entered more than one value for rel, this function only handles single values.") | ||
} | ||
if (length(rel2) > 1) { | ||
stop("Sorry, you entered more than one value for rel2, this function only handles single values.") | ||
} | ||
|
||
### OK, do it! | ||
unattR * sqrt(rel1 * rel2) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
testthat::test_that("sanity checks work", { | ||
testthat::expect_error(getAttenuatedR("A", .7, .7)) | ||
testthat::expect_error(getAttenuatedR(.9, "A", .7)) | ||
testthat::expect_error(getAttenuatedR(.9, .7, "A")) | ||
testthat::expect_error(getAttenuatedR(c(.9, .7), "A", .7)) | ||
testthat::expect_error(getAttenuatedR(.9, c(.7, .8), .7)) | ||
testthat::expect_error(getAttenuatedR(.9, .7, c(.7, .8))) | ||
testthat::expect_error(getAttenuatedR(-2, .7, .7)) | ||
testthat::expect_error(getAttenuatedR(1.2, .7, .7)) | ||
testthat::expect_error(getAttenuatedR(.8, .0001, .7)) | ||
testthat::expect_error(getAttenuatedR(.8, .7, .0001)) | ||
testthat::expect_error(getAttenuatedR(.8, 1.2, .7)) | ||
testthat::expect_error(getAttenuatedR(.8, .7, 1.2)) | ||
}) | ||
|
||
## test warnings | ||
|
||
### test of outputs | ||
testthat::test_that("Output correct", { | ||
set.seed(12345) | ||
testthat::expect_equal(getAttenuatedR(.9, .7, .7), | ||
.63) | ||
}) |
4 changes: 3 additions & 1 deletion
4
tests/testthat/whichSetOfN.R → tests/testthat/test-whichSetOfN.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters