Assign sage variable values into R objects via sagetex and Sweave
Posted
by sheed03
on Stack Overflow
See other posts from Stack Overflow
or by sheed03
Published on 2010-06-05T17:39:43Z
Indexed on
2010/06/05
17:42 UTC
Read the original article
Hit count: 271
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?
© Stack Overflow or respective owner