[R-SIG-Mac] Fwd: Passing data to lm using RSPerl

Don MacQueen macq at llnl.gov
Wed Jul 5 22:32:19 CEST 2006


Unfortunately, I have no experience with the syntax of RSPerl 
expressions within a perl script. But comparing the use of print(), 
that succeeded, with the use of lm(), which did not, I see something 
I think worth trying.

I would try this:

    &R::callWithNames( "lm", { 'r2 ~ r1' ,  @df }  );

because the third argument to lm() should be the data object itself. 
If you were doing this within R itself, the expressions you would 
write are

   print(name_of_dataframe)
   lm( r2~r1, name_of_dataframe)

So it seems to me that if
    &R::call( "print", @df);
is correct, then you would supply the data to lm() with an analogous 
syntax, i.e.,
    &R::callWithNames( "lm", { 'r2 ~ r1' , @df }  );

-Don

At 9:44 AM -0400 6/30/06, Adam Wizon wrote:
>Here is the code I am using (which I know is not quite right).  It 
>produces the output below.  The data frame summary seems to print 
>correctly, but the call to 'lm' says that I do not have a 'terms 
>component'.   I think I am confused about whether objects reside in R 
>or Perl although my code assumes I can create a Perl object (in this 
>case df) that can be passed back to R as a data frame.  Can someone 
>clarify this for me?
>
>--------------
>R::initR("--silent");
>&R::library("RSPerl");
>
>my @df = &R::callWithNames( "data.frame", { 'date', $r_date[0], 'r1', 
>$e_ret[0], 'r2', $e_ret[1] } );
>
>print "Printing summary of df\n";
>&R::call( "print", @df);
>
>print "Computing linear model parameters\n";
>&R::callWithNames( "lm", { 'r2 ~ r1' , 'data', \@df }  );
>
>--------------
>
>           date                    r2                    r1
>1  2006-05-01   -0.0573445954863402   -0.0112231851115216
>2  2006-05-02    0.0150507525376269    0.0290229885057473
>3  2006-05-03 -0.000275862068965462  -0.00670203853672164
>4  2006-05-04    0.0117273730684326 -0.000140567894293015
>5  2006-05-05    0.0278876312559662    0.0106846618866864
>6  2006-05-08    -0.054792703150912                     0
>7  2006-05-09  -0.00357919854024857   -0.0119627208234803
>8  2006-05-10  0.000986054373855577  -0.00605378009291858
>9  2006-05-11  -0.00830284266816779   -0.0347025495750707
>10 2006-05-12  -0.00744997871434661  -0.00660308143800444
>11 2006-05-15  -0.00986489384516403   0.00132939438700153
>12 2006-05-16   0.00678651360912568   -0.0414515415252987
>13 2006-05-17   -0.0254571531014699   0.00430901815943369
>14 2006-05-18   -0.0300956585724798   -0.0318725099601594
>15 2006-05-19   0.00485547378802812      0.02105096549541
>16 2006-05-22   -0.0130615326538316   -0.0175166640830879
>17 2006-05-23    0.0155293757649939   -0.0036289050173557
>18 2006-05-24    0.0207909604519773   0.00300870942201116
>19 2006-05-25   0.00715814331045678    0.0156299336911903
>20 2006-05-26   0.00600820633059805   -0.0121249805689414
>Printing summary of df
>           date                    r2                    r1
>1  2006-05-01   -0.0573445954863402   -0.0112231851115216
>2  2006-05-02    0.0150507525376269    0.0290229885057473
>3  2006-05-03 -0.000275862068965462  -0.00670203853672164
>4  2006-05-04    0.0117273730684326 -0.000140567894293015
>5  2006-05-05    0.0278876312559662    0.0106846618866864
>6  2006-05-08    -0.054792703150912                     0
>7  2006-05-09  -0.00357919854024857   -0.0119627208234803
>8  2006-05-10  0.000986054373855577  -0.00605378009291858
>9  2006-05-11  -0.00830284266816779   -0.0347025495750707
>10 2006-05-12  -0.00744997871434661  -0.00660308143800444
>11 2006-05-15  -0.00986489384516403   0.00132939438700153
>12 2006-05-16   0.00678651360912568   -0.0414515415252987
>13 2006-05-17   -0.0254571531014699   0.00430901815943369
>14 2006-05-18   -0.0300956585724798   -0.0318725099601594
>15 2006-05-19   0.00485547378802812      0.02105096549541
>16 2006-05-22   -0.0130615326538316   -0.0175166640830879
>17 2006-05-23    0.0155293757649939   -0.0036289050173557
>18 2006-05-24    0.0207909604519773   0.00300870942201116
>19 2006-05-25   0.00715814331045678    0.0156299336911903
>20 2006-05-26   0.00600820633059805   -0.0121249805689414
>Computing linear model parameters
>Error in terms.formula(formula, data = data) :
>          argument is not a valid model
>
>
>Begin forwarded message:
>
>>  From: Adam Wizon <adamwizon at comcast.net>
>>  Date: June 28, 2006 10:58:35 AM EDT
>>  To: r-sig-mac at stat.math.ethz.ch
>>  Subject: [R-SIG-Mac] Passing data to lm using RSPerl
>>
>>  I have successfully installed RSPerl.  I am retrieving data from a
>>  database in Perl and trying to run a linear model.  I have read the
>>  documentation, but I do not quite understand how to set up the call
>>  to 'lm' inside Perl.  Can someone provide an example I can work
>>  from?  Thanks.
>>
>>  Adam Wizon
>>
>>  _______________________________________________
>>  R-SIG-Mac mailing list
>>  R-SIG-Mac at stat.math.ethz.ch
>>  https://stat.ethz.ch/mailman/listinfo/r-sig-mac
>
>
>	[[alternative HTML version deleted]]
>
>_______________________________________________
>R-SIG-Mac mailing list
>R-SIG-Mac at stat.math.ethz.ch
>https://stat.ethz.ch/mailman/listinfo/r-sig-mac


-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA



More information about the R-SIG-Mac mailing list