2
$\begingroup$

I am trying to write a function that tests hypotheses and gives me a nice table but I can't. I need forum's help. For example, I have the following data

data1 = RandomReal[{0, .5}, 100]; 

I want to test whether the mean of the data is significantly different form zero using t-test and produce a table that has mean of the data and t-value but mean should have *, ** or *** if the mean is significant at 1%,5% and 10% respectively and t-value should be in brackets such [t-value]. If I do the following code:

{If[Quiet[TTest[data1, Automatic, "PValue"]] <= .05, SuperStar[
  Mean[data1]], Mean[data1]], 
 Quiet[TTest[data1, Automatic, "TestStatistic"]]}

I get

enter image description here

However, I am trying to get

enter image description here

based on whether I have P - value <= 0.01 or P - value <= 0.05 or P - value <= 0.1 respectively.

This problem may not look practical but I am trying to apply this function to a data with more than 20000 observations and 6 columns and I have to use the same test to different sub-samples and produce a table with mean and t-values.

Any help is greatly appreciated.

$\endgroup$
1
  • $\begingroup$ Stars can be done with Superscript. Have a look at ToString and StringJoin as well. $\endgroup$ Commented Feb 15, 2015 at 18:45

1 Answer 1

2
$\begingroup$

Maybe something like:

ClearAll[foo];
foo[t1_: 0.01, t2_: 0.05, t3_: 0.1] := 
  Module[{m = Mean@#, p = Quiet@TTest[#, Automatic, "TestData"], s = {"", "*", "**", "***"}, c}, 
    c = # <= p[[2]] < #2 & @@@ Partition[{0, t1, t2, t3, Infinity}, 2, 1];
    {Which @@ Riffle[c, Superscript[ToString@m, #] & /@ s /. Superscript[x_, ""] :> x], 
     Row[{"[", p[[1]], "]"}]}] &;

Example:

data1 = RandomReal[{0, .5}, 20];

foo[][data1]
(* {0.282717, [8.44013]} *)

foo[][data1 - .2]
(* {0.0827171^*, [2.4694]} *)

foo[][data1 - .25]
(* {0.0327171^***, [0.976723]} *)
$\endgroup$
2
  • $\begingroup$ Thank you kguler. This is helpful. With this help, I will be able to get what I want. $\endgroup$
    – ramesh
    Commented Feb 15, 2015 at 20:55
  • $\begingroup$ @ramesh, my pleasure. The answer deals with formatting only. Not sure if i got the logic right -- the conditions/inequalities may be all mixed up :) $\endgroup$
    – kglr
    Commented Feb 15, 2015 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.