0

I would like to present a good look result for my own functions.

I create my own function to solve some chi-square test exercises, and the result looks like that:

enter image description here

Here is my own function:

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)
}

And I want to fix it like this or better:

enter image description here

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Have a look at S3 classes and create a print function for your result as described here http://adv-r.had.co.nz/S3.html – drmariod Mar 26 '19 at 06:43
  • Instead of reinventing the wheel take a look at the [`broom` library](https://cran.r-project.org/web/packages/broom/vignettes/broom.html). In my opinion, your "good look result" is not very good looking, and -- perhaps more importantly -- not precise/correct from a statistical point of view. For example what does "The experimental data is not like the statement" even mean? Data are what they are! I guess you meant to say that the probability for observing data (frequencies) as extreme or more extreme (i.e. the p value) is small. – Maurits Evers Mar 26 '19 at 07:25
  • Thank you @drmariod – Vĩnh Vũ Quang Mar 26 '19 at 08:07
  • Thank you@MauritsEvers. My own function is about Goodness-of-Fit Test for polynomial distribution, and my bad vocabularies mislead you, sorry for this. I will try your suggestions, good guys :) – Vĩnh Vũ Quang Mar 26 '19 at 08:14
  • `stargazer`it is also a good library for your purposes! – LocoGris Mar 26 '19 at 09:41
  • @LocoGris Awesome! It will be very helpful to present these results in LaTex documents. Thank you! – Vĩnh Vũ Quang Mar 26 '19 at 10:20

0 Answers0