Perl: Statistics with R - by Eun Bae Kim (08/13/2018)
 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use strict;
use Statistics::R;
 
# Create a Communication Bridge
my $oR = Statistics::R->new();

# Set and Get values
$oR->set('x', 123);                        # set a value to a variable, 'x'
print "--------------------------------------------\n";
print $oR->get('x')."\n";                  # get a value from a variable, 'x'

# Run a Command.
my $sCommand = "a=c(1,2,3,2,3,2,1,2,3,3,2,1,2,3);\n".
               "b=c(6,7,8,7,6,7,8,7,7,7,8,7,6,7);\n".
               "c=c(3,4,3,4,5,4,4,5,4,4,5,4,3,4);\n".
               "r=t.test(a, b, alt='less');\n".
               "p1=r\$p.value;\n".
               "x=c(a, b, c);\n".
               "g=c(rep('a', 14), rep('b', 14), rep('c', 14));\n".
               "g=factor(g);\n".
               "d=data.frame(x, g);\n".
               "y=aov(x~g, data=d);\n".
               "r=anova(y);\n".
               "p2=r[1,5];\n";
$oR->run($sCommand);


print "--------------------------------------------\n";
print $sCommand;
print "--------------------------------------------\n";
my $fPValT = $oR->get('p1');                 # get a value from a variable, 'p1'
my $fPValA = $oR->get('p2');                 # get a value from a variable, 'p2'

print "p_Ttest = $fPValT\n";
print "p_Anova = $fPValA\n";


# For multiple Lines
$sCommand = <<EOF;
a=c(1,2,3,2,3,2,1,2,3,3,2,1,2,3);
b=c(6,7,8,7,6,7,8,7,7,7,8,7,6,7);
t.test(a, b, alt='less');
EOF
my $sOuput = $oR->run($sCommand);
print "--------------------------------------------\n";
print $sCommand."\n";
print "--------------------------------------------\n";
print $sOuput."\n";

# Close the Communication Bridge
$oR->stop();


# For more information, refer to the following link
# https://metacpan.org/pod/Statistics::R