WeBWorK Problems

R statistics for outliers

R statistics for outliers

by Vivien Altwasser -
Number of replies: 2

Hello everyone,

I am writing some statistic problems and got stuck with outliers. I wanted to use the grubbs and dixon test with the R library outlier but found no working routine to get this done on the webwork platform. I have installed the package in the linux shell and made sure, that everything works there as expected.

But I still get error code from the pg-file:

 Undefined subroutine &main::library called at line 83 of (eval 4558)

Here is the code which is partly copied from another outlier problem concerning grubbs (which worked fine, as it did not need the additional library). I know about the accumulating Macros and will deplete unnecessary ones in the future.


DOCUMENT();
loadMacros(
"MathObjects.pl",
"PGML.pl",
"niceTables.pl",
"PGstandard.pl",
"MathObjects.pl",
"parserMultiAnswer.pl",
"PGcourse.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"parserRadioButtons.pl",
"unionTables.pl",
"PGstatisticsmacros.pl",
"parserPopUp.pl",
"PGtikz.pl",
"parserOneOf.pl",
"PGcommonFunctions.pl",
"contextPiecewiseFunction.pl",
"answerHints.pl",
"problemPanic.pl",
"contextInequalities.pl",
"RserveClient.pl" # <--- R integration
);
###############

TEXT(beginproblem());

# Show which answers are correct and which ones are incorrect
$showPartialCorrectAnswers = 1;
#################################
# basic information
$n = random(5,8,1);
$alpha = list_random(0.1,.05,.01,.001);

#state mean and sd for a fictive normal distribution
$mean = random(15,25,.1);
$sd = random(1,5,1)+non_zero_random(.1,.9,.1);
@data = urand($mean,$sd,$n-1,2);

# insert probable outlier that is small or large
$ran = random(0,1,1);
if($ran == 0){
push(@data,min(@data)-$sd*random(2,5,.1));
}
else{
push(@data,max(@data)+$sd*random(2,5,.1));
}

# reorganise data, so outliers do not accumulate at the end of array
@shuffle = NchooseK(scalar(@data),scalar(@data));
@data = @data[@shuffle];
@line = join(" ",@data);

# start one session
rserve_start();

#initalize x
rserve_eval("x<-c()");

# transfer the array to a variable in R (is that really necessary? didnt get R to read an array directly, alternative might be to form up the array in R in the first place without perl)
foreach my $j (0..scalar(@data)-1){
rserve_eval("x[$j+1]<-c($data[$j])"); # needs "..." to understand perl variables
}
rserve_eval(library("outliers"));
rserve_finish();
###############################################
################################################
Context()->texStrings;
BEGIN_TEXT
@dixon
$PAR
Nachfolgend finden Sie die normalverteilten Ergebnisse einer Mehrfachbestimmung:$PAR
@line
$PAR
END_TEXT
I am grateful for hints to where the package has to be installed and what routine keeps it running for the web surface. I found scarce information on the package itself and absolutely nothing concerning R packages in Webwork in general.

Kind regards

In reply to Vivien Altwasser

Re: R statistics for outliers

by Danny Glin -

The reason for the error message is that reserve_eval needs to be passed a string, so you will need to put your R code in quotes.  You have to be a little careful if your R code also has quotes in it.

In this case you can put the R code in single quotes since you are not using any perl variables in that command, so the following should work:

rserve_eval('library("outliers")');

One other thing that may arise is that you need to make sure that you installed the R package for all users on your server, particularly if Rserve is running as a user other than yours.  If you ran install.packages() as a super user then this is probably the case.

In reply to Danny Glin

Re: R statistics for outliers

by Vivien Altwasser -

Many thanks, the combination of quotes did the trick. I also trief different options but it seems I did not fully understood the Quotienten.

Thanks again, saved my day.