The Art of R Programming

(WallPaper) #1

consider a call to the R linear model functionlm(). In our example, we will
predictbfroma.





a = [5,12,13]
b = [10,28,30]
lmout = r.lm('v2 ~ v1',data=r.data_frame(v1=a,v2=b))





This is somewhat more complex than it would have been if done directly
in R. What are the issues here?
First, since Python syntax does not include the tilde character, we needed
to specify the model formula via a string. Since this is done in R anyway, this
is not a major departure.
Second, we needed a data frame to contain our data. We created one
using R’sdata.frame()function. In order to form a period in an R function
name, we need to use an underscore on the Python end. Thus we called
r.data_frame(). Note that in this call, we named the columns of our data
framev1andv2and then used these in our model formula.
The output object is a Python dictionary (analog of R’slisttype), as you
can see here (in part):





lmout
{'qr': {'pivot': [1, 2], 'qr': array([[ -1.73205081, -17.32050808],
[ 0.57735027, -6.164414 ],
[ 0.57735027, 0.78355007]]), 'qraux':





You should recognize the various attributes oflm()objects here. For
example, the coefficients of the fitted regression line, which would be con-
tained inlmout$coefficientsif this were done in R, are here in Python as
lmout['coefficients']. So, you can access those coefficients accordingly, for
example like this:





lmout['coefficients']
{'v1': 2.5263157894736841, '(Intercept)': -2.5964912280701729}
lmout['coefficients']['v1']
2.5263157894736841





You can also submit R commands to work on variables in R’s namespace,
using the functionr(). This is convenient if there are many syntax clashes.
Here is how we could run thewireframe()example in Section 12.4 in RPy:





r.library('lattice')
r.assign('a',a)
r.assign('b',b)
r('g <- expand.grid(a,b)')
r('g$Var3 <- g$Var1^2 + g$Var1*g$Var2')
r('wireframe(Var3 ~ Var1+Var2,g)')
r('plot(wireframe(Var3 ~ Var1+Var2,g))')





Interfacing R to Other Languages 331
Free download pdf