tags:

views:

51

answers:

1

I am writing a short Sweave document that outputs into a Beamer presentation, in which I am using the sagetex package to solve an equation for two parameters in the beta binomial distribution, and I need to assign the parameter values into the R session so I can do additional processing on those values. The following code excerpt shows how I am interacting with sage:

<<echo=false,results=hide>>=
mean.raw <- c(5, 3.5, 2)
theta <- 0.5  
var.raw <- mean.raw + ((mean.raw^2)/theta)
@

\begin{frame}[fragile]
\frametitle{Test of Sage 2}
\begin{sagesilent}
var('a1, b1, a2, b2, a3, b3')
eqn1 = [1000*a1/(a1+b1)==\Sexpr{mean.raw[1]}, ((1000*a1*b1)*(1000+a1+b1))/((a1+b1)^2*(a1+b1+1))==\Sexpr{var.raw[1]}]
eqn2 = [1000*a2/(a2+b2)==\Sexpr{mean.raw[2]}, ((1000*a2*b2)*(1000+a2+b2))/((a2+b2)^2*(a2+b2+1))==\Sexpr{var.raw[2]}]
eqn3 = [1000*a3/(a3+b3)==\Sexpr{mean.raw[3]}, ((1000*a3*b3)*(1000+a3+b3))/((a3+b3)^2*(a3+b3+1))==\Sexpr{var.raw[3]}]
s1 = solve(eqn1, a1,b1)
s2 = solve(eqn2, a2,b2)
s3 = solve(eqn3, a3,b3)
\end{sagesilent}

Solutions of Beta Binomial Parameters:
\begin{itemize}
\item $\sage{s1[0]}$
\item $\sage{s2[0]}$
\item $\sage{s3[0]}$
\end{itemize}
\end{frame}

Everything compiles just fine, and in that slide I am able to see the solutions to the three equations respective parameters in that itemized list (for example the first item in the itemized list from that beamer slide is outputted as [a1=(328/667), b1=(65272/667)] (I am not able to post an image of the beamer slide but I hope you get the idea).

I would like to save the parameter values a1,b1,a2,b2,a3,b3 into R objects so that I can use them in simulations. I cannot find any documentation in the sagetex package on how to save output from sage commands into variables for use with other programs (in this case R). Any suggestions on how to get these values into R?

A: 

Wow, you are really mixing two worlds ;)

The only idea I can give you is the "solution_dict=True" parameter for the solve command. Then you get a Python dictionary whicht might help you to just output the value. But I have no idea what exactly Sweave does and when which step of the process rewrites what.

In general, it might be better if you would write it in sagetex only and call R via the rpy2 Python wrapper. But that might be too much work for you - maybe just for one slide and then stitch them together via some pdf-merging?

Harald Schilly