Comparison lines

I would like to learn how to compare a test statistic to critical values using a diagramming in RStudio, in other words I'm not knowledgeable on how to draw comparison lines since I wish to denote the tailed regions. Does anyone understand the parameters required and can offer me suggestions?

An example where the null hypothesis follows a t-distribution:

library(ggplot2)
set.seed(123)

## Generate 10 random values from a normal distribution and compute test statistic for the mean
sample=rnorm(n=10,mean=1,sd=1)
sample_statistic=mean(sample)/(sd(sample)/sqrt(length(sample)))

## Compute upper and lower significance thresholds 
lower=qt(p=0.05,df=9,lower.tail=TRUE)
upper=qt(p=0.05,df=9,lower.tail=FALSE)

## Create data for the null distribution, a t-distribution with 10-1=9 d.f.
plot_data=data.frame(x=seq(-5,5,0.001),y=sapply(seq(-5,5,0.001),function(x){dt(x=x,df=9)}))

## Create the plot with blue vertical bars for significance thresholds and a red vertical bar for the observed sample
(plot=ggplot(data=plot_data,aes(x=x,y=y))+
  geom_line()+
  geom_vline(xintercept=c(lower,upper),color="blue")+geom_vline(xintercept=sample_statistic,color="red"))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.