作者sqrt1089 (李崩子)
看板Perl
标题[问题] PERL 怎麽算 P VALUE
时间Mon Sep 5 13:32:43 2011
如题
数据:
10 100 300 5000
25 100 300 5000
3 100 300 5000
59 100 300 5000
46 100 300 5000
....
...
..
.
意思是说一个袋子里全部有5000颗球,有300颗是白球,其它4700颗是黑球
我一次取100颗出来观察,取n次,(n大概100次左右)。
第一次有10颗白球;第二次有25颗白球;第三次取到3颗,第四次取到59颗,...X100次
原本是有找到一个套件:use Math::GSL::Randist qw/:all/;
$p = gsl_ran_hypergeometric($r, $n1, $n2, $t);
但这个模组一直灌不起来。
最後改用:use Statistics::R;
但是去测试後发现好慢,原因是 我一次丢四个数字给他,
他回传一个 P_VALUE (或是 1- P_VALUE) 回来
假设我取 n 次,就会丢 n 次的 四个数字给副程式
这样的话 副程式 就做了N次的开档关档的工作。<<<原因就是出在这
有什麽办法可以改善呢?谢谢。
####
程式码如下:
open (FH,"test.txt")||die";#test.txt 就是 约100X4的数据如最上面
while(<FH>){
chomp;
..
.
$pvalue = &hypergeometric($get,$input,$included,$total);
print $pvalue;
}
sub hypergeometric {
my $common = shift;
$common_gene -= 1;
my $g1 = shift;
my $g2 = shift;
my $whole = shift;
my $no_g1 = $whole - $g1;
my $R = Statistics::R->new();
$R->startR;
$R->send(qq`
x <- 0:$common; N1 <- $g1 \n
N2 <- $no_g1; n <- $g2 \n
ans=cumsum(dhyper(x,N1,N2,n)) \n
print(ans)
`);
my $result = $R->read;
my @value = split " ", $result;
my $last_value = pop @value;
my $ans = 1-$last_value;
$R->stopR();
return $ans;
}
--
\ ∩──、 =====
\/ ● , \ ======
/\( ● ¯● |つ
| ╳_入_ノ ミ ︵ 用这种饵
(_/ ノ / / 也想让本大爷上钩…
/\___ノ_/ / ======
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.116.215.174