Reputation: 21
I am creating a hypothesis test and confidence interval using this command:
```{r}
t.test(urtak1$fermetraverd ~ urtak1$matssvaedi)
```
And I need to embed the t-value and p-value as well as the confidence interval (seperately) into text for my R markdown document. How do I do this using ' r ' ?
Here is my output:
Welch Two Sample t-test
data: urtak1$fermetraverd by urtak1$matssvaedi
t = 1.0812, df = 96.784, p-value = 0.2823
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-12.02648 40.80856
sample estimates:
mean in group Hagar mean in group Kringlan
348.5697 334.1787
Upvotes: 2
Views: 241
Reputation: 3794
You can do this using the broom
package. I do not have the data you are using your questione, so I set up something quickly with iris
.
In a first chunk you do the analysis and create an object that holds the "tidy" version of your t-test output. For example
```{r}
library(dplyr)
library(broom)
example <- iris %>%
filter(Species != "setosa") %>%
droplevels()
result <- t.test(example$Sepal.Length ~ example$Species)
tidy_result <- tidy(result)
```
Have a look at tidy_result
: the magic of the broom package is that it extracts all the output of your t-test (and many others!) in a tidy dataframe, that you can use elsewhere.
Now you can use this in your text, evaluating r tidy_result$statistic
to show your t-statistic and r tidy_result$p.value
to show your p-value (the back-ticks are not showing correctly, but should be placed in front of the 'r' expressions in with the tidy_result objects.
In your Rmarkdown it might look like this:
the t-test resulted in a t-statistic of `r tidy_result$statistic`
with a p-value of `r tidy_result$p.value`
To look at all the parameters you can include, evaluate tidy_result
in your console. You will see:
> tidy_result
estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
1 -0.652 5.936 6.588 -5.629165 1.866144e-07 94.02549 -0.8819731 -0.4220269 Welch Two Sample t-test two.sided
So you can choose any of the following:
"estimate" "estimate1" "estimate2"
"statistic" "p.value" "parameter"
"conf.low" "conf.high" "method" "alternative"
Notice that your to get your confidence interval, you have the low (conf.low
) and the high (conf.high
) value at your disposal.
Upvotes: 1