diff --git a/DESCRIPTION b/DESCRIPTION index 4430c35..7820357 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: WebPower Title: Basic and Advanced Statistical Power Analysis -Version: 0.9.0 -Date: 2023-04-16 +Version: 0.9.2 +Date: 2023-05-09 Authors@R: c(person("Zhiyong", "Zhang", role = c("aut", "cre"), email = "johnnyzhz@gmail.com"), person("Yujiao", "Mai", role = "aut"), @@ -25,6 +25,6 @@ Encoding: UTF-8 LazyLoad: yes LazyData: yes NeedsCompilation: no -Packaged: 2023-04-20 17:05:43 UTC; zzhang4 +Packaged: 2023-05-10 01:01:20 UTC; zzhang4 Repository: CRAN -Date/Publication: 2023-04-20 18:02:37 UTC +Date/Publication: 2023-05-10 02:30:02 UTC diff --git a/MD5 b/MD5 index b09b306..85c953c 100644 --- a/MD5 +++ b/MD5 @@ -1,10 +1,10 @@ -c789f5404bdf5b6774c5fde70fe240b2 *DESCRIPTION +268d8f29a27a853530f1423bdad590fd *DESCRIPTION 7d64335ba38d220709a921137814ee14 *NAMESPACE -a0bcec493c022a6e90c045aff311c3a6 *R/model14.R -e27c5aff1b059e0363768f2d08fa7e1e *R/model15.R -5208bbf4ba29a6d91130aa89f2a4060c *R/model58.R -bf344a714d712897d11ca09860ab631a *R/model7.R -df3ac23988eb7f24ce567e8c9f2ea0c6 *R/model8.R +950a9b89b49627f0f4d37baf4b901a1b *R/model14.R +11d33652db6615837b9b682f2ab7a2da *R/model15.R +ee4d00e15593b6ec2b9f583171095aef *R/model58.R +cf095a79c2a8266843b3d147df174096 *R/model7.R +19cd026ef07667091f1bd61ffc17b15a *R/model8.R 7e04ca484da8d6ceb462867d83b1ada8 *R/webpower.R 83b0027813eefbbbb56cdfae00a9adcf *data/webpower.RData f4c53917e7a6ce6720539abe8c6f18a1 *inst/CRT2.txt @@ -44,11 +44,11 @@ f93dcccad006d428281f7e0a359bacb0 *man/wp.mc.sem.power.curve.Rd e3aa54355131249a67530e681459f10a *man/wp.mc.t.Rd 60375052e10b52d9268e0edb729742e2 *man/wp.mediation.Rd b82ac428a511dcf6b45e9af35422e00b *man/wp.mmrm.Rd -dda9ad6387dcd7282f099fc61ff2e8f9 *man/wp.modmed.m14.Rd -997b1223aa006f8f095ca550cef30a6d *man/wp.modmed.m15.Rd -7bb3072286b579188a32716c571c1617 *man/wp.modmed.m58.Rd -0a91119994e601cb5b8dde2c92595d4f *man/wp.modmed.m7.Rd -71cb09b42c462ecb0c5ddde411530cc1 *man/wp.modmed.m8.Rd +c97d1a521efad8bfe8b5a2f8a4c9b598 *man/wp.modmed.m14.Rd +6a70dca0f9a5479a08ea3053943961cc *man/wp.modmed.m15.Rd +6014bbfc4c4a41fcc2f617a0681cb093 *man/wp.modmed.m58.Rd +241b0ad20c9c7ba24f9ad71e8ca0880a *man/wp.modmed.m7.Rd +cb4817b654955c55b6c5aba5a4c72886 *man/wp.modmed.m8.Rd 25eb6c557b4a4accd999ef15bc8cac40 *man/wp.mrt2arm.Rd 9015883be37879a88e9585d072e03de6 *man/wp.mrt3arm.Rd cf30c06a5737b7fa17b8f3f65fd7a6f0 *man/wp.poisson.Rd diff --git a/R/model14.R b/R/model14.R index 6bd7342..45a3735 100644 --- a/R/model14.R +++ b/R/model14.R @@ -5,10 +5,10 @@ #' power analysis of model 14 in Introduction to Mediation, Moderation, and Conditional Process Analysis #' #' @param a1 regression coefficient of mediator (m) on predictor (x) -#' @param cc regression coefficient of outcome (y) on predictor (x) +#' @param cp regression coefficient of outcome (y) on predictor (x) #' @param b1 regression coefficient of outcome (y) on mediator (m) -#' @param c1 regression coefficient of outcome (y) on moderator (w) -#' @param c2 regression coefficient of outcome (y) on the product (mw) +#' @param d1 regression coefficient of outcome (y) on moderator (w) +#' @param b2 regression coefficient of outcome (y) on the product (mw) #' @param sigx2 variance of predictor (x) #' @param sigw2 variance of moderator (w) #' @param sige12 variance of error in the first regression equation @@ -28,13 +28,13 @@ #' @return power of indirect effect, direct effect, and moderation #' @export #' @examples -#' test = wp.modmed.m14(a1 = 0.2, cc = 0.2, b1 = 0.5, c1 = 0.5, c2 = 0.2, sigx2 = 1, +#' test = wp.modmed.m14(a1 = 0.2, cp = 0.2, b1 = 0.5, d1 = 0.5, b2 = 0.2, sigx2 = 1, #' sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.5, n = 50, #' nrep = 100, alpha = 0.05, b = 1000, ncore = 1) #' print(test) -wp.modmed.m14 <- function (a1 = 0.2, cc = 0.2, b1 = 0.5, c1 = 0.5, c2 = 0.2, +wp.modmed.m14 <- function (a1 = 0.2, cp = 0.2, b1 = 0.5, d1 = 0.5, b2 = 0.2, sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, - sigx_w = 0.5, n = 100, nrep = 1000, alpha = 0.05, + sigx_w = 0.5, n = 100, nrep = 100, alpha = 0.05, b = 1000, nb = n, w_value = 0, method = "value", ncore = 1, pop.cov = NULL, mu = NULL, varnames = c('y', 'x', 'w', 'm', 'mw')) @@ -43,15 +43,15 @@ wp.modmed.m14 <- function (a1 = 0.2, cc = 0.2, b1 = 0.5, c1 = 0.5, c2 = 0.2, if (is.null(pop.cov) || is.null(mu)) { sigm2 = a1^2*sigx2 + sige12 sigxw2 = sigx2*sigw2 + sigx_w^2 - sigy2 = (cc + a1*b1)^2*sigx2 + c1^2*sigw2 + c2^2*sige12 * - sigw2 + (a1*c2)^2*sigxw2 + b1^2*sige12 + sige22 + 2*(cc + a1*b1)*c1*sigx_w + sigy2 = (cp + a1*b1)^2*sigx2 + d1^2*sigw2 + b2^2*sige12 * + sigw2 + (a1*b2)^2*sigxw2 + b1^2*sige12 + sige22 + 2*(cp + a1*b1)*d1*sigx_w sigmw2 = a1^2*sigxw2 + sige12*sigw2 sigx_m = a1*sigx2 - sigx_y = (cc + a1*b1)*sigx2 + c1*sigx_w - sigm_y = a1*(cc + a1*b1)*sigx2 + a1*c1*sigx_w + b1*sige12 + sigx_y = (cp + a1*b1)*sigx2 + d1*sigx_w + sigm_y = a1*(cp + a1*b1)*sigx2 + a1*d1*sigx_w + b1*sige12 sigm_w = a1*sigx_w - sigy_w = (cc + a1*b1)*sigx_w + c1*sigw2 - sigy_mw = a1^2*c2*sigxw2 + c2*sige12*sigw2 + sigy_w = (cp + a1*b1)*sigx_w + d1*sigw2 + sigy_mw = a1^2*b2*sigxw2 + b2*sige12*sigw2 # y, x, w, m, mw pop.cov = array( @@ -64,7 +64,7 @@ wp.modmed.m14 <- function (a1 = 0.2, cc = 0.2, b1 = 0.5, c1 = 0.5, c2 = 0.2, ) u_mw = a1*sigx_w - u_y = a1*c2*sigx_w + u_y = a1*b2*sigx_w colnames(pop.cov) = rownames(pop.cov) = c('y', 'x', 'w', 'm', 'mw') mu = c(u_y, 0, 0, 0, u_mw) }else{ @@ -86,21 +86,21 @@ wp.modmed.m14 <- function (a1 = 0.2, cc = 0.2, b1 = 0.5, c1 = 0.5, c2 = 0.2, test_boot2 = lm(y ~ x + m + w + mw, data = boot_data) boot_CI = test_boot1$coefficients[2]*(test_boot2$coefficients[3] + test_boot2$coefficients[5]*w_value) boot_CD = test_boot2$coefficients[2] - boot_c2 = as.numeric(test_boot2$coefficients[5]) + boot_b2 = as.numeric(test_boot2$coefficients[5]) boot_CI1 = test_boot1$coefficients[2] boot_CI2 = (test_boot2$coefficients[3] + test_boot2$coefficients[5]*w_value) - return(list(boot_CI, boot_CD, boot_c2, boot_CI1, boot_CI2)) + return(list(boot_CI, boot_CD, boot_b2, boot_CI1, boot_CI2)) } boot_effect = lapply(1:b, bootstrap) boot_CI = matrix(0, ncol = 1, nrow = b) boot_CD = matrix(0, ncol = 1, nrow = b) - boot_c2 = matrix(0, ncol = 1, nrow = b) + boot_b2 = matrix(0, ncol = 1, nrow = b) boot_CI1 = matrix(0, ncol = 1, nrow = b) boot_CI2 = matrix(0, ncol = 1, nrow = b) boot_CI = t(sapply(1:b, function(i) unlist(boot_effect[[i]][1]))) boot_CD = t(sapply(1:b, function(i) unlist(boot_effect[[i]][2]))) - boot_c2 = t(sapply(1:b, function(i) unlist(boot_effect[[i]][3]))) + boot_b2 = t(sapply(1:b, function(i) unlist(boot_effect[[i]][3]))) boot_CI1 = t(sapply(1:b, function(i) unlist(boot_effect[[i]][4]))) boot_CI2 = t(sapply(1:b, function(i) unlist(boot_effect[[i]][5]))) @@ -109,7 +109,7 @@ wp.modmed.m14 <- function (a1 = 0.2, cc = 0.2, b1 = 0.5, c1 = 0.5, c2 = 0.2, interval_CI1 = matrix(0, ncol = 1, nrow = 2) interval_CI2 = matrix(0, ncol = 1, nrow = 2) interval_CD = matrix(0, ncol = 1, nrow = 2) - interval_c2 = matrix(0, ncol = 1, nrow = 2) + interval_b2 = matrix(0, ncol = 1, nrow = 2) interval_CI[, 1] = quantile(boot_CI, probs = c(alpha / 2, 1 - alpha / 2), @@ -123,18 +123,18 @@ wp.modmed.m14 <- function (a1 = 0.2, cc = 0.2, b1 = 0.5, c1 = 0.5, c2 = 0.2, interval_CD[, 1] = quantile(boot_CD, probs = c(alpha / 2, 1 - alpha / 2), names = T) - interval_c2[, 1] = quantile(boot_c2, + interval_b2[, 1] = quantile(boot_b2, probs = c(alpha / 2, 1 - alpha / 2), names = T) r_CI = as.numeric(!sapply(1, function(i) dplyr::between(0, interval_CI[1, i], interval_CI[2, i]))) r_CD = as.numeric(!sapply(1, function(i) dplyr::between(0, interval_CD[1, i], interval_CD[2, i]))) - r_c2 = as.numeric(!sapply(1, function(i) dplyr::between(0, interval_c2[1, i], interval_c2[2, i]))) + r_b2 = as.numeric(!sapply(1, function(i) dplyr::between(0, interval_b2[1, i], interval_b2[2, i]))) if (method == "joint") { r_CI = as.numeric(!dplyr::between(0, interval_CI1[1, 1], interval_CI1[2, 1]))*as.numeric(!dplyr::between(0, interval_CI2[1, 1], interval_CI2[2, 1])) } - power = c(r_CI, r_CD, r_c2) + power = c(r_CI, r_CD, r_b2) return(power) } @@ -143,7 +143,7 @@ wp.modmed.m14 <- function (a1 = 0.2, cc = 0.2, b1 = 0.5, c1 = 0.5, c2 = 0.2, parallel::clusterExport( CL1, - c('a1', 'cc', 'b1', 'c1', 'c2', 'sigx2', 'sigw2', + c('a1', 'cp', 'b1', 'd1', 'b2', 'sigx2', 'sigw2', 'sige12', 'sige22', 'sigx_w', 'n', 'nrep', 'alpha', 'b','nb', 'pop.cov', 'mu', 'method' ), @@ -182,4 +182,3 @@ power3 is the power of moderation on the path m to y." } - diff --git a/R/model15.R b/R/model15.R index 62db577..09c0765 100644 --- a/R/model15.R +++ b/R/model15.R @@ -4,11 +4,11 @@ #' power analysis of model 15 in Introduction to Mediation, Moderation, and Conditional Process Analysis #' #' @param a1 regression coefficient of mediator (m) on predictor (x) -#' @param c regression coefficient of outcome (y) on predictor (x) +#' @param cp regression coefficient of outcome (y) on predictor (x) #' @param b1 regression coefficient of outcome (y) on mediator (m) #' @param b2 regression coefficient of outcome (y) on the product (mw) -#' @param c1 regression coefficient of outcome (y) on moderator (w) -#' @param c2 regression coefficient of outcome (y) on the product (xw) +#' @param d1 regression coefficient of outcome (y) on moderator (w) +#' @param d2 regression coefficient of outcome (y) on the product (xw) #' @param sigx2 variance of predictor (x) #' @param sigw2 variance of moderator (w) #' @param sige12 variance of error in the first regression equation @@ -28,13 +28,13 @@ #' @return power of indirect effect, direct effect, and moderation #' @export #' @examples -#' test = wp.modmed.m15(a1 = 0.6, c = 0.2, b1 = 0.3, b2 = 0.2, c1 = 0.2, c2 = 0.1, +#' test = wp.modmed.m15(a1 = 0.6, cp = 0.2, b1 = 0.3, b2 = 0.2, d1 = 0.2, d2 = 0.1, #' sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.4, #' n = 50, nrep = 100, alpha = 0.05, b = 1000, ncore = 1) #'print(test) -wp.modmed.m15 <- function(a1 = 0.6, c = 0.2, b1 = 0.3, b2 = 0.5, c1 = 0.2, c2 = 0.1, +wp.modmed.m15 <- function(a1 = 0.6, cp = 0.2, b1 = 0.3, b2 = 0.5, d1 = 0.2, d2 = 0.1, sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.4, - n = 500, nrep = 1000, alpha = 0.05, b = 1000, nb = n, + n = 100, nrep = 100, alpha = 0.05, b = 1000, nb = n, w_value = 0, method = "value", ncore = 1, pop.cov = NULL, mu = NULL, varnames = c('y','x','w','m','xw','mw')) @@ -42,18 +42,18 @@ wp.modmed.m15 <- function(a1 = 0.6, c = 0.2, b1 = 0.3, b2 = 0.5, c1 = 0.2, c2 = if (is.null(pop.cov) || is.null(mu)){ sigxw2 = sigx2*sigw2 + sigx_w^2 sigm_xw = 0 - sigy_xw = (c2 + a1*b2)*sigxw2 + sigy_xw = (d2 + a1*b2)*sigxw2 sigm_x = a1*sigx2 sigm_w = a1*sigx_w - sigy_x = c1*sigx_w + (c + a1*b1)*sigx2 - sigy_w = c1*sigw2 + (c + a1*b1)*sigx_w + sigy_x = d1*sigx_w + (cp + a1*b1)*sigx2 + sigy_w = d1*sigw2 + (cp + a1*b1)*sigx_w sige1w2 = sige12*sigw2 - sigy2 = (c + a1*b1)^2*sigx2 + (c2 + a1*b2)^2*sigxw2 + b2 ^ - 2*sige1w2 + c1^2*sigw2 + 2*c1*(c + a1*b1)*sigx_w + b1^2 * + sigy2 = (cp + a1*b1)^2*sigx2 + (d2 + a1*b2)^2*sigxw2 + b2 ^ + 2*sige1w2 + d1^2*sigw2 + 2*d1*(cp + a1*b1)*sigx_w + b1^2 * sige12 + sige22 sigm2 = a1^2*sigx2 + sige12 - sigy_m = a1*(c + a1*b1)*sigx2 + a1*c1*sigx_w + b1*sige12 - sigy_mw = a1*(c2 + a1*b2)*sigxw2 + b2*sige1w2 + sigy_m = a1*(cp + a1*b1)*sigx2 + a1*d1*sigx_w + b1*sige12 + sigy_mw = a1*(d2 + a1*b2)*sigxw2 + b2*sige1w2 sigxw_mw = a1*sigxw2 sigmw2 = a1^2*sigxw2 + sige1w2 @@ -71,7 +71,7 @@ wp.modmed.m15 <- function(a1 = 0.6, c = 0.2, b1 = 0.3, b2 = 0.5, c1 = 0.2, c2 = rownames(pop.cov) = colnames(pop.cov) = c('y', 'x', 'w', 'm', 'xw', 'mw') u_xw = sigx_w u_mw = sigm_w - u_y = c2*u_xw + b2*u_mw + u_y = d2*u_xw + b2*u_mw mu = c(u_y, 0, 0, 0, u_xw, u_mw) }else{ pop.cov = pop.cov @@ -93,23 +93,23 @@ wp.modmed.m15 <- function(a1 = 0.6, c = 0.2, b1 = 0.3, b2 = 0.5, c1 = 0.2, c2 = test_boot2 = lm(y ~ x + m + w + xw + mw, data = boot_data) boot_CI = (test_boot2$coefficients[3] + test_boot2$coefficients[6]*w_value)*test_boot1$coefficients[2] boot_CD = test_boot2$coefficients[2] + test_boot2$coefficients[5]*w_value - boot_c2 = test_boot2$coefficients[5] + boot_d2 = test_boot2$coefficients[5] boot_b2 = test_boot2$coefficients[6] boot_CI1 = test_boot1$coefficients[2] boot_CI2 = (test_boot2$coefficients[3] + test_boot2$coefficients[6]*w_value) - return(list(boot_CI, boot_CD, boot_c2, boot_b2, boot_CI1, boot_CI2)) + return(list(boot_CI, boot_CD, boot_d2, boot_b2, boot_CI1, boot_CI2)) } boot_effect = lapply(1:b, bootstrap) boot_CI = matrix(0, ncol = 1, nrow = b) boot_CI1 = matrix(0, ncol = 1, nrow = b) boot_CI2 = matrix(0, ncol = 1, nrow = b) boot_CD = matrix(0, ncol = 1, nrow = b) - boot_c2 = matrix(0, ncol = 1, nrow = b) + boot_d2 = matrix(0, ncol = 1, nrow = b) boot_b2 = matrix(0, ncol = 1, nrow = b) boot_CI=t(sapply(1:b,function(i) unlist(boot_effect[[i]][1]))) boot_CD=t(sapply(1:b,function(i) unlist(boot_effect[[i]][2]))) - boot_c2=t(sapply(1:b,function(i) unlist(boot_effect[[i]][3]))) + boot_d2=t(sapply(1:b,function(i) unlist(boot_effect[[i]][3]))) boot_b2=t(sapply(1:b,function(i) unlist(boot_effect[[i]][4]))) boot_CI1=t(sapply(1:b,function(i) unlist(boot_effect[[i]][5]))) boot_CI2=t(sapply(1:b,function(i) unlist(boot_effect[[i]][6]))) @@ -118,7 +118,7 @@ wp.modmed.m15 <- function(a1 = 0.6, c = 0.2, b1 = 0.3, b2 = 0.5, c1 = 0.2, c2 = interval_CI1=matrix(0,ncol=1,nrow=2) interval_CI2=matrix(0,ncol=1,nrow=2) interval_CD=matrix(0,ncol=1,nrow=2) - interval_c2=matrix(0,ncol=1,nrow=2) + interval_d2=matrix(0,ncol=1,nrow=2) interval_b2=matrix(0,ncol=1,nrow=2) interval_CI[, 1] = quantile(boot_CI, @@ -133,7 +133,7 @@ wp.modmed.m15 <- function(a1 = 0.6, c = 0.2, b1 = 0.3, b2 = 0.5, c1 = 0.2, c2 = interval_CD[, 1] = quantile(boot_CD, probs = c(alpha / 2, 1 - alpha / 2), names = T) - interval_c2[, 1] = quantile(boot_c2, + interval_d2[, 1] = quantile(boot_d2, probs = c(alpha / 2, 1 - alpha / 2), names = T) interval_b2[, 1] = quantile(boot_b2, @@ -143,17 +143,17 @@ wp.modmed.m15 <- function(a1 = 0.6, c = 0.2, b1 = 0.3, b2 = 0.5, c1 = 0.2, c2 = r_CI=as.numeric(!sapply(1,function(i) dplyr::between(0,interval_CI[1,i],interval_CI[2,i]))) r_DI=as.numeric(!sapply(1,function(i) dplyr::between(0,interval_CD[1,i],interval_CD[2,i]))) - r_c2=as.numeric(!sapply(1:1,function(i) dplyr::between(0,interval_c2[1,i],interval_c2[2,i]))) + r_d2=as.numeric(!sapply(1:1,function(i) dplyr::between(0,interval_d2[1,i],interval_d2[2,i]))) r_b2=as.numeric(!sapply(1:1,function(i) dplyr::between(0,interval_b2[1,i],interval_b2[2,i]))) if (method =="joint"){ r_CI=as.numeric(!dplyr::between(0,interval_CI1[1,1],interval_CI1[2,1]))*as.numeric(!dplyr::between(0,interval_CI2[1,1],interval_CI2[2,1])) } - power=c(r_CI,r_DI, r_c2, r_b2) + power=c(r_CI,r_DI, r_d2, r_b2) return(power) } if (ncore > 1){ CL1=parallel::makeCluster(ncore) - parallel::clusterExport(CL1,c('a1','c','b1','b2','c1','c2', + parallel::clusterExport(CL1,c('a1','c','b1','b2','d1','d2', 'sigx2','sigw2','sige12','sige22','sigx_w', 'n','nrep','alpha','b','nb','pop.cov','u_y','u_xw', 'u_mw', 'mu', 'method', 'w_value'),envir = environment()) @@ -187,3 +187,4 @@ power4 is the power of moderation on the path m to y."), class = "webpower") } + diff --git a/R/model58.R b/R/model58.R index c7907e8..0397c38 100644 --- a/R/model58.R +++ b/R/model58.R @@ -3,13 +3,13 @@ #' #' power analysis of model 58 in Introduction to Mediation, Moderation, and Conditional Process Analysis #' -#' @param a1 regression coefficient of outcome (m) on moderator (w) -#' @param b1 regression coefficient of mediator (m) on predictor (x) -#' @param c1 regression coefficient of outcome (m) on the product (xw) -#' @param a2 regression coefficient of outcome (y) on moderator (w) -#' @param b2 regression coefficient of outcome (y) on mediator (m) -#' @param c2 regression coefficient of outcome (y) on the product (mw) -#' @param b3 regression coefficient of outcome (y) on predictor (x) +#' @param c1 regression coefficient of outcome (m) on moderator (w) +#' @param a1 regression coefficient of mediator (m) on predictor (x) +#' @param c2 regression coefficient of outcome (m) on the product (xw) +#' @param d1 regression coefficient of outcome (y) on moderator (w) +#' @param b1 regression coefficient of outcome (y) on mediator (m) +#' @param b2 regression coefficient of outcome (y) on the product (mw) +#' @param cp regression coefficient of outcome (y) on predictor (x) #' @param sigx2 variance of predictor (x) #' @param sigw2 variance of moderator (w) #' @param sige12 variance of error in the first regression equation @@ -29,47 +29,47 @@ #' @return power of indirect effect, direct effect, and moderation #' @export #' @examples -#' test = wp.modmed.m58(a1 = 0.2, b1 = 0.2, c1 = 0.1, c2 = 0.1, -#' b2 = 0.2, b3 = 0.2, a2 = 0.2, +#' test = wp.modmed.m58(c1 = 0.2, a1 = 0.2, c2 = 0.1, b2 = 0.1, +#' b1 = 0.2, cp = 0.2, d1 = 0.2, #' sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.5, #' n = 50, nrep = 100, alpha = 0.05, b = 1000, ncore = 1) #' print(test) -wp.modmed.m58 <- function(a1 = 0.5, b1 = 0.75, c1 = 0.1, a2 = 0.4, b2 = 0.6, c2 = 0.1, - b3 = 0.5, sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.4, - n = 500, nrep = 1000, alpha = 0.05, b=1000, nb = n, +wp.modmed.m58 <- function(c1 = 0.5, a1 = 0.75, c2 = 0.1, d1 = 0.4, b1 = 0.6, b2 = 0.1, + cp = 0.5, sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.4, + n = 100, nrep = 100, alpha = 0.05, b = 1000, nb = n, w_value = 0, method = "value", ncore = 1, pop.cov = NULL, mu = NULL, varnames = c('x', 'w', 'm', 'xw', 'mw', 'y')) { if (is.null(pop.cov) || is.null(mu)){ - sigx_m = b1*sigx2 + a1*sigx_w - sigx_mw = c1*(1 + 2*sigx_w^2 / sigx2 / sigw2)*sigx2*sigw2 - sigx_y = a2*sigx_w + b3*sigx2 + b2*sigx_m + c2*sigx_mw + sigx_m = a1*sigx2 + c1*sigx_w + sigx_mw = c2*(1 + 2*sigx_w^2 / sigx2 / sigw2)*sigx2*sigw2 + sigx_y = d1*sigx_w + cp*sigx2 + b1*sigx_m + b2*sigx_mw sigx_xw = sigw_xw = 0 sigw2 = sigw2 - sigw_m = b1*sigx_w + a1*sigw2 - sigw_mw = 3*c1*sigx_w / sqrt(sigx2) / sqrt(sigw2)*sqrt(sigx2)*(sqrt(sigw2))^3 - sigw_y = a2*sigw2 + b3*sigx_w + b2*sigw_m + c2*sigw_mw + sigw_m = a1*sigx_w + c1*sigw2 + sigw_mw = 3*c2*sigx_w / sqrt(sigx2) / sqrt(sigw2)*sqrt(sigx2)*(sqrt(sigw2))^3 + sigw_y = d1*sigw2 + cp*sigx_w + b1*sigw_m + b2*sigw_mw sigxw2 = sigx2*sigw2 + sigx_w^2 sigxw_w2 = 3*sigx_w*sigw2 - sigx_w*sigw2 - sigm2 = b1^2*sigx2 + a1^2*sigw2 + c1^2*sigxw2 + sige12 + - 2*b1*a1*sigx_w - sigm_xw = c1*sigxw2 - sigm_mw = b1*c1*(sigx2*sigw2 + 2*sigx_w^2) + 3*a1*c1*sigx_w*sigw2 + c1*b1*sigxw2 + c1*a1*sigxw_w2 - sigm_y = (a2 + a1*b2)*sigw_m + (b3 + b1*b2)*sigx_m + b2*c1*sigm_xw + c2*sigm_mw + b2*sige12 + sigm2 = a1^2*sigx2 + c1^2*sigw2 + c2^2*sigxw2 + sige12 + + 2*a1*c1*sigx_w + sigm_xw = c2*sigxw2 + sigm_mw = a1*c2*(sigx2*sigw2 + 2*sigx_w^2) + 3*c1*c2*sigx_w*sigw2 + c2*a1*sigxw2 + c2*c1*sigxw_w2 + sigm_y = (d1 + c1*b1)*sigw_m + (cp + a1*b1)*sigx_m + b1*c2*sigm_xw + b2*sigm_mw + b1*sige12 - sigxw_mw = b1*sigxw2 + a1*(3*sigx_w*sigw2 - sigx_w*sigw2) - sigxw_y = (a2 + a1*b2)*sigw_xw + (b3 + b1*b2)*sigx_xw + b2*c1*sigxw2 + c2*sigxw_mw + sigxw_mw = a1*sigxw2 + c1*(3*sigx_w*sigw2 - sigx_w*sigw2) + sigxw_y = (d1 + c1*b1)*sigw_xw + (cp + a1*b1)*sigx_xw + b1*c2*sigxw2 + b2*sigxw_mw sigxw22 = 3*sigx2*sigw2^2 - 3*sigx_w^2*sigw2 + 15*sigx_w^2*sigw2 sige1w2 = sige12*sigw2 - sigmw2 = b1^2*sigxw2 + 2*a1^2*sigw2^2 + c1^2*sigxw22 + sige1w2 + 2*b1*a1*sigxw_w2 - sigmw_y = (a2 + a1*b2)*sigw_mw + (b3 + b1*b2)*sigx_mw + b2*c1*sigxw_mw + c2*sigmw2 + sigmw2 = a1^2*sigxw2 + 2*c1^2*sigw2^2 + c2^2*sigxw22 + sige1w2 + 2*a1*c1*sigxw_w2 + sigmw_y = (d1 + c1*b1)*sigw_mw + (cp + a1*b1)*sigx_mw + b1*c2*sigxw_mw + b2*sigmw2 - sigy2 = (a2 + a1*b2)^2*sigw2 + (b3 + b1*b2)^2*sigx2 + (b2*c1)^2*sigxw2 + c2^2*sigmw2 + b2^2*sige12 + sige22 + 2*(a2 + a1*b2)*(b3 + b1*b2)*sigx_w + 2*(a2 + a1*b2)*b2*c1*sigw_xw + 2*(a2 + a1*b2)*c2*sigw_mw + 2*(b3 + b1*b2)*c2*sigx_mw + 2*b2*c1*c2*sigxw_mw + sigy2 = (d1 + c1*b1)^2*sigw2 + (cp + a1*b1)^2*sigx2 + (b1*c2)^2*sigxw2 + b2^2*sigmw2 + b1^2*sige12 + sige22 + 2*(d1 + c1*b1)*(cp + a1*b1)*sigx_w + 2*(d1 + c1*b1)*b1*c2*sigw_xw + 2*(d1 + c1*b1)*b2*sigw_mw + 2*(cp + a1*b1)*b2*sigx_mw + 2*b1*c2*b2*sigxw_mw pop.cov=array(c(sigx2, sigx_w, sigx_m, sigx_xw, sigx_mw, sigx_y, 0, 0, @@ -77,15 +77,15 @@ wp.modmed.m58 <- function(a1 = 0.5, b1 = 0.75, c1 = 0.1, a2 = 0.4, b2 = 0.6, c2 sigx_m, sigw_m, sigm2, sigm_xw, sigm_mw, sigm_y, sige12, 0, sigx_xw, sigw_xw, sigm_xw, sigxw2, sigxw_mw, sigxw_y, 0, 0, sigx_mw, sigw_mw, sigm_mw, sigxw_mw, sigmw2, sigmw_y, 0, 0, - sigx_y, sigw_y, sigm_y, sigxw_y, sigmw_y, sigy2, b2*sige12, sige22, - 0, 0, sige12, 0, 0, b2*sige12, sige12, 0, + sigx_y, sigw_y, sigm_y, sigxw_y, sigmw_y, sigy2, b1*sige12, sige22, + 0, 0, sige12, 0, 0, b1*sige12, sige12, 0, 0, 0, 0, 0, 0, sige22, 0, sige22), dim=c(8, 8)) pop.cov = pop.cov[1:6, 1:6] u_xw = sigx_w - u_m = c1*u_xw - u_mw = b1*u_xw + a1*sigw2 - u_y = b2*u_m + c2*u_mw + u_m = c2*u_xw + u_mw = a1*u_xw + c1*sigw2 + u_y = b1*u_m + b2*u_mw mu = c(0, 0, u_m, u_xw, u_mw, u_y) rownames(pop.cov) = colnames(pop.cov) = c('x', 'w', 'm', 'xw', 'mw', 'y') }else{ @@ -112,29 +112,29 @@ wp.modmed.m58 <- function(a1 = 0.5, b1 = 0.75, c1 = 0.1, a2 = 0.4, b2 = 0.6, c2 boot_CI1 = (test_boot1$coefficients[2] + test_boot1$coefficients[4]*w_value) boot_CI2 = (test_boot2$coefficients[3] + test_boot2$coefficients[5]*w_value) boot_DI = test_boot2$coefficients[2] - boot_c1 = test_boot1$coefficients[4] - boot_c2 = test_boot2$coefficients[5] - return(list(boot_CI, boot_DI, boot_c1, boot_c2, boot_CI1, boot_CI2)) + boot_c2 = test_boot1$coefficients[4] + boot_b2 = test_boot2$coefficients[5] + return(list(boot_CI, boot_DI, boot_c2, boot_b2, boot_CI1, boot_CI2)) } boot_effect = lapply(1:b, bootstrap) boot_CI = matrix(0, ncol = 1, nrow = b) boot_CI1 = matrix(0, ncol = 1, nrow = b) boot_CI2 = matrix(0, ncol = 1, nrow = b) boot_DI = matrix(0, ncol = 1, nrow = b) - boot_c1 = matrix(0, ncol = 1, nrow = b) boot_c2 = matrix(0, ncol = 1, nrow = b) + boot_b2 = matrix(0, ncol = 1, nrow = b) boot_CI = t(sapply(1:b,function(i) unlist(boot_effect[[i]][1]))) boot_DI = as.matrix(sapply(1:b,function(i) unlist(boot_effect[[i]][2]))) - boot_c1 = as.matrix(sapply(1:b,function(i) unlist(boot_effect[[i]][3]))) - boot_c2 = as.matrix(sapply(1:b,function(i) unlist(boot_effect[[i]][4]))) + boot_c2 = as.matrix(sapply(1:b,function(i) unlist(boot_effect[[i]][3]))) + boot_b2 = as.matrix(sapply(1:b,function(i) unlist(boot_effect[[i]][4]))) boot_CI1 = t(sapply(1:b,function(i) unlist(boot_effect[[i]][5]))) boot_CI2 = t(sapply(1:b,function(i) unlist(boot_effect[[i]][6]))) interval_CI = matrix(0, ncol = 1, nrow = 2) interval_DI = matrix(0, ncol = 1, nrow = 2) - interval_c1 = matrix(0, ncol = 1, nrow = 2) interval_c2 = matrix(0, ncol = 1, nrow = 2) + interval_b2 = matrix(0, ncol = 1, nrow = 2) interval_CI1 = matrix(0, ncol = 1, nrow = 2) interval_CI2 = matrix(0, ncol = 1, nrow = 2) @@ -150,18 +150,18 @@ wp.modmed.m58 <- function(a1 = 0.5, b1 = 0.75, c1 = 0.1, a2 = 0.4, b2 = 0.6, c2 interval_DI[, 1] = quantile(boot_DI[, 1], probs = c(alpha / 2, 1 - alpha / 2), names = T) - interval_c1[, 1] = quantile(boot_c1[, 1], + interval_c2[, 1] = quantile(boot_c2[, 1], probs = c(alpha / 2, 1 - alpha / 2), names = T) - interval_c2[, 1] = quantile(boot_c2[, 1], + interval_b2[, 1] = quantile(boot_b2[, 1], probs = c(alpha / 2, 1 - alpha / 2), names = T) r_CI = as.numeric(!sapply(1,function(i) dplyr::between(0,interval_CI[1,i],interval_CI[2,i]))) r_DI = as.numeric(!sapply(1,function(i) dplyr::between(0,interval_DI[1,i],interval_DI[2,i]))) - r_c1 = as.numeric(!sapply(1,function(i) dplyr::between(0,interval_c1[1,i],interval_c1[2,i]))) r_c2 = as.numeric(!sapply(1,function(i) dplyr::between(0,interval_c2[1,i],interval_c2[2,i]))) - power = c(r_CI, r_DI, r_c1, r_c2) + r_b2 = as.numeric(!sapply(1,function(i) dplyr::between(0,interval_b2[1,i],interval_b2[2,i]))) + power = c(r_CI, r_DI, r_c2, r_b2) if (method == "joint"){ r_CI = as.numeric(!dplyr::between(0,interval_CI1[1,1], interval_CI1[2,1]))*as.numeric(!dplyr::between(0, interval_CI2[1,1], interval_CI2[2,1])) } @@ -169,7 +169,7 @@ wp.modmed.m58 <- function(a1 = 0.5, b1 = 0.75, c1 = 0.1, a2 = 0.4, b2 = 0.6, c2 } if (ncore > 1){ CL1 = parallel::makeCluster(ncore) - parallel::clusterExport(CL1,c('a1', 'b1', 'c1', 'c2', 'b2', 'b3', 'a2', + parallel::clusterExport(CL1,c('c1', 'a1', 'c2', 'b2', 'b1', 'cp', 'd1', 'sigx2', 'sigw2', 'sige12', 'sige22', 'sigx_w', 'n', 'nrep', 'alpha','b','nb','pop.cov', 'mu', 'method', 'w_value'),envir = environment()) @@ -198,9 +198,7 @@ wp.modmed.m58 <- function(a1 = 0.5, b1 = 0.75, c1 = 0.1, a2 = 0.4, b2 = 0.6, c2 note="power1 is the power of the conditional indirect effect of x on y through m. power2 is the power of the direct effect of x on y. power3 is the power of moderation on the path x to m. -power4 is the powe of moderation on the path m to y."), class = "webpower") +power4 is the power of moderation on the path m to y."), class = "webpower") return(power.structure) } - - diff --git a/R/model7.R b/R/model7.R index 9504625..6d787c9 100644 --- a/R/model7.R +++ b/R/model7.R @@ -4,11 +4,11 @@ #' #' power analysis of model 7 in Introduction to Mediation, Moderation, and Conditional Process Analysis #' -#' @param beta1 regression coefficient of mediator (m) on predictor (x) -#' @param beta2 regression coefficient of outcome (y) on predictor (x) -#' @param beta3 regression coefficient of outcome (y) on mediator (m) -#' @param a1 regression coefficient of mediator (m) on moderator (w) -#' @param c1 regression coefficient of mediator (m) on the product (xw) +#' @param a1 regression coefficient of mediator (m) on predictor (x) +#' @param cp regression coefficient of outcome (y) on predictor (x) +#' @param b1 regression coefficient of outcome (y) on mediator (m) +#' @param c1 regression coefficient of mediator (m) on moderator (w) +#' @param c2 regression coefficient of mediator (m) on the product (xw) #' @param sigx2 variance of predictor (x) #' @param sigw2 variance of moderator (w) #' @param sige12 variance of error in the first regression equation @@ -29,14 +29,14 @@ #' @export #' @examples #' # usage of wp.modmed.m7 -#' test = wp.modmed.m7(beta1 = 0.39, beta2 = 0.2, beta3 = 0.3, a1 = 0.39, -#' c1 = 0.2, sigx2 = 1, sigw2 = 1, sige12 = 1, +#' test = wp.modmed.m7(a1 = 0.39, cp = 0.2, b1 = 0.3, c1 = 0.39, +#' c2 = 0.2, sigx2 = 1, sigw2 = 1, sige12 = 1, #' sige22 = 1, sigx_w = 0.5, n = 50, nrep = 100, #' alpha = 0.05, b = 1000, ncore = 1) #' print(test) -wp.modmed.m7 <- function (beta1 = 0.39, beta2 = 0.2, beta3 = 0.3, a1 = 0.39, - c1 = 0.2, sigx2 = 1, sigw2 = 1, sige12 = 1, - sige22 = 1, sigx_w = 0.5, n = 500, nrep = 1000, +wp.modmed.m7 <- function (a1 = 0.39, cp = 0.2, b1 = 0.3, c1 = 0.39, + c2 = 0.2, sigx2 = 1, sigw2 = 1, sige12 = 1, + sige22 = 1, sigx_w = 0.5, n = 100, nrep = 100, alpha = 0.05, b = 1000, nb = n, w_value = 0, method="value", ncore = 1, pop.cov = NULL, mu = NULL, varnames = c('y', 'x', 'w', 'm', 'xw')) @@ -44,30 +44,30 @@ wp.modmed.m7 <- function (beta1 = 0.39, beta2 = 0.2, beta3 = 0.3, a1 = 0.39, if (is.null(pop.cov) || is.null(mu)){ sigxw2 = sigx2*sigw2 + sigx_w^2 - sigm_x = beta1*sigx2 + a1*sigx_w - sigm_w = beta1*sigx_w + a1*sigw2 - sigm_xw = c1*sigxw2 - sigm2 = beta1^2*sigx2 + a1^2*sigw2 + c1^2*sigxw2 + sige12 + 2*beta1*a1*sigx_w - sigy_x = (beta2 + beta3*beta1)*sigx2 + beta3*a1*sigx_w - sigy_w = (beta2 + beta3*beta1)*sigx_w + beta3*a1*sigw2 - sigy_xw = beta3*c1*sigxw2 - sigy_m = beta2*sigm_x + beta3*sigm2 - sigy2 = (beta2 + beta3*beta1)^2*sigx2 + beta3^2*a1^2*sigw2 + beta3^2*c1^2*sigxw2 + beta3^2*sige12 + sige22 + 2*beta3*a1*(beta2 + beta3*beta1)*sigx_w - - pop.cov = array(c(sigy2, sigy_x, sigy_w, sigy_m, sigy_xw, beta3*sige12,sige22, + sigm_x = a1*sigx2 + c1*sigx_w + sigm_w = a1*sigx_w + c1*sigw2 + sigm_xw = c2*sigxw2 + sigm2 = a1^2*sigx2 + c1^2*sigw2 + c2^2*sigxw2 + sige12 + 2*a1*c1*sigx_w + sigy_x = (cp + b1*a1)*sigx2 + b1*c1*sigx_w + sigy_w = (cp + b1*a1)*sigx_w + b1*c1*sigw2 + sigy_xw = b1*c2*sigxw2 + sigy_m = cp*sigm_x + b1*sigm2 + sigy2 = (cp + b1*a1)^2*sigx2 + b1^2*c1^2*sigw2 + b1^2*c2^2*sigxw2 + b1^2*sige12 + sige22 + 2*b1*c1*(cp + b1*a1)*sigx_w + + pop.cov = array(c(sigy2, sigy_x, sigy_w, sigy_m, sigy_xw, b1*sige12,sige22, sigy_x, sigx2, sigx_w, sigm_x, 0, 0, 0, sigy_w, sigx_w, sigw2, sigm_w, 0, 0, 0, sigy_m, sigm_x, sigm_w, sigm2, sigm_xw, sige12, 0, sigy_xw, 0, 0, sigm_xw, sigxw2, 0, 0, - beta3*sige12, 0, 0, sige12, 0, sige12, 0, + b1*sige12, 0, 0, sige12, 0, sige12, 0, sige22, 0, 0, 0, 0, 0, sige22), dim = c(7, 7)) pop.cov = pop.cov[1:5, 1:5] colnames(pop.cov) = rownames(pop.cov)=c('y','x','w','m','xw') # means u_xw = sigx_w - u_m = c1*u_xw - u_y = beta3*u_m + u_m = c2*u_xw + u_y = b1*u_m mu = c(u_y, 0, 0, u_m, u_xw) }else{ pop.cov = pop.cov @@ -141,7 +141,7 @@ wp.modmed.m7 <- function (beta1 = 0.39, beta2 = 0.2, beta3 = 0.3, a1 = 0.39, } if (ncore > 1){ CL1 = parallel::makeCluster(ncore) - parallel::clusterExport(CL1, c('beta1', 'beta2', 'beta3', 'a1', 'c1', + parallel::clusterExport(CL1, c('a1', 'cp', 'b1', 'c1', 'c2', 'sigx2', 'sigw2', 'sige12', 'sige22', 'sigx_w', 'n', 'nrep', 'alpha', 'b', 'nb', 'pop.cov', 'method', 'mu', 'w_value'), envir = environment()) @@ -175,4 +175,3 @@ power3 is the power of moderation on the path x to m."), class = "webpower") - diff --git a/R/model8.R b/R/model8.R index a02d4e5..5a3ca1a 100644 --- a/R/model8.R +++ b/R/model8.R @@ -4,13 +4,13 @@ #' #' power analysis of model 8 in Introduction to Mediation, Moderation, and Conditional Process Analysis #' -#' @param beta1 regression coefficient of mediator (m) on predictor (x) -#' @param beta2 regression coefficient of outcome (y) on predictor (m) -#' @param beta3 regression coefficient of outcome (y) on mediator (x) -#' @param a1 regression coefficient of mediator (m) on moderator (w) -#' @param a2 regression coefficient of mediator (y) on moderator (w) -#' @param c1 regression coefficient of mediator (m) on the product (xw) -#' @param c2 regression coefficient of mediator (y) on the product (xw) +#' @param a1 regression coefficient of mediator (m) on predictor (x) +#' @param cp regression coefficient of outcome (y) on predictor (m) +#' @param b1 regression coefficient of outcome (y) on mediator (x) +#' @param c1 regression coefficient of mediator (m) on moderator (w) +#' @param d1 regression coefficient of mediator (y) on moderator (w) +#' @param c2 regression coefficient of mediator (m) on the product (xw) +#' @param d2 regression coefficient of mediator (y) on the product (xw) #' @param sigx2 variance of predictor (x) #' @param sigw2 variance of moderator (w) #' @param sige12 variance of error in the first regression equation @@ -31,42 +31,42 @@ #' @export #' @examples #' # usage of wp.modmed.m8 -#' test = wp.modmed.m8(beta1 = 0.2, beta2 = 0.2, beta3 = 0.2, -#' a1 = 0.2, a2=0.2, c1 = 0.2, c2 = 0.2, +#' test = wp.modmed.m8(a1 = 0.2, cp = 0.2, b1 = 0.2, +#' c1 = 0.2, d1=0.2, c2 = 0.2, d2 = 0.2, #' sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.5, #' n = 50, nrep = 100, alpha = 0.05, b = 1000, ncore = 1) #' print(test) -wp.modmed.m8 <- function (beta1 = 0.2, beta2 = 0.75, beta3 = 0.8, - a1 = 0.6, a2 = 0.3, c1 = 0.8, c2 = 0.79, +wp.modmed.m8 <- function (a1 = 0.2, cp = 0.75, b1 = 0.8, + c1 = 0.6, d1 = 0.3, c2 = 0.8, d2 = 0.79, sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.5, - n = 100, nrep = 1000, alpha = 0.05, b = 1000, nb = n, + n = 100, nrep = 100, alpha = 0.05, b = 1000, nb = n, w_value = 0, method = "value", ncore = 1, pop.cov = NULL, mu = NULL, varnames = c('y','x','w','m','xw')) { if (is.null(pop.cov) || is.null(mu)){ sigxw2 = sigx2*sigw2 + sigx_w^2 - sigm_xw = c1*sigxw2 - sigy_xw = (beta2*c1 + c2)*sigxw2 - sigm_x = a1*sigx_w + beta1*sigx2 - sigm_w = beta1*sigx_w+a1*sigw2 - sigy_x = (a2 + a1*beta2)*sigx_w + (beta3 + beta1*beta2)*sigx2 - sigy_w = (a2+a1*beta2)*sigw2 + (beta3 + beta1*beta2)*sigx_w - sigy2 = (a2 + a1*beta2)^2*sigw2 + (beta3 + beta1*beta2)^2*sigx2 + beta2^2*sige12 + - (beta2*c1 + c2)^2*sigxw2 + sige22 + 2*(a2 + a1*beta2)*(beta3 + beta1*beta2)*sigx_w - sigm2 = beta1^2*sigx2 + a1^2*sigw2 + c1^2*sigxw2 + sige12 + 2*beta1*a1*sigx_w - sigy_m = a2*sigm_w + beta3*sigm_x + beta2*sigm2 + c2*sigm_xw - pop.cov = array(c(sigy2, sigy_x, sigy_w, sigy_m, sigy_xw, beta2*sige12, sige22, + sigm_xw = c2*sigxw2 + sigy_xw = (cp*c2 + d2)*sigxw2 + sigm_x = c1*sigx_w + a1*sigx2 + sigm_w = a1*sigx_w+c1*sigw2 + sigy_x = (d1 + c1*cp)*sigx_w + (b1 + a1*cp)*sigx2 + sigy_w = (d1+c1*cp)*sigw2 + (b1 + a1*cp)*sigx_w + sigy2 = (d1 + c1*cp)^2*sigw2 + (b1 + a1*cp)^2*sigx2 + cp^2*sige12 + + (cp*c2 + d2)^2*sigxw2 + sige22 + 2*(d1 + c1*cp)*(b1 + a1*cp)*sigx_w + sigm2 = a1^2*sigx2 + c1^2*sigw2 + c2^2*sigxw2 + sige12 + 2*a1*c1*sigx_w + sigy_m = d1*sigm_w + b1*sigm_x + cp*sigm2 + d2*sigm_xw + pop.cov = array(c(sigy2, sigy_x, sigy_w, sigy_m, sigy_xw, cp*sige12, sige22, sigy_x, sigx2, sigx_w, sigm_x, 0, 0, 0, sigy_w, sigx_w, sigw2, sigm_w, 0, 0, 0, sigy_m, sigm_x, sigm_w, sigm2, sigm_xw, sige12, 0, sigy_xw, 0, 0, sigm_xw, sigxw2, 0, 0, - beta2*sige12, 0, 0, sige12, 0, sige12, 0, + cp*sige12, 0, 0, sige12, 0, sige12, 0, sige22, 0, 0, 0, 0, 0, sige22), dim = c(7, 7)) pop.cov = pop.cov[1:5,1:5] u_xw = sigx_w - u_m = c1*u_xw - u_y = beta2*u_m + c2*u_xw + u_m = c2*u_xw + u_y = cp*u_m + d2*u_xw colnames(pop.cov) = rownames(pop.cov) = c('y','x','w','m','xw') mu = c(u_y, 0, 0, u_m, u_xw) }else{ @@ -89,32 +89,32 @@ wp.modmed.m8 <- function (beta1 = 0.2, beta2 = 0.75, beta3 = 0.8, test_boot2 = lm(y ~ x + m + w + xw,data = boot_data) boot_CI = (test_boot1$coefficients[2] + test_boot1$coefficients[4]*w_value)*test_boot2$coefficients[3] boot_CD = test_boot2$coefficients[2] + test_boot2$coefficients[5]*w_value - boot_c1 = as.numeric(test_boot1$coefficients[4]) - boot_c2 = as.numeric(test_boot2$coefficients[5]) + boot_c2 = as.numeric(test_boot1$coefficients[4]) + boot_d2 = as.numeric(test_boot2$coefficients[5]) boot_CI1 = (test_boot1$coefficients[2] + test_boot1$coefficients[4]*w_value) boot_CI2 = test_boot2$coefficients[3] - return(list(boot_CI, boot_CD, boot_c1, boot_c2, boot_CI1, boot_CI2)) + return(list(boot_CI, boot_CD, boot_c2, boot_d2, boot_CI1, boot_CI2)) } boot_effect = lapply(1:b, bootstrap) boot_CI = matrix(0, ncol = 1, nrow = b) boot_CD = matrix(0, ncol = 1, nrow = b) - boot_c1 = matrix(0, ncol = 1, nrow = b) boot_c2 = matrix(0, ncol = 1, nrow = b) + boot_d2 = matrix(0, ncol = 1, nrow = b) boot_CI1 = matrix(0, ncol = 1, nrow = b) boot_CI2 = matrix(0, ncol = 1, nrow = b) boot_CI = t(sapply(1:b, function(i) unlist(boot_effect[[i]][1]))) boot_CD = t(sapply(1:b, function(i) unlist(boot_effect[[i]][2]))) - boot_c1 = t(sapply(1:b, function(i) unlist(boot_effect[[i]][3]))) - boot_c2 = t(sapply(1:b, function(i) unlist(boot_effect[[i]][4]))) + boot_c2 = t(sapply(1:b, function(i) unlist(boot_effect[[i]][3]))) + boot_d2 = t(sapply(1:b, function(i) unlist(boot_effect[[i]][4]))) boot_CI1 = t(sapply(1:b, function(i) unlist(boot_effect[[i]][5]))) boot_CI2 = t(sapply(1:b, function(i) unlist(boot_effect[[i]][6]))) interval_CI = matrix(0, ncol = 1, nrow = 2) interval_CD = matrix(0, ncol = 1, nrow = 2) - interval_c1 = matrix(0, ncol = 1, nrow = 2) interval_c2 = matrix(0, ncol = 1, nrow = 2) + interval_d2 = matrix(0, ncol = 1, nrow = 2) interval_CI1 = matrix(0, ncol = 1, nrow = 2) interval_CI2 = matrix(0, ncol = 1, nrow = 2) @@ -125,10 +125,10 @@ wp.modmed.m8 <- function (beta1 = 0.2, beta2 = 0.75, beta3 = 0.8, interval_CD[, 1] = quantile(boot_CD, probs = c(alpha / 2, 1 - alpha / 2), names = T) - interval_c1[, 1] = quantile(boot_c1, + interval_c2[, 1] = quantile(boot_c2, probs = c(alpha / 2, 1 - alpha / 2), names = T) - interval_c2[, 1] = quantile(boot_c2, + interval_d2[, 1] = quantile(boot_d2, probs = c(alpha / 2, 1 - alpha / 2), names = T) interval_CI1[, 1] = quantile(boot_CI1, @@ -141,17 +141,17 @@ wp.modmed.m8 <- function (beta1 = 0.2, beta2 = 0.75, beta3 = 0.8, r_CI = as.numeric(!sapply(1,function(i) dplyr::between(0,interval_CI[1,i], interval_CI[2,i]))) r_DI = as.numeric(!sapply(1,function(i) dplyr::between(0,interval_CD[1,i], interval_CD[2,i]))) - r_c1 = as.numeric(!sapply(1, function(i) dplyr::between(0,interval_c1[1,i], interval_c1[2,i]))) r_c2 = as.numeric(!sapply(1, function(i) dplyr::between(0,interval_c2[1,i], interval_c2[2,i]))) + r_d2 = as.numeric(!sapply(1, function(i) dplyr::between(0,interval_d2[1,i], interval_d2[2,i]))) if (method == "joint"){ r_CI = as.numeric(!dplyr::between(0, interval_CI1[1,1], interval_CI1[2,1]))*as.numeric(!dplyr::between(0, interval_CI2[1,1], interval_CI2[2,1])) } - power=c(r_CI, r_DI, r_c1, r_c2) + power=c(r_CI, r_DI, r_c2, r_d2) return(power) } if (ncore > 1){ CL1 = parallel::makeCluster(ncore) - parallel::clusterExport(CL1, c('beta1', 'beta2', 'beta3', 'a1', 'a2', 'c1','c2', + parallel::clusterExport(CL1, c('a1', 'cp', 'b1', 'c1', 'd1', 'c2','d2', 'sigx2', 'sigw2', 'sige12', 'sige22', 'sigx_w', 'n', 'nrep', 'alpha', 'b', 'nb', 'pop.cov', 'mu', 'w_value', 'method'), envir = environment()) @@ -182,3 +182,5 @@ power4 is the power of moderation on the path x to y."), class = "webpower") return(power.structure) } + + diff --git a/man/wp.modmed.m14.Rd b/man/wp.modmed.m14.Rd index 8df034a..c18e70d 100644 --- a/man/wp.modmed.m14.Rd +++ b/man/wp.modmed.m14.Rd @@ -6,17 +6,17 @@ \usage{ wp.modmed.m14( a1 = 0.2, - cc = 0.2, + cp = 0.2, b1 = 0.5, - c1 = 0.5, - c2 = 0.2, + d1 = 0.5, + b2 = 0.2, sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.5, n = 100, - nrep = 1000, + nrep = 100, alpha = 0.05, b = 1000, nb = n, @@ -31,13 +31,13 @@ wp.modmed.m14( \arguments{ \item{a1}{regression coefficient of mediator (m) on predictor (x)} -\item{cc}{regression coefficient of outcome (y) on predictor (x)} +\item{cp}{regression coefficient of outcome (y) on predictor (x)} \item{b1}{regression coefficient of outcome (y) on mediator (m)} -\item{c1}{regression coefficient of outcome (y) on moderator (w)} +\item{d1}{regression coefficient of outcome (y) on moderator (w)} -\item{c2}{regression coefficient of outcome (y) on the product (mw)} +\item{b2}{regression coefficient of outcome (y) on the product (mw)} \item{sigx2}{variance of predictor (x)} @@ -79,9 +79,9 @@ power analysis of model 14 in Introduction to Mediation, Moderation, and Conditi } \examples{ \donttest{ -test = wp.modmed.m14(a1 = 0.2, cc = 0.2, b1 = 0.5, c1 = 0.5, c2 = 0.2, sigx2 = 1, - sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.5, n = 50, - nrep = 100, alpha = 0.05, b = 1000, ncore = 1) +test = wp.modmed.m14(a1 = 0.2, cp = 0.2, b1 = 0.5, d1 = 0.5, b2 = 0.2, sigx2 = 1, + sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.5, n = 50, + nrep = 100, alpha = 0.05, b = 1000, ncore = 1) print(test) } } diff --git a/man/wp.modmed.m15.Rd b/man/wp.modmed.m15.Rd index 8326882..130df5d 100644 --- a/man/wp.modmed.m15.Rd +++ b/man/wp.modmed.m15.Rd @@ -6,18 +6,18 @@ \usage{ wp.modmed.m15( a1 = 0.6, - c = 0.2, + cp = 0.2, b1 = 0.3, b2 = 0.5, - c1 = 0.2, - c2 = 0.1, + d1 = 0.2, + d2 = 0.1, sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.4, - n = 500, - nrep = 1000, + n = 100, + nrep = 100, alpha = 0.05, b = 1000, nb = n, @@ -32,15 +32,15 @@ wp.modmed.m15( \arguments{ \item{a1}{regression coefficient of mediator (m) on predictor (x)} -\item{c}{regression coefficient of outcome (y) on predictor (x)} +\item{cp}{regression coefficient of outcome (y) on predictor (x)} \item{b1}{regression coefficient of outcome (y) on mediator (m)} \item{b2}{regression coefficient of outcome (y) on the product (mw)} -\item{c1}{regression coefficient of outcome (y) on moderator (w)} +\item{d1}{regression coefficient of outcome (y) on moderator (w)} -\item{c2}{regression coefficient of outcome (y) on the product (xw)} +\item{d2}{regression coefficient of outcome (y) on the product (xw)} \item{sigx2}{variance of predictor (x)} @@ -80,12 +80,11 @@ power of indirect effect, direct effect, and moderation \description{ power analysis of model 15 in Introduction to Mediation, Moderation, and Conditional Process Analysis } - \examples{ \donttest{ -test = wp.modmed.m15(a1 = 0.6, c = 0.2, b1 = 0.3, b2 = 0.2, c1 = 0.2, c2 = 0.1, - sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.4, - n = 50, nrep = 100, alpha = 0.05, b = 1000, ncore = 1) +test = wp.modmed.m15(a1 = 0.6, cp = 0.2, b1 = 0.3, b2 = 0.2, d1 = 0.2, d2 = 0.1, + sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.4, + n = 50, nrep = 100, alpha = 0.05, b = 1000, ncore = 1) print(test) } } diff --git a/man/wp.modmed.m58.Rd b/man/wp.modmed.m58.Rd index 6728062..ad83319 100644 --- a/man/wp.modmed.m58.Rd +++ b/man/wp.modmed.m58.Rd @@ -5,20 +5,20 @@ \title{model58} \usage{ wp.modmed.m58( - a1 = 0.5, - b1 = 0.75, - c1 = 0.1, - a2 = 0.4, - b2 = 0.6, + c1 = 0.5, + a1 = 0.75, c2 = 0.1, - b3 = 0.5, + d1 = 0.4, + b1 = 0.6, + b2 = 0.1, + cp = 0.5, sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.4, - n = 500, - nrep = 1000, + n = 100, + nrep = 100, alpha = 0.05, b = 1000, nb = n, @@ -27,23 +27,23 @@ wp.modmed.m58( ncore = 1, pop.cov = NULL, mu = NULL, - varnames = c('x', 'w', 'm', 'xw', 'mw', 'y') + varnames = c("x", "w", "m", "xw", "mw", "y") ) } \arguments{ -\item{a1}{regression coefficient of outcome (m) on moderator (w)} +\item{c1}{regression coefficient of outcome (m) on moderator (w)} -\item{b1}{regression coefficient of mediator (m) on predictor (x)} +\item{a1}{regression coefficient of mediator (m) on predictor (x)} -\item{c1}{regression coefficient of outcome (m) on the product (xw)} +\item{c2}{regression coefficient of outcome (m) on the product (xw)} -\item{a2}{regression coefficient of outcome (y) on moderator (w)} +\item{d1}{regression coefficient of outcome (y) on moderator (w)} -\item{b2}{regression coefficient of outcome (y) on mediator (m)} +\item{b1}{regression coefficient of outcome (y) on mediator (m)} -\item{c2}{regression coefficient of outcome (y) on the product (mw)} +\item{b2}{regression coefficient of outcome (y) on the product (mw)} -\item{b3}{regression coefficient of outcome (y) on predictor (x)} +\item{cp}{regression coefficient of outcome (y) on predictor (x)} \item{sigx2}{variance of predictor (x)} @@ -83,13 +83,12 @@ power of indirect effect, direct effect, and moderation \description{ power analysis of model 58 in Introduction to Mediation, Moderation, and Conditional Process Analysis } - \examples{ \donttest{ -test = wp.modmed.m58(a1 = 0.2, b1 = 0.2, c1 = 0.1, c2 = 0.1, - b2 = 0.2, b3 = 0.2, a2 = 0.2, - sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.5, - n = 50, nrep = 100, alpha = 0.05, b = 1000, ncore = 1) +test = wp.modmed.m58(c1 = 0.2, a1 = 0.2, c2 = 0.1, b2 = 0.1, + b1 = 0.2, cp = 0.2, d1 = 0.2, + sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.5, + n = 50, nrep = 100, alpha = 0.05, b = 1000, ncore = 1) print(test) } } diff --git a/man/wp.modmed.m7.Rd b/man/wp.modmed.m7.Rd index 476f59e..a43568d 100644 --- a/man/wp.modmed.m7.Rd +++ b/man/wp.modmed.m7.Rd @@ -5,18 +5,18 @@ \title{model7} \usage{ wp.modmed.m7( - beta1 = 0.39, - beta2 = 0.2, - beta3 = 0.3, a1 = 0.39, - c1 = 0.2, + cp = 0.2, + b1 = 0.3, + c1 = 0.39, + c2 = 0.2, sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.5, - n = 500, - nrep = 1000, + n = 100, + nrep = 100, alpha = 0.05, b = 1000, nb = n, @@ -29,15 +29,15 @@ wp.modmed.m7( ) } \arguments{ -\item{beta1}{regression coefficient of mediator (m) on predictor (x)} +\item{a1}{regression coefficient of mediator (m) on predictor (x)} -\item{beta2}{regression coefficient of outcome (y) on predictor (x)} +\item{cp}{regression coefficient of outcome (y) on predictor (x)} -\item{beta3}{regression coefficient of outcome (y) on mediator (m)} +\item{b1}{regression coefficient of outcome (y) on mediator (m)} -\item{a1}{regression coefficient of mediator (m) on moderator (w)} +\item{c1}{regression coefficient of mediator (m) on moderator (w)} -\item{c1}{regression coefficient of mediator (m) on the product (xw)} +\item{c2}{regression coefficient of mediator (m) on the product (xw)} \item{sigx2}{variance of predictor (x)} @@ -80,10 +80,10 @@ power analysis of model 7 in Introduction to Mediation, Moderation, and Conditio \examples{ \donttest{ # usage of wp.modmed.m7 -test = wp.modmed.m7(beta1 = 0.39, beta2 = 0.2, beta3 = 0.3, a1 = 0.39, - c1 = 0.2, sigx2 = 1, sigw2 = 1, sige12 = 1, - sige22 = 1, sigx_w = 0.5, n = 50, nrep = 100, - alpha = 0.05, b = 1000, ncore = 1) +test = wp.modmed.m7(a1 = 0.39, cp = 0.2, b1 = 0.3, c1 = 0.39, + c2 = 0.2, sigx2 = 1, sigw2 = 1, sige12 = 1, + sige22 = 1, sigx_w = 0.5, n = 50, nrep = 100, + alpha = 0.05, b = 1000, ncore = 1) print(test) } } diff --git a/man/wp.modmed.m8.Rd b/man/wp.modmed.m8.Rd index 2b34082..1f2b2a1 100644 --- a/man/wp.modmed.m8.Rd +++ b/man/wp.modmed.m8.Rd @@ -5,20 +5,20 @@ \title{model8} \usage{ wp.modmed.m8( - beta1 = 0.2, - beta2 = 0.75, - beta3 = 0.8, - a1 = 0.6, - a2 = 0.3, - c1 = 0.8, - c2 = 0.79, + a1 = 0.2, + cp = 0.75, + b1 = 0.8, + c1 = 0.6, + d1 = 0.3, + c2 = 0.8, + d2 = 0.79, sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.5, n = 100, - nrep = 1000, + nrep = 100, alpha = 0.05, b = 1000, nb = n, @@ -31,19 +31,19 @@ wp.modmed.m8( ) } \arguments{ -\item{beta1}{regression coefficient of mediator (m) on predictor (x)} +\item{a1}{regression coefficient of mediator (m) on predictor (x)} -\item{beta2}{regression coefficient of outcome (y) on predictor (m)} +\item{cp}{regression coefficient of outcome (y) on predictor (m)} -\item{beta3}{regression coefficient of outcome (y) on mediator (x)} +\item{b1}{regression coefficient of outcome (y) on mediator (x)} -\item{a1}{regression coefficient of mediator (m) on moderator (w)} +\item{c1}{regression coefficient of mediator (m) on moderator (w)} -\item{a2}{regression coefficient of mediator (y) on moderator (w)} +\item{d1}{regression coefficient of mediator (y) on moderator (w)} -\item{c1}{regression coefficient of mediator (m) on the product (xw)} +\item{c2}{regression coefficient of mediator (m) on the product (xw)} -\item{c2}{regression coefficient of mediator (y) on the product (xw)} +\item{d2}{regression coefficient of mediator (y) on the product (xw)} \item{sigx2}{variance of predictor (x)} @@ -86,10 +86,10 @@ power analysis of model 8 in Introduction to Mediation, Moderation, and Conditio \examples{ \donttest{ # usage of wp.modmed.m8 -test = wp.modmed.m8(beta1 = 0.2, beta2 = 0.2, beta3 = 0.2, - a1 = 0.2, a2=0.2, c1 = 0.2, c2 = 0.2, - sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.5, - n = 50, nrep = 100, alpha = 0.05, b = 1000, ncore = 1) +test = wp.modmed.m8(a1 = 0.2, cp = 0.2, b1 = 0.2, + c1 = 0.2, d1=0.2, c2 = 0.2, d2 = 0.2, + sigx2 = 1, sigw2 = 1, sige12 = 1, sige22 = 1, sigx_w = 0.5, + n = 50, nrep = 100, alpha = 0.05, b = 1000, ncore = 1) print(test) } }