t分布の描画

自由度18のt分布における棄却域と,t(18)=2.794のp値

library(tidyverse)
## ─ Attaching packages ─────────────────────────────────── tidyverse 1.2.1 ─
## ✔ ggplot2 3.1.1     ✔ purrr   0.3.2
## ✔ tibble  2.1.1     ✔ dplyr   0.8.1
## ✔ tidyr   0.8.3     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ─ Conflicts ──────────────────────────────────── tidyverse_conflicts() ─
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
# t分布の描画 ------------------------------------------------------------------
library(gridExtra)
## 
##  次のパッケージを付け加えます: 'gridExtra'
##  以下のオブジェクトは 'package:dplyr' からマスクされています: 
## 
##      combine
old = theme_set(theme_gray(base_family = "HiraKakuProN-W3"))

q <- qt(c(0.025,0.975),df=18)
ggplot(data = data.frame(X = c(-5, 5)), aes(x = X)) + stat_function(fun = dt, args = list(df = 18))+xlab("")+
  geom_ribbon(data=data.frame(X=x<-seq(q[1],q[2],len=101), Y=dt(x,df=18)), aes(x=X, ymin=0, ymax=Y),alpha=0.3)+
  geom_ribbon(data=data.frame(X=x<-seq(2.794003,5,len=101), Y=dt(x,df=18)), aes(x=X, ymin=0, ymax=Y),fill="red",alpha=0.8)

片側と両側の違い

# 片側と両側
p1 <- ggplot(data = data.frame(X = c(-5, 5)), aes(x = X)) + stat_function(fun = dt, args = list(df = 18))+xlab("")+
  geom_ribbon(data=data.frame(X=x<-seq(q[1],q[2],len=101), Y=dt(x,df=18)), aes(x=X, ymin=0, ymax=Y),alpha=0.3)

p2 <- ggplot(data = data.frame(X = c(-5, 5)), aes(x = X)) + stat_function(fun = dt, args = list(df = 18))+xlab("")+
  geom_ribbon(data=data.frame(X=x<-seq(-5,qt(0.95,df=18),len=101), Y=dt(x,df=18)), aes(x=X, ymin=0, ymax=Y),alpha=0.3)
  

grid.arrange(p1, p2, ncol = 1)

Share