« View all new features in
Mathematica
9
◄
previous
|
next
►
New in
Mathematica
9
›
Built-in Integration with R
Visualize Linear Model Fit
Fit data with R's linear model fitting function and visualize the result in
Mathematica
.
First, load and install the R runtime. Generate random data in
Mathematica
and perform a linear fit in R. Finally, use
Mathematica
to visualize the fit.
In[1]:=
X
Needs["RLink`"] InstallR[]
In[2]:=
X
x = Range[10]; y = 2*x + RandomReal[{0, 1}, 10];
In[3]:=
X
lmfit = RFunction["function(x,y){ lmfit <- lm(y~x) lmfit$coeff }"][x, y]
Out[3]=
In[4]:=
X
Show[{ListPlot[Transpose@{x, y}], Plot[lmfit[[1, 1]] + z*lmfit[[1, 2]], {z, x[[1]], x[[-1]]}, PlotStyle -> Pink]}]
Out[4]=