library(ggplot2)
# F 분포 그래프
ggplot(data.frame(x=c(0,5)), aes(x=x)) +
stat_function(fun=df, args=list(df1=9, df2=9), colour="blue", size=0.5) +
stat_function(fun=df, args=list(df1=10, df2=30), colour="red", size=0.5) +
stat_function(fun=df, args=list(df1=50, df2=100), colour="yellow", size=0.5) +
annotate("segment", x=3, xend=3.5, y=1.4, yend=1.4, colour="blue", size=0.5) +
annotate("segment", x=3, xend=3.5, y=1.2, yend=1.2, colour="red", size=0.5) +
annotate("segment", x=3, xend=3.5, y=1.0, yend=1.0, colour="yellow", size=0.5) +
annotate("text", x=4.3, y=1.4, label="F(df1=9, df2=9)") +
annotate("text", x=4.3, y=1.2, label="F(df1=10, df2=30)") +
annotate("text", x=4.3, y=1.0, label="F(df1=50, df2=100)") +
ggtitle("F Distribution")

# 누적 F 확률 그래프
ggplot(data.frame(x=c(0,5)), aes(x=x)) +
stat_function(fun=pf, args=list(df1=9, df2=9), colour="blue", size=1) +
ggtitle("Cumulative F-distribution : F(df1=9, df2=9)") +
geom_hline(yintercept = 0.95) +
geom_vline(xintercept = 3.17) +
geom_vline(xintercept = 2.1028, show.legend = T) + geom_hline(yintercept = 0.8578)

# 누적 F분포 확률 값 계산
pf(3.17, 9, 9) # => 0.949% 누적분포 함수 ( cumulative distribution function )
[1] 0.9496076
qf(0.95, 9, 9) # => 3.178 분위수 함수 ( quantile function )
[1] 3.178893
a = c(175, 168, 168, 190, 156, 181, 182, 175, 174, 179)
b = c(185, 169, 173, 173, 188, 186, 175, 174, 179, 180)
c = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
var.test(a, b) # F = 2.1028, p-value = 0.2834
F test to compare two variances
data: a and b
F = 2.1028, num df = 9, denom df = 9, p-value = 0.2834
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.5223017 8.4657950
sample estimates:
ratio of variances
2.102784
var(a) / var(b) # => 2.1028
[1] 2.102784
qf(0.95, length(a) - 1, length(b) - 1) # => 3.178 , 2.10 < 3.17 , so 귀무가설 지지 ( 등분산 )
[1] 3.178893
pf(2.10, 9, 9) #=> 0.8578
[1] 0.8578669
( 1 - pf(2.1028, length(a) - 1, length(b) - 1) ) * 2 # => 0.2834 == p-value of var.test
[1] 0.2834206