Я хотел бы представить хороший результат для своих собственных функций.
Я создаю свою собственную функцию для решения некоторых тестовых упражнений хи-квадрат, и результат выглядит так:
Вот моя собственная функция:
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)
}
И я хочу исправить это так или лучше: