With the data I have, this R
code x <- t.test(Age ~ Completers, var.equal = TRUE, data = data)
renders the following result:
Two Sample t-test
data: Age by Completers
t = 0.93312, df = 1060, p-value = 0.351
alternative hypothesis: true difference in means between group Completers and group Non Completers is not equal to 0
95 percent confidence interval:
-0.5844018 1.6442118
sample estimates:
mean in group Completers mean in group Non Completers
37.16052 36.63062
What I would like is to plot each mean (found in x$estimate[1]
and x$estimate[2]
) with its own point on the x
axis at its proper height on the y
axis (on the same graph) and each point complemented with the same confidence interval (CI) (found in x$conf.int[1]
and x$conf.int[2]
). Like this[*]:
Unfortunately, if I'm not mistaken, plot()
(from the Generic X-Y Plotting) does not seem to handle this. So I tried with plotCI
(from gplots) as follows:
library(gplots)
plotCI(x = x$estimate[1], y = x$estimate[2],
li = x$conf.int[1], ui = x$conf.int[2])
But it renders as shown below:
My questions:
- Is there a way to obtain a plot such as in the first graph with Base R code?
- If not, what would be the solution (short of using the
jmv::
code (see [*]))?
EDIT
As requested in the comments, please find hereunder some code that help reproduce the data (T-Test results are won't be exactly the same as above, but the idea is the same):
# Generate random numbers with specific mean and standard deviation
completers <- data.frame(Completers = 1,
Age = rnorm(100, mean = 37.16052, sd = 8.34224))
nonCompleters <- data.frame(Completers = 0,
Age = rnorm(100, mean = 36.63062, sd = 11.12173))
# Convert decimaled number to integers
completers[] <- lapply(completers, as.integer)
nonCompleters[] <- lapply(nonCompleters, as.integer)
# Stack data from 2 different data frames
df <- rbind(completers, nonCompleters)
# Remove useless data frames
rm(completers, nonCompleters)
# Age ~ Completers (T-Test)
(tTest <- t.test(df$Age ~ df$Completers, var.equal = TRUE))
Sources:
- Generate random numbers with specific mean and standard deviation (Scroll down until "From Normal Distribution")
- Convert decimaled number to integers
- Stack data from 2 different data frames
[*] Graph obtained with Jamovi Version 2.3.15.0 which uses the following code (but I would like to avoid using jmv::
):
jmv::ttestIS(
formula = Age ~ Completers,
data = data,
plots = TRUE
)
System used:
- R 4.2.1
- RStudio 2022.07.1 Build 554
- macOS Monterey Version 12.5.1 (Intel)