Update on r codes

This commit is contained in:
mhjensen
2017-12-08 11:32:08 +01:00
parent 3eaf9e4578
commit dd90513eb3
30 changed files with 1431 additions and 206 deletions
+40 -37
View File
@@ -145,7 +145,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Dec 6, 2017</h4></center> <!-- date -->
<center><h4>Dec 8, 2017</h4></center> <!-- date -->
<br>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
@@ -287,10 +287,13 @@ either Python or C++ as programming languages.
To add more entropy, <b>cython</b> can also be used when running your notebooks. It means that Python with the Jupyter/IPython notebook
setup allows you to integrate widely popular softwares and tools for scientific computing. With its versatility,
including symbolic operations, Python offers a unique computational environment. Your Jupyter/IPython notebook
can easily be converted into a nicely rendered <b>PDF</b> file or a Latex file for further processing.
can easily be converted into a nicely rendered <b>PDF</b> file or a Latex file for further processing. For example, convert to latex as
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>jupyter nbconvert filename<span style="color: #666666">.</span>ipynb <span style="color: #666666">--</span>to latex
</pre></div>
<p>
This never ends.
If you use the light mark-up language <b>doconce</b> you can convert a standard ascii text file into various HTML
formats, ipython notebooks, latex files, pdf files etc.
@@ -804,26 +807,26 @@ plt<span style="color: #666666">.</span>show()
<p>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>HudsonBay <span style="color: #666666">=</span> read<span style="color: #666666">.</span>csv(<span style="color: #BA2121">&quot;src/Hudson_Bay.csv&quot;</span>,header<span style="color: #666666">=</span>T)
<!-- code=r (!bc r) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>HudsonBay <span style="color: #666666">=</span> read.csv(<span style="color: #BA2121">&quot;src/Hudson_Bay.csv&quot;</span>,header<span style="color: #666666">=</span><span style="color: #008000">T</span>)
fix(HudsonBay)
dim(HudsonBay)
names(HudsonBay)
plot(HudsonBay$Year, HudsonBay$Hares<span style="color: #666666">..</span>x1000<span style="color: #666666">.</span>)
attach(HudsonBay)
plot(Year, Hares<span style="color: #666666">..</span>x1000<span style="color: #666666">.</span>)
plot(Year, Hares<span style="color: #666666">..</span>x1000<span style="color: #666666">.</span>, col<span style="color: #666666">=</span><span style="color: #BA2121">&quot;red&quot;</span>, varwidth<span style="color: #666666">=</span>T, xlab<span style="color: #666666">=</span><span style="color: #BA2121">&quot;Years&quot;</span>, ylab<span style="color: #666666">=</span><span style="color: #BA2121">&quot;Haresx 1000&quot;</span>)
summary(HudsonBay)
summary(Hares<span style="color: #666666">..</span>x1000<span style="color: #666666">.</span>)
library(MASS)
library(ISLR)
scatter<span style="color: #666666">.</span>smooth(x<span style="color: #666666">=</span>Year, y <span style="color: #666666">=</span> Hares<span style="color: #666666">..</span>x1000<span style="color: #666666">.</span>)
linearMod <span style="color: #666666">=</span> lm(Hares<span style="color: #666666">..</span>x1000<span style="color: #666666">.</span> <span style="color: #666666">~</span> Year)
<span style="color: #008000; font-weight: bold">print</span>(linearMod)
summary(linearMod)
<span style="color: #008000">dim</span>(HudsonBay)
<span style="color: #008000">names</span>(HudsonBay)
plot(HudsonBay<span style="color: #666666">$</span>Year, HudsonBay<span style="color: #666666">$</span>Hares..x1000.)
<span style="color: #008000; font-weight: bold">attach</span>(HudsonBay)
plot(Year, Hares..x1000.)
plot(Year, Hares..x1000., col<span style="color: #666666">=</span><span style="color: #BA2121">&quot;red&quot;</span>, varwidth<span style="color: #666666">=</span><span style="color: #008000">T</span>, xlab<span style="color: #666666">=</span><span style="color: #BA2121">&quot;Years&quot;</span>, ylab<span style="color: #666666">=</span><span style="color: #BA2121">&quot;Haresx 1000&quot;</span>)
<span style="color: #008000">summary</span>(HudsonBay)
<span style="color: #008000">summary</span>(Hares..x1000.)
<span style="color: #008000; font-weight: bold">library</span>(MASS)
<span style="color: #008000; font-weight: bold">library</span>(ISLR)
scatter.smooth(x<span style="color: #666666">=</span>Year, y <span style="color: #666666">=</span> Hares..x1000.)
linearMod <span style="color: #666666">=</span> lm(Hares..x1000. <span style="color: #666666">~</span> Year)
<span style="color: #008000">print</span>(linearMod)
<span style="color: #008000">summary</span>(linearMod)
plot(linearMod)
confint(linearMod)
predict(linearMod,data<span style="color: #666666">.</span>frame(Year<span style="color: #666666">=</span>c(<span style="color: #666666">1910</span>,<span style="color: #666666">1914</span>,<span style="color: #666666">1920</span>)),interval<span style="color: #666666">=</span><span style="color: #BA2121">&quot;confidence&quot;</span>)
predict(linearMod,<span style="color: #B00040">data.frame</span>(Year<span style="color: #666666">=</span><span style="color: #B00040">c</span>(<span style="color: #666666">1910</span>,<span style="color: #666666">1914</span>,<span style="color: #666666">1920</span>)),interval<span style="color: #666666">=</span><span style="color: #BA2121">&quot;confidence&quot;</span>)
</pre></div>
</div>
@@ -838,26 +841,26 @@ predict(linearMod,data<span style="color: #666666">.</span>frame(Year<span style
<p>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000">set</span><span style="color: #666666">.</span>seed(<span style="color: #666666">1485</span>)
<span style="color: #008000">len</span> <span style="color: #666666">=</span> <span style="color: #666666">24</span>
x <span style="color: #666666">=</span> runif(<span style="color: #008000">len</span>)
y <span style="color: #666666">=</span> x<span style="color: #666666">^3+</span>rnorm(<span style="color: #008000">len</span>, <span style="color: #666666">0</span>,<span style="color: #666666">0.06</span>)
ds <span style="color: #666666">=</span> data<span style="color: #666666">.</span>frame(x <span style="color: #666666">=</span> x, y <span style="color: #666666">=</span> y)
<span style="color: #008000">str</span>(ds)
<!-- code=r (!bc r) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000">set.seed</span>(<span style="color: #666666">1485</span>)
len <span style="color: #666666">=</span> <span style="color: #666666">24</span>
x <span style="color: #666666">=</span> runif(len)
y <span style="color: #666666">=</span> x<span style="color: #666666">^3+</span>rnorm(len, <span style="color: #666666">0</span>,<span style="color: #666666">0.06</span>)
ds <span style="color: #666666">=</span> <span style="color: #B00040">data.frame</span>(x <span style="color: #666666">=</span> x, y <span style="color: #666666">=</span> y)
str(ds)
plot( y <span style="color: #666666">~</span> x, main <span style="color: #666666">=</span><span style="color: #BA2121">&quot;Known cubic with noise&quot;</span>)
s <span style="color: #666666">=</span> seq(<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,length <span style="color: #666666">=100</span>)
s <span style="color: #666666">=</span> <span style="color: #008000">seq</span>(<span style="color: #666666">0</span>,<span style="color: #666666">1</span>,length <span style="color: #666666">=100</span>)
lines(s, s<span style="color: #666666">^3</span>, lty <span style="color: #666666">=2</span>, col <span style="color: #666666">=</span><span style="color: #BA2121">&quot;green&quot;</span>)
m <span style="color: #666666">=</span> nls(y <span style="color: #666666">~</span> I(x<span style="color: #666666">^</span>power), data <span style="color: #666666">=</span> ds, start <span style="color: #666666">=</span> <span style="color: #008000">list</span>(power<span style="color: #666666">=1</span>), trace <span style="color: #666666">=</span> T)
class(m)
summary(m)
power <span style="color: #666666">=</span> <span style="color: #008000">round</span>(summary(m)$coefficients[<span style="color: #666666">1</span>], <span style="color: #666666">3</span>)
power<span style="color: #666666">.</span>se <span style="color: #666666">=</span> <span style="color: #008000">round</span>(summary(m)$coefficients[<span style="color: #666666">2</span>], <span style="color: #666666">3</span>)
m <span style="color: #666666">=</span> nls(y <span style="color: #666666">~</span> <span style="color: #008000">I</span>(x<span style="color: #666666">^</span>power), data <span style="color: #666666">=</span> ds, start <span style="color: #666666">=</span> <span style="color: #B00040">list</span>(power<span style="color: #666666">=1</span>), trace <span style="color: #666666">=</span> <span style="color: #008000">T</span>)
<span style="color: #008000">class</span>(m)
<span style="color: #008000">summary</span>(m)
power <span style="color: #666666">=</span> <span style="color: #008000">round</span>(<span style="color: #008000">summary</span>(m)<span style="color: #666666">$</span>coefficients[<span style="color: #666666">1</span>], <span style="color: #666666">3</span>)
power.se <span style="color: #666666">=</span> <span style="color: #008000">round</span>(<span style="color: #008000">summary</span>(m)<span style="color: #666666">$</span>coefficients[<span style="color: #666666">2</span>], <span style="color: #666666">3</span>)
plot(y <span style="color: #666666">~</span> x, main <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;Fitted power model&quot;</span>, sub <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;Blue: fit; green: known&quot;</span>)
s <span style="color: #666666">=</span> seq(<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, length <span style="color: #666666">=</span> <span style="color: #666666">100</span>)
s <span style="color: #666666">=</span> <span style="color: #008000">seq</span>(<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, length <span style="color: #666666">=</span> <span style="color: #666666">100</span>)
lines(s, s<span style="color: #666666">^3</span>, lty <span style="color: #666666">=</span> <span style="color: #666666">2</span>, col <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;green&quot;</span>)
lines(s, predict(m, <span style="color: #008000">list</span>(x <span style="color: #666666">=</span> s)), lty <span style="color: #666666">=</span> <span style="color: #666666">1</span>, col <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;blue&quot;</span>)
text(<span style="color: #666666">0</span>, <span style="color: #666666">0.5</span>, paste(<span style="color: #BA2121">&quot;y =x^ (&quot;</span>, power, <span style="color: #BA2121">&quot; +/- &quot;</span>, power<span style="color: #666666">.</span>se, <span style="color: #BA2121">&quot;)&quot;</span>, sep <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;&quot;</span>), pos <span style="color: #666666">=</span> <span style="color: #666666">4</span>)
lines(s, predict(m, <span style="color: #B00040">list</span>(x <span style="color: #666666">=</span> s)), lty <span style="color: #666666">=</span> <span style="color: #666666">1</span>, col <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;blue&quot;</span>)
text(<span style="color: #666666">0</span>, <span style="color: #666666">0.5</span>, <span style="color: #008000">paste</span>(<span style="color: #BA2121">&quot;y =x^ (&quot;</span>, power, <span style="color: #BA2121">&quot; +/- &quot;</span>, power.se, <span style="color: #BA2121">&quot;)&quot;</span>, sep <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;&quot;</span>), pos <span style="color: #666666">=</span> <span style="color: #666666">4</span>)
</pre></div>
</div>