我想为自己的功能展示一个漂亮的外观。
我创建了自己的函数来解决一些卡方测试练习,结果如下:
这是我自己的功能:
chisq.poly = function(prob, freq, significance.level = 0.95) {
#calculate chi-squared value
expect_freq<-prob*sum(freq)
dif_freq<-freq-expect_freq
dif_freq_sq<-((freq-expect_freq)^2)/expect_freq
chi_square<-sum(dif_freq_sq)
chi_square_crit<-qchisq(significance.level, length(freq)-1, lower.tail=TRUE)
#we put all necessary values into a dataframe (good for display) and then rename headers
chi_table<-data.frame(prob,freq,expect_freq,dif_freq)
colnames(chi_table)<-c("Probability","Frequency","Expected frequency","Difference")
#Finally, we show the result of test
hypothesis<-"H0: The experimental data is like the statemnent."
result<-if (chi_square>chi_square_crit) {"Reject the null hypothesis. The experimental data is not like the statemnent."
}
else "cannot reject null hypothesis"
warning_test<-if (abs(sum(expect_freq-freq))<sum(abs(expect_freq-freq))) {"The expected frequency value may be less than 5, the result cannot be properly."
}
else "Something good so far."
O<-list(chi_table,chi_square,chi_square_crit,hypothesis,result,warning_test)
names(O)<-c('Table','Chi_square','Critical Chi_square','Hypothesis','Result','Warning')
return(O)
}
我想像这样或更佳地解决它: