Skip to content

Commit

Permalink
Update Lecture 4-5
Browse files Browse the repository at this point in the history
Conclude Lecture 4 and Started lecture 5
  • Loading branch information
lacorreia65 committed Feb 8, 2023
1 parent 3c93721 commit 0604033
Show file tree
Hide file tree
Showing 43 changed files with 456 additions and 3,691 deletions.
408 changes: 204 additions & 204 deletions .Rhistory

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
12 changes: 6 additions & 6 deletions .Rproj.user/B584AE7E/pcs/windowlayoutstate.pper
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"left": {
"splitterpos": 128,
"splitterpos": 107,
"topwindowstate": "NORMAL",
"panelheight": 959,
"windowheight": 997
"panelheight": 803,
"windowheight": 841
},
"right": {
"splitterpos": 601,
"splitterpos": 503,
"topwindowstate": "NORMAL",
"panelheight": 959,
"windowheight": 997
"panelheight": 803,
"windowheight": 841
}
}
34 changes: 0 additions & 34 deletions .Rproj.user/B584AE7E/sources/per/t/18D958F7

This file was deleted.

Empty file.
28 changes: 0 additions & 28 deletions .Rproj.user/B584AE7E/sources/per/t/53D919D4

This file was deleted.

Empty file.
12 changes: 6 additions & 6 deletions .Rproj.user/B584AE7E/sources/per/t/77F68D30
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"path": "C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/25. Project 2022/Statistical-Thinking-2023/Statistical Thinking 2023_v2.qmd",
"project_path": "Statistical Thinking 2023_v2.qmd",
"type": "quarto_markdown",
"hash": "2945690342",
"hash": "1647904797",
"contents": "",
"dirty": false,
"created": 1673908998203.0,
Expand All @@ -15,18 +15,18 @@
"source_window_id": "",
"Source": "Source",
"docOutlineVisible": "1",
"rmdVisualCollapsedChunks": "12696",
"rmdVisualModeLocation": "14119:23446",
"cursorPosition": "734,0",
"rmdVisualCollapsedChunks": "",
"rmdVisualModeLocation": "16884:27202",
"cursorPosition": "924,0",
"scrollLine": "0",
"docOutlineSize": "118"
},
"folds": "",
"lastKnownWriteTime": 1675068098,
"lastKnownWriteTime": 1675828299,
"encoding": "UTF-8",
"collab_server": "",
"source_window": "",
"last_content_update": 1675068098355,
"last_content_update": 1675828299557,
"read_only": false,
"read_only_alternatives": []
}
115 changes: 115 additions & 0 deletions .Rproj.user/B584AE7E/sources/per/t/77F68D30-contents
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,118 @@ traceplot(fit_m4.6, pars = "sigma")
sigma_sample <- extract(fit_m4.6)[["sigma"]]
hist(sigma_sample)
```

### Cherry Blossoms example (pp.114) - Splines

```{r}
data(cherry_blossoms)
d <-cherry_blossoms
d2 <- d[ complete.cases(d$temp),]
precis(d2)
```

```{r}
?cherry_blossoms
d2 <-d[complete.cases(d$doy),]#completecasesondoy
num_knots <-15
knot_list <-quantile(d2$year,probs=seq(0,1,length.out=num_knots))
```

Building B (Block Variables)

```{r}
library(splines)
B <-bs(d2$year,
knots=knot_list[-c(1,num_knots)] ,
degree=3 ,intercept=TRUE)
```

```{r}
plot( NULL,xlim=range(d2$year),ylim=c(0,1),xlab="year",ylab="basis")
for (i in 1:ncol(B))lines(d2$year,B[,i])
```

```{r}
m4.7 <-quap(
alist(
D ~dnorm(mu,sigma),
mu <-a+B%*%w,
a ~dnorm(100,10),
w ~dnorm(0,10),
sigma ~dexp(1)
), data=list(D=d2$doy,B=B),
start=list( w=rep(0,ncol(B))))
```

```{r}
precis(m4.7)
```

```{r}
post <-extract.samples(m4.7)
w <-apply(post$w,2,mean)
#plot( NULL,xlim=range(d2$year),ylim=c(-6,6),
# xlab="year" ,ylab="basis*weight")
plot( d2$year,d2$doy-m4.7@optim$par[18],col=col.alpha(rangi2,0.3),pch=16,
xlim=range(d2$year),ylim=c(-26,26),
xlab="year" ,ylab="basis*weight")
for (i in 1:ncol(B))lines(d2$year,w[i]*B[,i])
```

```{r}
mu <-link(m4.7)
mu_PI <-apply(mu,2,PI,0.97)
plot( d2$year,d2$doy,col=col.alpha(rangi2,0.3),pch=16)
shade( mu_PI,d2$year,col=col.alpha("black",0.5))
```

```{r}
m4.7alt <-quap(
alist(
D ~dnorm(mu,sigma),
mu <-a+sapply(1:827,function(i)sum(B[i,]*w)),
a ~dnorm(100,1),
w ~dnorm(0,10),
sigma ~dexp(1)
),
data=list( D=d2$doy,B=B),
start=list( w=rep(0,ncol(B))))
```

Now using the B-Splines with the Weight x Height Data, we have the following:

```{r}
data(Howell1)
# d <-Howell1[Howell1$age>=18,]
d <-Howell1
```

## Lecture 5

```{r}
n <- 1000
Z <- rbern(n, .5)
X <- rbern(n, (1-Z)*.1+Z*.9)
Y <- rbern(n, (1-Z)*.1+Z*.9)
```

```{r}
cor(X,Y)
```

```{r}
cols <- c(4,2)

N <- 300
Z <- rbern(N)
X <- rnorm(N,2*Z-1)
Y <- rnorm(N,2*Z-1)

plot(X,Y, col=cols[Z+1], lwd=3)

abline(lm(Y[Z==1]~X[Z==1]), col=2, lwd=3)

abline(lm(Y[Z==0]~X[Z==0]), col=4, lwd=3)

abline(lm(Y~X), lwd=3)
```
28 changes: 0 additions & 28 deletions .Rproj.user/B584AE7E/sources/per/t/AC03FAA1

This file was deleted.

Empty file.
26 changes: 0 additions & 26 deletions .Rproj.user/B584AE7E/sources/per/t/FA83A758

This file was deleted.

Loading

0 comments on commit 0604033

Please sign in to comment.