Skip to content

Commit

Permalink
Fixed some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaffo committed May 21, 2014
1 parent eea63ac commit d853fe1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 58 deletions.
Binary file modified 07_RegressionModels/03_04_bonus/fig/unnamed-chunk-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified 07_RegressionModels/03_04_bonus/fig/unnamed-chunk-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 17 additions & 30 deletions 07_RegressionModels/03_04_bonus/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,11 @@ <h2>Regression models</h2>

<!-- SLIDES -->
<slide class="" id="slide-1" style="background:;">
<hgroup>
<h2>How to fit functions using linear models</h2>
</hgroup>
<article data-timings="">
<pre><code>## Error: object &#39;opts_chunk&#39; not found
</code></pre>

<pre><code>## Error: object &#39;knit_hooks&#39; not found
</code></pre>

<pre><code>## Error: object &#39;knit_hooks&#39; not found
</code></pre>

<h2>How to fit functions using linear models</h2>

<ul>
<ul>
<li>Consider a model \(Y_i = f(X_i) + \epsilon\). </li>
<li>How can we fit such a model using linear models (called scatterplot smoothing)</li>
<li>Consider the model
Expand All @@ -80,18 +72,16 @@ <h2>How to fit functions using linear models</h2>
<h2>Simulated example</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">n &lt;- 500
x &lt;- seq(0, 4 * pi, length = n)
y &lt;- sin(x) + rnorm(n, sd = 0.3)
knots &lt;- seq(0, 8 * pi, length = 20)
<pre><code class="r">n &lt;- 500; x &lt;- seq(0, 4 * pi, length = n); y &lt;- sin(x) + rnorm(n, sd = .3)
knots &lt;- seq(0, 8 * pi, length = 20);
splineTerms &lt;- sapply(knots, function(knot) (x &gt; knot) * (x - knot))
xMat &lt;- cbind(1, x, splineTerms)
yhat &lt;- predict(lm(y ~ xMat - 1))
plot(x, y, frame = FALSE, pch = 21, bg = &quot;lightblue&quot;, cex = 2)
lines(x, yhat, col = &quot;red&quot;, lwd = 2)
</code></pre>

<p><img src="assets/fig/unnamed-chunk-1.png" alt="plot of chunk unnamed-chunk-1"> </p>
<div class="rimage center"><img src="fig/unnamed-chunk-1.png" title="plot of chunk unnamed-chunk-1" alt="plot of chunk unnamed-chunk-1" class="plot" /></div>

</article>
<!-- Presenter Notes -->
Expand Down Expand Up @@ -123,7 +113,7 @@ <h2>Adding squared terms</h2>
lines(x, yhat, col = &quot;red&quot;, lwd = 2)
</code></pre>

<p><img src="assets/fig/unnamed-chunk-2.png" alt="plot of chunk unnamed-chunk-2"> </p>
<div class="rimage center"><img src="fig/unnamed-chunk-2.png" title="plot of chunk unnamed-chunk-2" alt="plot of chunk unnamed-chunk-2" class="plot" /></div>

</article>
<!-- Presenter Notes -->
Expand Down Expand Up @@ -158,12 +148,10 @@ <h2>Notes</h2>
<h2>Harmonics using linear models</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">## Chord finder, playing the white keys on a piano from octave c4 - c5
notes4 &lt;- c(261.63, 293.66, 329.63, 349.23, 392, 440, 493.88, 523.25)
t &lt;- seq(0, 2, by = 0.001)
n &lt;- length(t)
c4 &lt;- sin(2 * pi * notes4[1] * t)
e4 &lt;- sin(2 * pi * notes4[3] * t)
<pre><code class="r">##Chord finder, playing the white keys on a piano from octave c4 - c5
notes4 &lt;- c(261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25)
t &lt;- seq(0, 2, by = .001); n &lt;- length(t)
c4 &lt;- sin(2 * pi * notes4[1] * t); e4 &lt;- sin(2 * pi * notes4[3] * t);
g4 &lt;- sin(2 * pi * notes4[5] * t)
chord &lt;- c4 + e4 + g4 + rnorm(n, 0, 0.3)
x &lt;- sapply(notes4, function(freq) sin(2 * pi * freq * t))
Expand All @@ -176,20 +164,19 @@ <h2>Harmonics using linear models</h2>

<slide class="" id="slide-7" style="background:;">
<article data-timings="">
<p><img src="assets/fig/unnamed-chunk-4.png" alt="plot of chunk unnamed-chunk-4"> </p>
<div class="rimage center"><img src="fig/unnamed-chunk-4.png" title="plot of chunk unnamed-chunk-4" alt="plot of chunk unnamed-chunk-4" class="plot" /></div>

</article>
<!-- Presenter Notes -->
</slide>

