x<-dplyr::filter(PlantGrowth,group=="ctrl")summary(x$weight)#> Min. 1st Qu. Median Mean 3rd Qu. Max. #> 4.170 4.550 5.155 5.032 5.293 6.110shapiro.test(x$weight)#> #> Shapiro-Wilk normality test#> #> data: x$weight#> W = 0.95668, p-value = 0.7475t.test(x$weight, mu=5)#> #> One Sample t-test#> #> data: x$weight#> t = 0.17355, df = 9, p-value = 0.8661#> alternative hypothesis: true mean is not equal to 5#> 95 percent confidence interval:#> 4.614882 5.449118#> sample estimates:#> mean of x #> 5.032
df2<-tibble( experimental=c(120.61 ,126.33 ,108.35 ,139.99 ,115.29 ,146.96 ,115.64,124.62 ,121.40 ,134.81 ,130.73 ,118.37 ,129.28 ,117.45), control=c(58.23 ,54.50 ,59.47 ,59.64 ,53.77 ,43.48 ,54.63 ,71.91 ,53.97 ,49.72 ,61.26 ,78.17,NA,NA))e_mean<-mean(df2$experimental)e_sd<-sd(df2$experimental)n1<-length(df2$experimental)ctrl_mean<-mean(df2$control,na.rm =TRUE)ctrl_sd<-sd(df2$control,na.rm =TRUE)n2<-length(df2$control)-sum(is.na(df2$control))Sc_2<-((n1-1)*e_sd^2+(n2-1)*ctrl_sd^2)/(n1+n2-2)t2<-(e_mean-ctrl_mean)/sqrt(Sc_2*(1/14+1/12))t.test(df2$experimental,df2$control,var.equal =TRUE)#> #> Two Sample t-test#> #> data: df2$experimental and df2$control#> t = 16.967, df = 24, p-value = 7.215e-15#> alternative hypothesis: true difference in means is not equal to 0#> 95 percent confidence interval:#> 58.63784 74.87954#> sample estimates:#> mean of x mean of y #> 124.98786 58.22917
# 检验两个样本的方差是否相等var.test(df2$experimental,df2$control)#> #> F test to compare two variances#> #> data: df2$experimental and df2$control#> F = 1.287, num df = 13, denom df = 11, p-value = 0.6831#> alternative hypothesis: true ratio of variances is not equal to 1#> 95 percent confidence interval:#> 0.3794599 4.1152566#> sample estimates:#> ratio of variances #> 1.287025
Code
F_stats<-(e_sd^2)/(ctrl_sd^2)F_stats#> [1] 1.287025# 计算p值p_value<-1-pf(F_stats, df1=13, df2=11, lower.tail =F)# p_value <- pf(F_stats, df1=13, df2=11)alpha<-0.05confidence_level<-1-alpha# 计算F分布的临界值f_critical_lower<-qf((1-confidence_level)/2, df1 =13, df2 =11)f_critical_upper<-qf(confidence_level, df1 =13, df2 =11)# 计算方差比率的置信区间ci_lower<-sqrt(f_critical_lower*(ctrl_sd^2/e_sd^2))ci_upper<-sqrt(f_critical_upper*(ctrl_sd^2/e_sd^2))# 输出结果cat("95% CI for variances ratio:", ci_lower, "to", ci_upper)#> 95% CI for variances ratio: 0.4929485 to 1.464781