Help with R?

84 views
Skip to first unread message

kcrisman

unread,
Nov 23, 2009, 4:52:03 PM11/23/09
to sage-support
Hi support,

Two questions. The first should be easy, second maybe not.

1. Any links to someone actually doing multiple cool basic stats
examples using R from within Sage? I couldn't find any in a quick
Wiki and sagemath.org search, but that doesn't mean they aren't
there. I need this for a demo I'll be giving in a little more than a
week, from the notebook. Plotting e.g. histograms would be even
better, but I think there are some issues with that currently? Keep
in mind I want to do this pretty much natively with R inside Sage,
either using r.command or r evaluation option in the notebook - trying
to keep it simple, just showing the capability.

2. Apparently, Sage still has issues with loading packages, at least
in OS X.

In the documentation we have
EXAMPLES:
sage: r.install_package('Hmisc') #optional
requires internet
[1] 4 5 6
but this is just flat wrong, because it's install_packages()

I did this, and it seemed to work. Though it told me to do r.restart
(), but
sage: r.restart()
Error: object 'sage0' not found

Anyway, next up is the "standard" package that our speaker at the last
JMM couldn't get to load in the Sage version of R, so he just showed
slides instead :(

sage: r.install_packages('MASS')
** You are using OS X. Unfortunately, the R optional package system
currently doesn't support OS X very well. We are working on this. **
<snip>
> options(repos="http://cran.r-project.org/"); install.packages("MASS")
Warning message:
In getDependencies(pkgs, dependencies, available, lib) :
package ‘MASS’ is not available

Still not sure why this doesn't work; in fact, it's supposed to be
included in every *binary* shipped, obviously that doesn't apply
directly to Sage...

Also note that even though r.library() "should" list loaded libraries,
you in fact have to do r("library()"), so that the Sage interface
isn't the same as just r.command; but maybe this is just rpy, of which
I know little.

Thanks for any help!
- kcrisman

Jason Grout

unread,
Nov 23, 2009, 5:00:38 PM11/23/09
to sage-s...@googlegroups.com
kcrisman wrote:

> Anyway, next up is the "standard" package that our speaker at the last
> JMM couldn't get to load in the Sage version of R, so he just showed
> slides instead :(
>
> sage: r.install_packages('MASS')
> ** You are using OS X. Unfortunately, the R optional package system
> currently doesn't support OS X very well. We are working on this. **
> <snip>
>> options(repos="http://cran.r-project.org/"); install.packages("MASS")
> Warning message:
> In getDependencies(pkgs, dependencies, available, lib) :
> package �MASS� is not available
>
> Still not sure why this doesn't work; in fact, it's supposed to be
> included in every *binary* shipped, obviously that doesn't apply
> directly to Sage...


The R spkg does not compile the standard packages that come with R.
There is a specific switch in the spkg-install that prevents this. I
think the reasons are mainly historical, and the sage copy of R should
have these enabled, if possible. You can see what I'm talking about if
you look at the spkg-install, IIRC.

Thanks,

Jason

kcrisman

unread,
Nov 23, 2009, 8:01:46 PM11/23/09
to sage-support


I opened a ticket for the bad doctest: http://trac.sagemath.org/sage_trac/ticket/7521

>
> > Still not sure why this doesn't work; in fact, it's supposed to be
> > included in every *binary* shipped, obviously that doesn't apply
> > directly to Sage...
>
> The R spkg does not compile the standard packages that come with R.
> There is a specific switch in the spkg-install that prevents this.  I
> think the reasons are mainly historical, and the sage copy of R should
> have these enabled, if possible.  You can see what I'm talking about if
> you look at the spkg-install, IIRC.

Okay, but how do I get this library "now", as it were? It's weird
that I can load another package by name, but not this one. If I can
get this one loaded, I think I can use our speaker's examples.
Otherwise I need other examples.

- kcrisman

William Stein

unread,
Nov 23, 2009, 9:10:49 PM11/23/09
to sage-s...@googlegroups.com
On Mon, Nov 23, 2009 at 1:52 PM, kcrisman <kcri...@gmail.com> wrote:
> Hi support,
>
> Two questions.  The first should be easy, second maybe not.
>
> 1. Any links to someone actually doing multiple cool basic stats
> examples using R from within Sage?  I couldn't find any in a quick
> Wiki and sagemath.org search, but that doesn't mean they aren't
> there.   I need this for a demo I'll be giving in a little more than a
> week, from the notebook.  Plotting e.g. histograms would be even
> better, but I think there are some issues with that currently?  Keep
> in mind I want to do this pretty much natively with R inside Sage,
> either using r.command or r evaluation option in the notebook - trying
> to keep it simple, just showing the capability.

For basic stats, see http://trac.sagemath.org/sage_trac/ticket/7197
which finally just got a positive review. This should go into
sage-4.3, I hope. It does simple symbolic descriptive stats, but
will also fall back to numpy, etc., when the input is from numpy
(etc.).

For histograms, I always use finance.TimeSeries, which is very fast
and has a nice (but simple) histogram function.

William

Jason Grout

unread,
Nov 23, 2009, 9:35:51 PM11/23/09
to sage-s...@googlegroups.com
I use matplotlib for histograms and boxplots, which gives nice control
over titles, axes labels, etc.


--
Jason Grout

Jason Grout

unread,
Nov 23, 2009, 9:52:09 PM11/23/09
to sage-s...@googlegroups.com
I just modified the R spkg-install thusly:


diff -r b73bca59a75a spkg-install
--- a/spkg-install Sun Sep 20 18:25:26 2009 -0700
+++ b/spkg-install Mon Nov 23 20:35:29 2009 -0600
@@ -77,19 +77,17 @@
CFLAGS="-I$SAGE_LOCAL/include -L$SAGE_LOCAL/lib/ "$CFLAGS; export CFLAGS
LDFLAGS="-L$SAGE_LOCAL/lib/ "$LDFLAGS; export LDFLAGS

-# do not build recommended packages for now, for speed.
-
if [ `uname` = "Darwin" ]; then
echo "Configuring R for OSX"
- ./configure --prefix="$SAGE_LOCAL" --with-recommended-packages=no
--enable-R-shlib --with-x=$XSUPPORT --with-readline="$SAGE_LOCAL" $OSXFW
+ ./configure --prefix="$SAGE_LOCAL" --enable-R-shlib
--with-x=$XSUPPORT --with-readline="$SAGE_LOCAL" $OSXFW
else
echo "Configuring R with ATLAS"
- ./configure --prefix="$SAGE_LOCAL" --with-recommended-packages=no
--enable-R-shlib --with-x=$XSUPPORT --with-readline="$SAGE_LOCAL"
--with-blas="-L$SAGE_LOCAL/lib -lf77blas -latlas"
--with-lapack="-L$SAGE_LOCAL/lib -llapack -lcblas" $SUN_FLAGS
+ ./configure --prefix="$SAGE_LOCAL" --enable-R-shlib
--with-x=$XSUPPORT --with-readline="$SAGE_LOCAL"
--with-blas="-L$SAGE_LOCAL/lib -lf77blas -latlas"
--with-lapack="-L$SAGE_LOCAL/lib -llapack -lcblas" $SUN_FLAGS
fi

if [ $? -ne 0 ]; then
echo "Configuring R with fallback options"
- ./configure --prefix="$SAGE_LOCAL" --with-recommended-packages=no
--enable-R-shlib --with-x=no --with-readline="$SAGE_LOCAL" $OSXFW $SUN_FLAGS
+ ./configure --prefix="$SAGE_LOCAL" --enable-R-shlib --with-x=no
--with-readline="$SAGE_LOCAL" $OSXFW $SUN_FLAGS
fi

if [ $? -ne 0 ]; then


(I just removed the --with-recommended-packages=no switch from all
command lines)


Then I installed the new spkg with:

sage -f r-2.9.2.spkg

After a long build, I now get:

sage: r.installed_packages()

Package LibPath Version
Priority Bundle
base "base" "/home/jason/sage/local/lib/R//library" "2.9.2"
"base" NA
boot "boot" "/home/jason/sage/local/lib/R//library" "1.2-38"
"recommended" NA
class "class" "/home/jason/sage/local/lib/R//library" "7.2-48"
"recommended" "VR"
cluster "cluster" "/home/jason/sage/local/lib/R//library" "1.12.0"
"recommended" NA
codetools "codetools" "/home/jason/sage/local/lib/R//library" "0.2-2"
"recommended" NA
datasets "datasets" "/home/jason/sage/local/lib/R//library" "2.9.2"
"base" NA
foreign "foreign" "/home/jason/sage/local/lib/R//library" "0.8-37"
"recommended" NA
graphics "graphics" "/home/jason/sage/local/lib/R//library" "2.9.2"
"base" NA
grDevices "grDevices" "/home/jason/sage/local/lib/R//library" "2.9.2"
"base" NA
grid "grid" "/home/jason/sage/local/lib/R//library" "2.9.2"
"base" NA
KernSmooth "KernSmooth" "/home/jason/sage/local/lib/R//library" "2.23-2"
"recommended" NA
lattice "lattice" "/home/jason/sage/local/lib/R//library"
"0.17-25" "recommended" NA
MASS "MASS" "/home/jason/sage/local/lib/R//library" "7.2-48"
"recommended" "VR"
Matrix "Matrix" "/home/jason/sage/local/lib/R//library"
"0.999375-30" "recommended" NA
methods "methods" "/home/jason/sage/local/lib/R//library" "2.9.2"
"base" NA
mgcv "mgcv" "/home/jason/sage/local/lib/R//library" "1.5-5"
"recommended" NA
nlme "nlme" "/home/jason/sage/local/lib/R//library" "3.1-93"
"recommended" NA
nnet "nnet" "/home/jason/sage/local/lib/R//library" "7.2-48"
"recommended" "VR"
rpart "rpart" "/home/jason/sage/local/lib/R//library" "3.1-45"
"recommended" NA
spatial "spatial" "/home/jason/sage/local/lib/R//library" "7.2-48"
"recommended" "VR"
splines "splines" "/home/jason/sage/local/lib/R//library" "2.9.2"
"base" NA
stats "stats" "/home/jason/sage/local/lib/R//library" "2.9.2"
"base" NA
stats4 "stats4" "/home/jason/sage/local/lib/R//library" "2.9.2"
"base" NA
survival "survival" "/home/jason/sage/local/lib/R//library" "2.35-4"
"recommended" NA
tcltk "tcltk" "/home/jason/sage/local/lib/R//library" "2.9.2"
"base" NA
tools "tools" "/home/jason/sage/local/lib/R//library" "2.9.2"
"base" NA
utils "utils" "/home/jason/sage/local/lib/R//library" "2.9.2"
"base" NA
Contains Depends

base NA NA

boot NA "R (>= 2.7.0), graphics, stats"

class "MASS class nnet spatial" "R (>= 2.5.0), grDevices, graphics,
stats, utils"
cluster NA "R (>= 2.5.0), stats, graphics,
utils"
codetools NA "R (>= 2.1)"

datasets NA NA

foreign NA "R (>= 2.6.0), stats"

graphics NA NA

grDevices NA NA

grid NA NA

KernSmooth NA "R (>= 2.5.0), stats"

lattice NA "R (>= 2.5.0)"

MASS "MASS class nnet spatial" "R (>= 2.5.0), grDevices, graphics,
stats, utils"
Matrix NA "R (>= 2.9.0), stats, methods,
utils, lattice"
methods NA NA

mgcv NA "R (>= 2.3.0)"

nlme NA "graphics, stats, R (>= 2.4.0)"

nnet "MASS class nnet spatial" "R (>= 2.5.0), grDevices, graphics,
stats, utils"
rpart NA "R (>= 2.7.0), graphics, stats,
grDevices"
spatial "MASS class nnet spatial" "R (>= 2.5.0), grDevices, graphics,
stats, utils"
splines NA NA

stats NA NA

stats4 NA "methods, graphics, stats"

survival NA "stats, utils, graphics, splines, R
(>= 2.0.0)"
tcltk NA NA

tools NA NA

utils NA NA

Imports Suggests
Enhances
base NA NA
NA
boot NA "survival"
NA
class NA "lattice, nlme,
survival" NA
cluster NA NA
NA
codetools NA NA
NA
datasets NA NA
NA
foreign "methods, utils" NA
NA
graphics "grDevices" NA
NA
grDevices NA NA
NA
grid "grDevices" "lattice"
NA
KernSmooth NA "MASS"
NA
lattice "grid, grDevices, graphics, stats, utils" "grid, KernSmooth"
"chron"
MASS NA "lattice, nlme,
survival" NA
Matrix "graphics, lattice, grid, stats" NA
"graph, SparseM"
methods "utils" NA
NA
mgcv "graphics, stats, nlme" "nlme (>= 3.1-64),
splines" NA
nlme "lattice" NA
NA
nnet NA "lattice, nlme,
survival" NA
rpart NA "survival"
NA
spatial NA "lattice, nlme,
survival" NA
splines "graphics, stats" NA
NA
stats NA NA
NA
stats4 NA NA
NA
survival NA NA
NA
tcltk NA NA
NA
tools NA NA
NA
utils NA NA
NA
OS_type Built
base NA "2.9.2"
boot NA "2.9.2"
class NA "2.9.2"
cluster NA "2.9.2"
codetools NA "2.9.2"
datasets NA "2.9.2"
foreign NA "2.9.2"
graphics NA "2.9.2"
grDevices NA "2.9.2"
grid NA "2.9.2"
KernSmooth NA "2.9.2"
lattice NA "2.9.2"
MASS NA "2.9.2"
Matrix NA "2.9.2"
methods NA "2.9.2"
mgcv NA "2.9.2"
nlme NA "2.9.2"
nnet NA "2.9.2"
rpart NA "2.9.2"
spatial NA "2.9.2"
splines NA "2.9.2"
stats NA "2.9.2"
stats4 NA "2.9.2"
survival NA "2.9.2"
tcltk NA "2.9.2"
tools NA "2.9.2"
utils NA "2.9.2"




So it looks like MASS is installed. Do you know a command I can check
it with?

Thanks,

Jason


--
Jason Grout

Jason Grout

unread,
Nov 23, 2009, 10:10:56 PM11/23/09
to sage-s...@googlegroups.com
Jason Grout wrote:

>
> So it looks like MASS is installed. Do you know a command I can check
> it with?

Indeed, it appears that it works and loads the MASS library:


sage: import rpy2.rpy_classic as rpy
sage: r=rpy.r
sage: rpy.set_default_mode(rpy.BASIC_CONVERSION)
sage: r.library('MASS')

['MASS',
'stats',
'graphics',
'grDevices',
'utils',
'datasets',
'methods',
'base']
sage: r.DDT

[2.79,
2.9300000000000002,
3.2200000000000002,
3.7799999999999998,
3.2200000000000002,
3.3799999999999999,
3.1800000000000002,
3.3300000000000001,
3.3399999999999999,
3.0600000000000001,
3.0699999999999998,
3.5600000000000001,
3.0800000000000001,
4.6399999999999997,
3.3399999999999999]

(DDT is a dataset included in the MASS package; see
http://stat.ethz.ch/R-manual/R-patched/library/MASS/html/00Index.html)


Jason


--
Jason Grout

kcrisman

unread,
Nov 23, 2009, 11:40:28 PM11/23/09
to sage-support
Thanks for all this feedback. In the event, the specific reason I
wanted to have this was for a presentation where the idea would be one
could "just use" R, which has a lot of undergraduate resources/texts
available, not necessarily to plot my own histograms or use the new
stats - since, if history is a guide, I am not an optimal person to
say anything meaningful about or use statistics! But I still
appreciate the feedback, and definitely appreciate that it's possible
to load these packages after all, so thank you.

Not sure if it's optimal for Sage to include all these, of course, in
its R... but it still seems like it's a bug that
sage: r.install_packages('MASS')
doesn't work right off the bat. Maybe there should even be an
r.install_recommended_packages() command which would do that, since we
wouldn't want to include all that data in Sage.

Is there any way to avoid this piece, or is it just because
rpy_classic is better than rpy2?

sage: import rpy2.rpy_classic as rpy
sage: r=rpy.r
sage: rpy.set_default_mode(rpy.BASIC_CONVERSION)

Sorry for all the questions.

- kcrisman
> (DDT is a dataset included in the MASS package; seehttp://stat.ethz.ch/R-manual/R-patched/library/MASS/html/00Index.html)
>
> Jason
>
> --
> Jason Grout
Reply all
Reply to author
Forward
0 new messages