Xrow_to_Perl: Export One Row of the X Data Frame to a Simple Perl Hash...

Description Usage Arguments Value Note Author(s) See Also Examples

Description

Export one row of the X data frame suitable for the input to the Cubist modeling to a simple Perl language hash reference idiom in a local variable ( $X = { ... };. The previous Perl notation means create a variable $X and as a reference toa hash (the opening and closing braces) and the triple dots are key => value, pairs presenting the targeted row of X. This ability to export the row as a hash reference for Perl makes it straightforward for testing Cubist model exported by cubist_asPerl_idioms. The variable $X can be vivicated into a Perl script by the eval function. Values that are NA in R become the string "infinity" in Perl; this function thus makes that conversion on the fly.

Usage

1

Arguments

x

A data frame having at least the columns required by a Cubist model that the uer is pursuing. This data frame must be compatible with Cubist::cubist() and Cubist::predict.cubist();

row

The row number in x to export;

file

The argument of the same name and purpose as file for the cat() function. This is a connection; and

comment

An optional comment line that is written before the rest of the variables.

Value

This function is used for its side effects written to the file system or to the console.

Note

Some more details on how the results of the script could be used. The design here is to get the variable $X defined within a Perl script and though not used here, the export by cubist_asPerl_idioms is designed around a variable of a hash reference contain a single row of the X input data frame for Cubist.

Suppose that we have run the code in the Examples and have the hash reference written into a file named input.txt. We can have this hash reference available inside a Perl script. To demonstrate, suppose we have our script named slurp.pl and the contents of that script are shown below:

1
2
3
4
5
6
7
8
9
  #!/usr/bin/perl -w            # very conventional declaration of Perl script
  use strict;                   # force declaration of variables
  my ($row, $X) = (0, "empty"); # declare and set them both
  undef $/;                     # undefine line ending to as to
  my $f = <>;                   # **slurp** file name into variable $f
  print "read : $f\n";          # echo what was read by prior line
  eval $f;                      # evaluate the contents of $f
  print "row=$row and pi=$X->{pi}\n";   # print to standard output
  # row=1 and pi=3.14159265358979       # should be seen on execution

We can run the Perl script from the command line by ./slurp.pl input.txt or on a unix-pipe line by cat input.txt | ./slurp.pl.

Author(s)

W.H. Asquith

See Also

cubist_asPerl_idioms

Examples

 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
# We set the simulation sample size to 1,000
nsim <- 1000

set.seed(1) # arbitrary here, but setting the seed for reproducibility

# simulate some X and Y data
X <- sort(runif(nsim, min=0, max=2))
Y <- 0.34*sin(2*pi*X) - .74*cos(2*pi*X) -
     0.14*sin(4*pi*X) + .19*cos(4*pi*X) + rnorm(nsim, sd=0.2)
X <- data.frame(X=X, pi=pi) # the design that follows both by Cubist
# and by the idiom functions in this package need at least two columns
# even if the second column containing pi is never used.

# export row 3 with the comment and result go to the console
Xrow_to_Perl(X, row=3, comment=" from Cubist.Idioms::Xrow_to_Perl()")
##  from Cubist.Idioms::Xrow_to_Perl()
#  $ROW = 3;
#  $X = {
#   X => 0.00262931315228343,
#   pi => 3.14159265358979};

path <- tempdir()
file <- paste0(path,"/","perlsrc.txt")
FH <- file(file, open="w")
  Xrow_to_Perl(X, row=3, comment="", file=FH)
close(FH)
print(file)

wasquith-usgs/Cubist.Idioms documentation built on Jan. 1, 2021, 12:45 p.m.