BE-proff ● 2019-02-04 13:12 (2079 d 21:58 ago) Posting: # 19855 Views: 5,323 |
|
Hi All, Recently I have started learning Python and I'd like to write script calculating sample size (the same as sampleN.TOST function in R). Can anybody share formula of formulas set for relevant calculations? |
ElMaestro ★★★ Denmark, 2019-02-04 14:27 (2079 d 20:42 ago) @ BE-proff Posting: # 19857 Views: 4,580 |
|
Hi BE-proff, ❝ Can anybody share formula of formulas set for relevant calculations? This is a great challenge you awarded yourself. Sample size calculation is not a set of deterministic equations (yet?!?), but usually involve integrals. For a start try and look at Potvin's paper. Here you'll find a very decent approximation which is based on the central t test. This provides the "exact" solution when the expected match is 100% and it is a very decent* approximation in most other relevant case whenever desired power is above the magical 80%. Perhaps you can start there and then make it more complicated and exact once you get into it? Here's how I would approach it (and I am assuming you are not relying on external libraries**): 1. Make a function for the density of central t at any level of df. 2. Make a function that can integrate it, from minus infinity to some arbitrary value x. 3. Make a function that can find the critical value of the central t for an arbitrary p-level between 0 and 1. This one may be a little tricky if you write it from scratch but you can find inspiration by googling Russel Lenth's ASA243 Al Gore Rhythm. I imagine you can also find some py code on the www but I am not certain about it. With those three at hand you can reproduce power.TOST sand sampleN.TOST values using the central t approximation by insertion into the equation from Potvin. You may be able to find a way to import and use gsl or similar libraries in python, then it is simply a matter of calling some functions. Can't help with the implementation of it as I don't do python. Next, if you are hardcore you can look into noncentral t and Owen's Q. If you can write the framework above then you have all the skill at hand to also do this last step. This will give you the opportunity to get anything power.TOST does. Happy coding. Interested to hear of your further achievements, so please keep me posted. * I am sure the BE police will spank me for saying so but I am willing to defend my words. ** I am also willing to at least read posts asking why someone would want to reinvent the wheel. — Pass or fail! ElMaestro |
BE-proff ● 2019-02-04 14:43 (2079 d 20:27 ago) @ ElMaestro Posting: # 19858 Views: 4,572 |
|
Hi ElMaestro, Thank you for clarification. It looks really very challenging... So I can't promise you quick response - wait for some time. |
ElMaestro ★★★ Denmark, 2019-02-04 18:17 (2079 d 16:53 ago) @ BE-proff Posting: # 19864 Views: 4,935 |
|
Hi BE-proff, I just downloaded python and played around with it. Syntatically it is not so hard, I think. Below are some functions that will get you started. They execute just fine as a script on my computer (Win10). The functions use Simpson integrals and various constants that that you can play around with to achieve the combination of accuracy and speed that suits you. Note that I only made step 1-3, so I will leave it to you to put these into Potvin's equation. They are in no way optimized so there's plenty of work to do still
import math On my machine I get:
>>> — Pass or fail! ElMaestro |
ElMaestro ★★★ Denmark, 2019-02-04 23:31 (2079 d 11:39 ago) @ BE-proff Posting: # 19865 Views: 4,421 |
|
Hey, learning python is great fun The code below is executing quite instantaneously on my machine. It involves a simple re-write of the density function using the very handy shortcut noted on wikipedia.
import math Now don't feed it non-integer df's without extending the density function appropriately. — Pass or fail! ElMaestro |
BE-proff ● 2019-02-05 08:19 (2079 d 02:51 ago) @ ElMaestro Posting: # 19866 Views: 4,492 |
|
Hi ElMaestro, Star is shocked Since my math skills are not very strong I will hardly make out the idea But nevertheless I have managed to realize CVfromCI function in pyto |