<slide class="" id="slide-8" style="background:;">
<article data-timings="">
<pre><code class="r">## (How you would really do it)
a &lt;- fft(chord)
plot(Re(a)^2, type = &quot;l&quot;)
<pre><code class="r">##(How you would really do it)
a &lt;- fft(chord); plot(Re(a)^2, type = &quot;l&quot;)
</code></pre>

<p><img src="assets/fig/unnamed-chunk-5.png" alt="plot of chunk unnamed-chunk-5"> </p>
<div class="rimage center"><img src="fig/unnamed-chunk-5.png" title="plot of chunk unnamed-chunk-5" alt="plot of chunk unnamed-chunk-5" class="plot" /></div>

</article>
<!-- Presenter Notes -->
Expand All @@ -201,7 +188,7 @@ <h2>Harmonics using linear models</h2>
<ul>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=1 title=''>
data-slide=1 title='How to fit functions using linear models'>
1
</a>
</li>
Expand Down
40 changes: 12 additions & 28 deletions 07_RegressionModels/03_04_bonus/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ widgets : [mathjax] # {mathjax, quiz, bootstrap}
mode : selfcontained # {standalone, draft}
---

```
## Error: object 'opts_chunk' not found
```

```
## Error: object 'knit_hooks' not found
```

```
## Error: object 'knit_hooks' not found
```


## How to fit functions using linear models
Expand All @@ -45,18 +34,16 @@ is continuous at the knot points.
## Simulated example

```r
n <- 500
x <- seq(0, 4 * pi, length = n)
y <- sin(x) + rnorm(n, sd = 0.3)
knots <- seq(0, 8 * pi, length = 20)
n <- 500; x <- seq(0, 4 * pi, length = n); y <- sin(x) + rnorm(n, sd = .3)
knots <- seq(0, 8 * pi, length = 20);
splineTerms <- sapply(knots, function(knot) (x > knot) * (x - knot))
xMat <- cbind(1, x, splineTerms)
yhat <- predict(lm(y ~ xMat - 1))
plot(x, y, frame = FALSE, pch = 21, bg = "lightblue", cex = 2)
lines(x, yhat, col = "red", lwd = 2)
```

![plot of chunk unnamed-chunk-1](assets/fig/unnamed-chunk-1.png)
<div class="rimage center"><img src="fig/unnamed-chunk-1.png" title="plot of chunk unnamed-chunk-1" alt="plot of chunk unnamed-chunk-1" class="plot" /></div>


---
Expand All @@ -77,7 +64,7 @@ plot(x, y, frame = FALSE, pch = 21, bg = "lightblue", cex = 2)
lines(x, yhat, col = "red", lwd = 2)
```

![plot of chunk unnamed-chunk-2](assets/fig/unnamed-chunk-2.png)
<div class="rimage center"><img src="fig/unnamed-chunk-2.png" title="plot of chunk unnamed-chunk-2" alt="plot of chunk unnamed-chunk-2" class="plot" /></div>


---
Expand All @@ -93,12 +80,10 @@ lines(x, yhat, col = "red", lwd = 2)
## Harmonics using linear models

```r
## Chord finder, playing the white keys on a piano from octave c4 - c5
notes4 <- c(261.63, 293.66, 329.63, 349.23, 392, 440, 493.88, 523.25)
t <- seq(0, 2, by = 0.001)
n <- length(t)
c4 <- sin(2 * pi * notes4[1] * t)
e4 <- sin(2 * pi * notes4[3] * t)
##Chord finder, playing the white keys on a piano from octave c4 - c5
notes4 <- c(261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25)
t <- seq(0, 2, by = .001); n <- length(t)
c4 <- sin(2 * pi * notes4[1] * t); e4 <- sin(2 * pi * notes4[3] * t);
g4 <- sin(2 * pi * notes4[5] * t)
chord <- c4 + e4 + g4 + rnorm(n, 0, 0.3)
x <- sapply(notes4, function(freq) sin(2 * pi * freq * t))
Expand All @@ -107,18 +92,17 @@ fit <- lm(chord ~ x - 1)


---
![plot of chunk unnamed-chunk-4](assets/fig/unnamed-chunk-4.png)
<div class="rimage center"><img src="fig/unnamed-chunk-4.png" title="plot of chunk unnamed-chunk-4" alt="plot of chunk unnamed-chunk-4" class="plot" /></div>


---

```r
## (How you would really do it)
a <- fft(chord)
plot(Re(a)^2, type = "l")
##(How you would really do it)
a <- fft(chord); plot(Re(a)^2, type = "l")
```

![plot of chunk unnamed-chunk-5](assets/fig/unnamed-chunk-5.png)
<div class="rimage center"><img src="fig/unnamed-chunk-5.png" title="plot of chunk unnamed-chunk-5" alt="plot of chunk unnamed-chunk-5" class="plot" /></div>



Expand Down

0 comments on commit d853fe1

Please sign in to comment.