@@ -683,7 +688,7 @@ one-sided t-test
B > A : 0.9709984
-The one-sided p-value corresponds to half the two-sided p-value, if and only if the reference group is the one supposed to be smaller.
+The one-sided p-value corresponds to half the two-sided p-value, if and only if the reference group is the one supposed to be smaller. The reference group is a term in R, and is (unless specified) the groupname that comes first in the alphabet. In this case, the reference group is groupA. So the one-sided p-value for groupB > groupA is 0.029 which also equals 0.058/2.
diff --git a/search.json b/search.json
index 2f36338..b009d4e 100644
--- a/search.json
+++ b/search.json
@@ -53,7 +53,7 @@
"href": "posts/lmm-one-sided-t-tests/index.html",
"title": "One-sided tests for Linear Mixed Models",
"section": "",
- "text": "Simulate (and show) some Data\nfit linear mixed model\nanova and two-sided t-test\none-sided t-test\n\n\nsimulate data\nSimulate data for 2 groups, 3 repetition, 5 cages per repetition, 2 mice of each group per cage\n\n\nCode\ngroup_effect <- 1\n\nset.seed(321)\nD <- data.frame(\n repetition = rep(as.factor(1:3), each=20),\n cage = factor(rep(1:15, each=4)),\n group = rep(c(\"A\", \"B\"), each=2, times=15)\n)\n\n# cage effect\ncage_effect <- data.frame(\n cage = unique(D$cage),\n cage_effect = rnorm(length(unique(D$cage)), sd=0.4) |> round(2)\n)\n\nD <- merge(cage_effect, D, by=\"cage\", sort=FALSE)\nD$AUC <- rnorm(nrow(D), mean=10, sd=2) + D$cage_effect + \n ifelse(D$group == \"B\", group_effect, 0)\nD$cage_effect <- NULL\n\nknitr::kable(D)\n\n\n\n\n\ncage\nrepetition\ngroup\nAUC\n\n\n\n\n1\n1\nA\n9.070657\n\n\n1\n1\nA\n11.592138\n\n\n1\n1\nB\n12.520665\n\n\n1\n1\nB\n12.835169\n\n\n2\n1\nA\n10.612712\n\n\n2\n1\nA\n11.554511\n\n\n2\n1\nB\n10.505877\n\n\n2\n1\nB\n12.696671\n\n\n3\n1\nA\n7.745522\n\n\n3\n1\nA\n8.373969\n\n\n3\n1\nB\n11.080001\n\n\n3\n1\nB\n6.228138\n\n\n4\n1\nA\n10.785032\n\n\n4\n1\nA\n7.709345\n\n\n4\n1\nB\n10.000631\n\n\n4\n1\nB\n7.889176\n\n\n5\n1\nA\n10.781430\n\n\n5\n1\nA\n11.218396\n\n\n5\n1\nB\n13.411695\n\n\n5\n1\nB\n10.640873\n\n\n6\n2\nA\n10.339079\n\n\n6\n2\nA\n5.657473\n\n\n6\n2\nB\n14.381581\n\n\n6\n2\nB\n10.791359\n\n\n7\n2\nA\n10.346535\n\n\n7\n2\nA\n7.216693\n\n\n7\n2\nB\n11.446316\n\n\n7\n2\nB\n11.790792\n\n\n8\n2\nA\n10.578174\n\n\n8\n2\nA\n11.689476\n\n\n8\n2\nB\n11.772819\n\n\n8\n2\nB\n11.607435\n\n\n9\n2\nA\n12.047757\n\n\n9\n2\nA\n12.412829\n\n\n9\n2\nB\n9.171424\n\n\n9\n2\nB\n9.320691\n\n\n10\n2\nA\n8.001816\n\n\n10\n2\nA\n9.281320\n\n\n10\n2\nB\n11.421702\n\n\n10\n2\nB\n9.449261\n\n\n11\n3\nA\n8.122167\n\n\n11\n3\nA\n10.822403\n\n\n11\n3\nB\n11.916796\n\n\n11\n3\nB\n14.680328\n\n\n12\n3\nA\n11.015324\n\n\n12\n3\nA\n13.514035\n\n\n12\n3\nB\n11.700311\n\n\n12\n3\nB\n14.086357\n\n\n13\n3\nA\n8.083916\n\n\n13\n3\nA\n12.421829\n\n\n13\n3\nB\n6.086836\n\n\n13\n3\nB\n10.293708\n\n\n14\n3\nA\n10.653113\n\n\n14\n3\nA\n12.397645\n\n\n14\n3\nB\n9.655439\n\n\n14\n3\nB\n9.994885\n\n\n15\n3\nA\n8.795892\n\n\n15\n3\nA\n9.361011\n\n\n15\n3\nB\n11.279876\n\n\n15\n3\nB\n11.528820\n\n\n\n\n\n\n\nfit linear mixed model\n\nlibrary(lmerTest)\nD$group <- factor(D$group)\nfit <- lmer(AUC ~ group + repetition + (1|cage), data=D)\n\n\n\nanova and two-sided t-test\n\nanova(fit)\n\nType III Analysis of Variance Table with Satterthwaite's method\n Sum Sq Mean Sq NumDF DenDF F value Pr(>F) \ngroup 13.0512 13.0512 1 44 3.7887 0.0580 .\nrepetition 1.4311 0.7156 2 12 0.2077 0.8153 \n---\nSignif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\nsummary(fit) # look at the p-value of groupB\n\nLinear mixed model fit by REML. t-tests use Satterthwaite's method [\nlmerModLmerTest]\nFormula: AUC ~ group + repetition + (1 | cage)\n Data: D\n\nREML criterion at convergence: 246.2\n\nScaled residuals: \n Min 1Q Median 3Q Max \n-2.4500 -0.6722 0.1466 0.6608 1.9061 \n\nRandom effects:\n Groups Name Variance Std.Dev.\n cage (Intercept) 0.5942 0.7709 \n Residual 3.4448 1.8560 \nNumber of obs: 60, groups: cage, 15\n\nFixed effects:\n Estimate Std. Error df t value Pr(>|t|) \n(Intercept) 9.8962 0.5903 17.0200 16.764 5.14e-12 ***\ngroupB 0.9328 0.4792 44.0000 1.946 0.058 . \nrepetition2 0.0736 0.7630 12.0000 0.096 0.925 \nrepetition3 0.4579 0.7630 12.0000 0.600 0.560 \n---\nSignif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\nCorrelation of Fixed Effects:\n (Intr) groupB rpttn2\ngroupB -0.406 \nrepetition2 -0.646 0.000 \nrepetition3 -0.646 0.000 0.500\n\n\nSo groupB is 0.93 bigger than groupA. The p-value is 0.058.\n\n\none-sided t-test\nsee this stackoverflow post\n\nCOEFS <- coef(summary(fit))\n\n# put here your names for the groups\ngroup_A_name <- levels(D$group)[1]\ngroup_B_name <- levels(D$group)[2]\n\nt_val <- COEFS[2, \"t value\"]\ndf <- COEFS[2, \"df\"]\n\n# PVALUE: one-sided t-test for groupB > groupA\np_val <- pt(t_val, df, lower.tail=FALSE)\n\ncat(\"p-value for one-sided t-test for the alternative hypothethis:\\n\", \n levels(D$group)[2], \">\", levels(D$group)[1], \":\", p_val, \"\\n\")\n\np-value for one-sided t-test for the alternative hypothethis:\n B > A : 0.02900157 \n\n# PVALUE: one-sided t-test for groupB < groupA\np_val <- pt(t_val, df, lower.tail=TRUE)\ncat(\"p-value for one-sided t-test for the alternative hypothethis:\\n\", \n levels(D$group)[2], \">\", levels(D$group)[1], \":\", p_val, \"\\n\")\n\np-value for one-sided t-test for the alternative hypothethis:\n B > A : 0.9709984 \n\n\nThe one-sided p-value corresponds to half the two-sided p-value, if and only if the reference group is the one supposed to be smaller."
+ "text": "Simulate (and show) some Data\nfit linear mixed model\nanova and two-sided t-test\none-sided t-test\n\n\nsimulate data\nSimulate data for 2 groups, 3 repetition, 5 cages per repetition, 2 mice of each group per cage\n\n\nCode\ngroup_effect <- 1\n\nset.seed(321)\nD <- data.frame(\n repetition = rep(as.factor(1:3), each=20),\n cage = factor(rep(1:15, each=4)),\n group = rep(c(\"A\", \"B\"), each=2, times=15)\n)\n\n# cage effect\ncage_effect <- data.frame(\n cage = unique(D$cage),\n cage_effect = rnorm(length(unique(D$cage)), sd=0.4) |> round(2)\n)\n\nD <- merge(cage_effect, D, by=\"cage\", sort=FALSE)\nD$AUC <- rnorm(nrow(D), mean=10, sd=2) + D$cage_effect + \n ifelse(D$group == \"B\", group_effect, 0)\nD$cage_effect <- NULL\n\nknitr::kable(D)\n\n\n\n\n\ncage\nrepetition\ngroup\nAUC\n\n\n\n\n1\n1\nA\n9.070657\n\n\n1\n1\nA\n11.592138\n\n\n1\n1\nB\n12.520665\n\n\n1\n1\nB\n12.835169\n\n\n2\n1\nA\n10.612712\n\n\n2\n1\nA\n11.554511\n\n\n2\n1\nB\n10.505877\n\n\n2\n1\nB\n12.696671\n\n\n3\n1\nA\n7.745522\n\n\n3\n1\nA\n8.373969\n\n\n3\n1\nB\n11.080001\n\n\n3\n1\nB\n6.228138\n\n\n4\n1\nA\n10.785032\n\n\n4\n1\nA\n7.709345\n\n\n4\n1\nB\n10.000631\n\n\n4\n1\nB\n7.889176\n\n\n5\n1\nA\n10.781430\n\n\n5\n1\nA\n11.218396\n\n\n5\n1\nB\n13.411695\n\n\n5\n1\nB\n10.640873\n\n\n6\n2\nA\n10.339079\n\n\n6\n2\nA\n5.657473\n\n\n6\n2\nB\n14.381581\n\n\n6\n2\nB\n10.791359\n\n\n7\n2\nA\n10.346535\n\n\n7\n2\nA\n7.216693\n\n\n7\n2\nB\n11.446316\n\n\n7\n2\nB\n11.790792\n\n\n8\n2\nA\n10.578174\n\n\n8\n2\nA\n11.689476\n\n\n8\n2\nB\n11.772819\n\n\n8\n2\nB\n11.607435\n\n\n9\n2\nA\n12.047757\n\n\n9\n2\nA\n12.412829\n\n\n9\n2\nB\n9.171424\n\n\n9\n2\nB\n9.320691\n\n\n10\n2\nA\n8.001816\n\n\n10\n2\nA\n9.281320\n\n\n10\n2\nB\n11.421702\n\n\n10\n2\nB\n9.449261\n\n\n11\n3\nA\n8.122167\n\n\n11\n3\nA\n10.822403\n\n\n11\n3\nB\n11.916796\n\n\n11\n3\nB\n14.680328\n\n\n12\n3\nA\n11.015324\n\n\n12\n3\nA\n13.514035\n\n\n12\n3\nB\n11.700311\n\n\n12\n3\nB\n14.086357\n\n\n13\n3\nA\n8.083916\n\n\n13\n3\nA\n12.421829\n\n\n13\n3\nB\n6.086836\n\n\n13\n3\nB\n10.293708\n\n\n14\n3\nA\n10.653113\n\n\n14\n3\nA\n12.397645\n\n\n14\n3\nB\n9.655439\n\n\n14\n3\nB\n9.994885\n\n\n15\n3\nA\n8.795892\n\n\n15\n3\nA\n9.361011\n\n\n15\n3\nB\n11.279876\n\n\n15\n3\nB\n11.528820\n\n\n\n\n\n\n\nfit linear mixed model\n\nlibrary(lmerTest)\nD$group <- factor(D$group)\n\n# if we only analyze the first repetition:\n# fit <- lmer(AUC ~ group + (1|cage), data=D)\n\n# otherwise:\nfit <- lmer(AUC ~ group + repetition + (1|cage), data=D)\n\n\n\nanova and two-sided t-test\n\nanova(fit)\n\nType III Analysis of Variance Table with Satterthwaite's method\n Sum Sq Mean Sq NumDF DenDF F value Pr(>F) \ngroup 13.0512 13.0512 1 44 3.7887 0.0580 .\nrepetition 1.4311 0.7156 2 12 0.2077 0.8153 \n---\nSignif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\nsummary(fit) # look at the p-value of groupB\n\nLinear mixed model fit by REML. t-tests use Satterthwaite's method [\nlmerModLmerTest]\nFormula: AUC ~ group + repetition + (1 | cage)\n Data: D\n\nREML criterion at convergence: 246.2\n\nScaled residuals: \n Min 1Q Median 3Q Max \n-2.4500 -0.6722 0.1466 0.6608 1.9061 \n\nRandom effects:\n Groups Name Variance Std.Dev.\n cage (Intercept) 0.5942 0.7709 \n Residual 3.4448 1.8560 \nNumber of obs: 60, groups: cage, 15\n\nFixed effects:\n Estimate Std. Error df t value Pr(>|t|) \n(Intercept) 9.8962 0.5903 17.0200 16.764 5.14e-12 ***\ngroupB 0.9328 0.4792 44.0000 1.946 0.058 . \nrepetition2 0.0736 0.7630 12.0000 0.096 0.925 \nrepetition3 0.4579 0.7630 12.0000 0.600 0.560 \n---\nSignif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\nCorrelation of Fixed Effects:\n (Intr) groupB rpttn2\ngroupB -0.406 \nrepetition2 -0.646 0.000 \nrepetition3 -0.646 0.000 0.500\n\n\nSo groupB is 0.93 bigger than groupA. The p-value is 0.058.\n\n\none-sided t-test\nsee this stackoverflow post\n\nCOEFS <- coef(summary(fit))\n\n# put here your names for the groups\ngroup_A_name <- levels(D$group)[1]\ngroup_B_name <- levels(D$group)[2]\n\nt_val <- COEFS[2, \"t value\"]\ndf <- COEFS[2, \"df\"]\n\n# PVALUE: one-sided t-test for groupB > groupA\np_val <- pt(t_val, df, lower.tail=FALSE)\n\ncat(\"p-value for one-sided t-test for the alternative hypothethis:\\n\", \n levels(D$group)[2], \">\", levels(D$group)[1], \":\", p_val, \"\\n\")\n\np-value for one-sided t-test for the alternative hypothethis:\n B > A : 0.02900157 \n\n# PVALUE: one-sided t-test for groupB < groupA\np_val <- pt(t_val, df, lower.tail=TRUE)\ncat(\"p-value for one-sided t-test for the alternative hypothethis:\\n\", \n levels(D$group)[2], \">\", levels(D$group)[1], \":\", p_val, \"\\n\")\n\np-value for one-sided t-test for the alternative hypothethis:\n B > A : 0.9709984 \n\n\nThe one-sided p-value corresponds to half the two-sided p-value, if and only if the reference group is the one supposed to be smaller. The reference group is a term in R, and is (unless specified) the groupname that comes first in the alphabet. In this case, the reference group is groupA. So the one-sided p-value for groupB > groupA is 0.029 which also equals 0.058/2."
},
{
"objectID": "posts/lmer-slopes-new/index.html",
diff --git a/sitemap.xml b/sitemap.xml
index 27aa92f..d67432f 100644
--- a/sitemap.xml
+++ b/sitemap.xml
@@ -14,7 +14,7 @@