adding more code examples
This commit is contained in:
@@ -74,33 +74,33 @@ div { text-align: justify; text-justify: inter-word; }
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Predator-Prey model from ecology', 2, None, '___sec6'),
|
||||
('Case study from Hudson bay', 2, None, '___sec7'),
|
||||
('Hudson bay data', 2, None, '___sec8'),
|
||||
('Plotting the data', 2, None, '___sec9'),
|
||||
('Non-Linear Least squares in R', 2, None, '___sec6'),
|
||||
('Predator-Prey model from ecology', 2, None, '___sec7'),
|
||||
('Case study from Hudson bay', 2, None, '___sec8'),
|
||||
('Hudson bay data', 2, None, '___sec9'),
|
||||
('Plotting the data', 2, None, '___sec10'),
|
||||
('Hares and lynx in Hudson bay from 1900 to 1920',
|
||||
2,
|
||||
None,
|
||||
'___sec10'),
|
||||
'___sec11'),
|
||||
('Why now create a computer model for the hare and lynx '
|
||||
'populations?',
|
||||
2,
|
||||
None,
|
||||
'___sec11'),
|
||||
('The traditional (top-down) approach', 2, None, '___sec12'),
|
||||
('Basic mathematics notation', 2, None, '___sec13'),
|
||||
'___sec12'),
|
||||
('The traditional (top-down) approach', 2, None, '___sec13'),
|
||||
('Basic mathematics notation', 2, None, '___sec14'),
|
||||
('Basic dynamics of the population of hares',
|
||||
2,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Basic dynamics of the population of lynx', 2, None, '___sec15'),
|
||||
('Evolution equations', 2, None, '___sec16'),
|
||||
('Adapt the model to the Hudson Bay case', 2, None, '___sec17'),
|
||||
('The program', 2, None, '___sec18'),
|
||||
('The plot', 2, None, '___sec19'),
|
||||
('Linear regression in Python', 2, None, '___sec20'),
|
||||
('Linear Least squares in R', 2, None, '___sec21'),
|
||||
('Non-Linear Least squares in R', 2, None, '___sec22'),
|
||||
'___sec15'),
|
||||
('Basic dynamics of the population of lynx', 2, None, '___sec16'),
|
||||
('Evolution equations', 2, None, '___sec17'),
|
||||
('Adapt the model to the Hudson Bay case', 2, None, '___sec18'),
|
||||
('The program', 2, None, '___sec19'),
|
||||
('The plot', 2, None, '___sec20'),
|
||||
('Linear regression in Python', 2, None, '___sec21'),
|
||||
('Linear Least squares in R', 2, None, '___sec22'),
|
||||
('Example: ecoli lab experiment', 2, None, '___sec23'),
|
||||
('The program', 2, None, '___sec24'),
|
||||
('The output', 2, None, '___sec25'),
|
||||
@@ -156,7 +156,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>May 28, 2018</h4></center> <!-- date -->
|
||||
<center><h4>May 29, 2018</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -186,16 +186,20 @@ then use machine learning algorithms included in for example
|
||||
<b>scikit-learn</b>.
|
||||
|
||||
<p>
|
||||
These examples will serve us the purpose of getting started. Furthermore, they
|
||||
allow us to catch more than two birds with a stone. They will allow us
|
||||
to bring in some programming specific topics and tools as well as
|
||||
showing the power of various Python (and R) packages for machine
|
||||
learning and statistical data analysis. In the lectures on linear
|
||||
algebra we cover in more detail various programming features of languages like Python and C++ (and other), we will also look into more specific linear functions which
|
||||
are relevant for the various algorithms we will discuss. Here, we will
|
||||
mainly focus on two specific Python packages for Machine Learning,
|
||||
scikit-learn and tensorflow (see below for links etc).
|
||||
Moreover, the examples we introduce will serve as inputs to many of our discussions later, as well as allowing you to set up models and produce your own data and get started with programming.
|
||||
These examples will serve us the purpose of getting
|
||||
started. Furthermore, they allow us to catch more than two birds with
|
||||
a stone. They will allow us to bring in some programming specific
|
||||
topics and tools as well as showing the power of various Python (and
|
||||
R) packages for machine learning and statistical data analysis. In the
|
||||
lectures on linear algebra we cover in more detail various programming
|
||||
features of languages like Python and C++ (and other), we will also
|
||||
look into more specific linear functions which are relevant for the
|
||||
various algorithms we will discuss. Here, we will mainly focus on two
|
||||
specific Python packages for Machine Learning, scikit-learn and
|
||||
tensorflow (see below for links etc). Moreover, the examples we
|
||||
introduce will serve as inputs to many of our discussions later, as
|
||||
well as allowing you to set up models and produce your own data and
|
||||
get started with programming.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -580,7 +584,85 @@ years etc.
|
||||
|
||||
<p>
|
||||
We will discuss in more
|
||||
detail these and more function in the various lectures.
|
||||
detail these and other functions in the various lectures. We conclude this part with another example. Instead of
|
||||
a linear \( x \)-dependence we study now a cubic polynomial and use the polynomial regression analysis tools of scikit-learn.
|
||||
|
||||
<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; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> Ridge
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> PolynomialFeatures
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.pipeline</span> <span style="color: #008000; font-weight: bold">import</span> make_pipeline
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">f</span>(x):
|
||||
<span style="color: #BA2121; font-style: italic">""" function to approximate by polynomial interpolation"""</span>
|
||||
<span style="color: #008000; font-weight: bold">return</span> x<span style="color: #666666">*</span>x<span style="color: #666666">*</span>x
|
||||
|
||||
<span style="color: #408080; font-style: italic"># generate points used to plot </span>
|
||||
x_plot <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, <span style="color: #666666">10</span>, <span style="color: #666666">100</span>)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># generate points and keep a subset of them </span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, <span style="color: #666666">10</span>, <span style="color: #666666">100</span>)
|
||||
rng <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>RandomState(<span style="color: #666666">0</span>)
|
||||
rng<span style="color: #666666">.</span>shuffle(x)
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sort(x[:<span style="color: #666666">20</span>])
|
||||
y <span style="color: #666666">=</span> f(x)
|
||||
<span style="color: #408080; font-style: italic"># create matrix versions of these arrays </span>
|
||||
X <span style="color: #666666">=</span> x[:, np<span style="color: #666666">.</span>newaxis]
|
||||
X_plot <span style="color: #666666">=</span> x_plot[:, np<span style="color: #666666">.</span>newaxis]
|
||||
|
||||
colors <span style="color: #666666">=</span> [<span style="color: #BA2121">'teal'</span>, <span style="color: #BA2121">'yellowgreen'</span>, <span style="color: #BA2121">'gold'</span>]
|
||||
lw <span style="color: #666666">=</span> <span style="color: #666666">2</span>
|
||||
plt<span style="color: #666666">.</span>plot(x_plot, f(x_plot), color<span style="color: #666666">=</span><span style="color: #BA2121">'cornflowerblue'</span>, linewidth<span style="color: #666666">=</span>lw,
|
||||
label<span style="color: #666666">=</span><span style="color: #BA2121">"ground truth"</span>)
|
||||
plt<span style="color: #666666">.</span>scatter(x, y, color<span style="color: #666666">=</span><span style="color: #BA2121">'navy'</span>, s<span style="color: #666666">=30</span>, marker<span style="color: #666666">=</span><span style="color: #BA2121">'o'</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">"training points"</span>)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">for</span> count, degree <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>([<span style="color: #666666">3</span>, <span style="color: #666666">4</span>, <span style="color: #666666">5</span>]):
|
||||
model <span style="color: #666666">=</span> make_pipeline(PolynomialFeatures(degree), Ridge())
|
||||
model<span style="color: #666666">.</span>fit(X, y)
|
||||
y_plot <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(X_plot)
|
||||
plt<span style="color: #666666">.</span>plot(x_plot, y_plot, color<span style="color: #666666">=</span>colors[count], linewidth<span style="color: #666666">=</span>lw,
|
||||
label<span style="color: #666666">=</span><span style="color: #BA2121">"degree </span><span style="color: #BB6688; font-weight: bold">%d</span><span style="color: #BA2121">"</span> <span style="color: #666666">%</span> degree)
|
||||
|
||||
plt<span style="color: #666666">.</span>legend(loc<span style="color: #666666">=</span><span style="color: #BA2121">'lower left'</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec6">Non-Linear Least squares in R </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
<p>
|
||||
|
||||
<!-- 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">"Known cubic with noise"</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">"green"</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">"Fitted power model"</span>, sub <span style="color: #666666">=</span> <span style="color: #BA2121">"Blue: fit; green: known"</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">"green"</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">"blue"</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">"y =x^ ("</span>, power, <span style="color: #BA2121">" +/- "</span>, power.se, <span style="color: #BA2121">")"</span>, sep <span style="color: #666666">=</span> <span style="color: #BA2121">""</span>), pos <span style="color: #666666">=</span> <span style="color: #666666">4</span>)
|
||||
</pre></div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<p>
|
||||
Another useful Python package is
|
||||
@@ -600,7 +682,7 @@ display(data_pandas)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec6">Predator-Prey model from ecology </h2>
|
||||
<h2 id="___sec7">Predator-Prey model from ecology </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -624,7 +706,7 @@ scientific method:
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec7">Case study from Hudson bay </h2>
|
||||
<h2 id="___sec8">Case study from Hudson bay </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -645,7 +727,7 @@ Here we start by
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec8">Hudson bay data </h2>
|
||||
<h2 id="___sec9">Hudson bay data </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -694,7 +776,7 @@ One reason that this particular system has been so extensively studied is that t
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec9">Plotting the data </h2>
|
||||
<h2 id="___sec10">Plotting the data </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -730,7 +812,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec10">Hares and lynx in Hudson bay from 1900 to 1920 </h2>
|
||||
<h2 id="___sec11">Hares and lynx in Hudson bay from 1900 to 1920 </h2>
|
||||
|
||||
<p>
|
||||
<br /><br /><center><p><img src="fig/Hudson_Bay_data.png" align="bottom" width=700></p></center><br /><br />
|
||||
@@ -738,7 +820,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec11">Why now create a computer model for the hare and lynx populations? </h2>
|
||||
<h2 id="___sec12">Why now create a computer model for the hare and lynx populations? </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -771,7 +853,7 @@ climate and other complicating factors. How significant are these?
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec12">The traditional (top-down) approach </h2>
|
||||
<h2 id="___sec13">The traditional (top-down) approach </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -804,7 +886,7 @@ ODEs</em> (which cannot be solved)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec13">Basic mathematics notation </h2>
|
||||
<h2 id="___sec14">Basic mathematics notation </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -824,7 +906,7 @@ ODEs</em> (which cannot be solved)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec14">Basic dynamics of the population of hares </h2>
|
||||
<h2 id="___sec15">Basic dynamics of the population of hares </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -852,7 +934,7 @@ $$ \Delta H = a\Delta t H^n - b \Delta t H^nL^n$$
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec15">Basic dynamics of the population of lynx </h2>
|
||||
<h2 id="___sec16">Basic dynamics of the population of lynx </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -881,7 +963,7 @@ $$ \Delta L = d\Delta t H^nL^n - c\Delta t L^n$$
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec16">Evolution equations </h2>
|
||||
<h2 id="___sec17">Evolution equations </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -910,7 +992,7 @@ Note:
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec17">Adapt the model to the Hudson Bay case </h2>
|
||||
<h2 id="___sec18">Adapt the model to the Hudson Bay case </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -933,7 +1015,7 @@ Note:
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec18">The program </h2>
|
||||
<h2 id="___sec19">The program </h2>
|
||||
|
||||
<p>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -993,7 +1075,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec19">The plot </h2>
|
||||
<h2 id="___sec20">The plot </h2>
|
||||
|
||||
<p>
|
||||
<br /><br /><center><p><img src="fig/Hudson_Bay_sim.png" align="bottom" width=700></p></center><br /><br />
|
||||
@@ -1004,7 +1086,7 @@ If we perform a least-square fitting, we can find optimal values for the paramet
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec20">Linear regression in Python </h2>
|
||||
<h2 id="___sec21">Linear regression in Python </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -1037,7 +1119,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec21">Linear Least squares in R </h2>
|
||||
<h2 id="___sec22">Linear Least squares in R </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -1068,40 +1150,6 @@ predict(linearMod,<span style="color: #B00040">data.frame</span>(Year<span style
|
||||
</div>
|
||||
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec22">Non-Linear Least squares in R </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
<p>
|
||||
|
||||
<!-- 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">"Known cubic with noise"</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">"green"</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">"Fitted power model"</span>, sub <span style="color: #666666">=</span> <span style="color: #BA2121">"Blue: fit; green: known"</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">"green"</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">"blue"</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">"y =x^ ("</span>, power, <span style="color: #BA2121">" +/- "</span>, power.se, <span style="color: #BA2121">")"</span>, sep <span style="color: #666666">=</span> <span style="color: #BA2121">""</span>), pos <span style="color: #666666">=</span> <span style="color: #666666">4</span>)
|
||||
</pre></div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user