Skip to content

Commit

Permalink
Update Lec4-01
Browse files Browse the repository at this point in the history
Update Lecture 4 - Examples
  • Loading branch information
lacorreia65 committed Jan 19, 2023
1 parent 534de1c commit cb57df6
Show file tree
Hide file tree
Showing 64 changed files with 1,909 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .Rproj.user/B584AE7E/sources/prop/8C7CB8DC
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"source_window_id": "",
"Source": "Source"
}
4 changes: 2 additions & 2 deletions .Rproj.user/B584AE7E/sources/prop/EA93BB00
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"Source": "Source",
"docOutlineVisible": "1",
"rmdVisualCollapsedChunks": "",
"rmdVisualModeLocation": "7168:12914",
"cursorPosition": "365,0",
"rmdVisualModeLocation": "10046:19198",
"cursorPosition": "527,0",
"scrollLine": "0"
}
1 change: 1 addition & 0 deletions .Rproj.user/B584AE7E/sources/prop/INDEX
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
C%3A%2FUsers%2Flacor%2FOneDrive%2F100.%20Personal%2FSelfDevelopment%2F20.%20Project%202019%2FStockMarket%2FAsset_Analysis_v23.Rmd="8C7CB8DC"
C%3A%2FUsers%2Flacor%2FOneDrive%2F100.%20Personal%2FSelfDevelopment%2F25.%20Project%202022%2FStatistical-Thinking-2023%2FStatistical%20Thinking%202023_v2.qmd="EA93BB00"
Empty file.
Empty file.
10 changes: 5 additions & 5 deletions .Rproj.user/B584AE7E/sources/session-605e3cd7/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": "1278555502",
"hash": "1895704522",
"contents": "",
"dirty": false,
"created": 1673908998203.0,
Expand All @@ -16,16 +16,16 @@
"Source": "Source",
"docOutlineVisible": "1",
"rmdVisualCollapsedChunks": "",
"rmdVisualModeLocation": "7168:12914",
"cursorPosition": "365,0",
"rmdVisualModeLocation": "10046:19198",
"cursorPosition": "527,0",
"scrollLine": "0"
},
"folds": "",
"lastKnownWriteTime": 1674158939,
"lastKnownWriteTime": 1674170667,
"encoding": "UTF-8",
"collab_server": "",
"source_window": "",
"last_content_update": 1674158939323,
"last_content_update": 1674170667303,
"read_only": false,
"read_only_alternatives": []
}
159 changes: 159 additions & 0 deletions .Rproj.user/B584AE7E/sources/session-605e3cd7/77F68D30-contents
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ true_pos <- sapply(p, function (q) (ptr*q)/(ptr*q+(1-ptr)*(1-q)))
plot(true_pos, p, col=2, lwd=3)
```

### Intermezzo - Central Limit Theorem convergence

```{r}
n=20
logYs = -9.89
Expand Down Expand Up @@ -390,3 +392,160 @@ plot(theta_vec,post_approx,xlim = c(0,6), ylab = "Density", xlab = "theta", typ
abline(v=theta_hat, col = 'red')
#dev.off()
```

## Lecture 4

### Workflow

```{r}
library(rethinking)
library(tidyverse)
library(dplyr)

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

```{r}
d |>
ggplot(aes(x=height, y=weight, color=factor(male)))+
geom_point()+
theme_bw()

d |>
ggplot(aes(x=weight))+
geom_density(aes(color=factor(male)))+
theme_bw()

d |>
ggplot(aes(x=height))+
geom_density(aes(color=factor(male)))+
theme_bw()
```

```{r}
# S = 1: female, S=2: male
sim_HW <- function(S, b, a) {
N <- length(S)
H <- ifelse(S==1,150,160)+rnorm(N,0,5)
W <- a[S] + b[S]*H + rnorm(N, 0, 5)
data.frame(S,H,W)
}
```

Simulating our data

```{r}
S <- rbern(100)+1
dat <- sim_HW(S, b=c(.5,.6), a=c(0,0))
head(dat)

dat |>
ggplot(aes(x=H, y=W, color=factor(S)))+
geom_point()+
theme_bw()
```

### Testing the Model

```{r}
# female sample
S <- rep(1,100)
simF <- sim_HW(S, b=c(.5,.6), a=c(0,0))

# male sample
S <- rep(2,100)
simM <- sim_HW(S, b=c(.5,.6), a=c(0,0))

# effect of sex (male-female)
mean(simM$W-simF$W)

```

### Determine the Estimator of our model

```{r}
S <- rbern(100)+1
dat <- sim_HW(S, b=c(.5,.6), a=c(0,0))

# estimate posterior
m_SW <- quap(
alist(
W~dnorm(mu,sigma),
mu <- a[S],
a[S] ~ dnorm(60,10),
sigma ~ dunif(0,10)
), data=dat
)
precis(m_SW,depth=2)
```

### Running our model Over the Data

```{r}
# recoding
dat <- list(
W = d$weight,
S = d$male+1
)
# estimate posterior
m_SW <- quap(
alist(
W~dnorm(mu,sigma),
mu <- a[S],
a[S] ~ dnorm(60,10),
sigma ~ dunif(0,10)
), data=dat
)
precis(m_SW,depth=2)
```

```{r}
post <- extract.samples(m_SW)
dens(post$a[,1],xlim=c(35, 55),lwd=3,col=2,xlab="posterior mean weight(kg")
dens(post$a[,2], lwd=3,col=4,add=TRUE)
```

Simulating the weights using the posteriors obtrained

```{r}
W1 <- rnorm(1000,post$a[,1], post$sigma)
W2 <- rnorm(1000,post$a[,2], post$sigma)
dens(W1,xlim=c(20,70),ylim=c(0,.085),lwd=3, col=2)
dens(W2,lwd=3, col=4, add=TRUE)

```

### Computing the Contrast

Now we need to compute the **contrast distribution**, the difference between the categories

```{r}
mu_contrast <- post$a[,2]-post$a[,1]
dens(mu_contrast,xlim=c(3,10),lwd=3, col=1,xlab="posterior mean weight contrast(kg)")
```

```{r}
W1 <- rnorm(1000,post$a[,1], post$sigma)
W2 <- rnorm(1000,post$a[,2], post$sigma)

# conntrast
W_contrast <- W2 - W1
dens(W_contrast,xlim=c(-25,35),lwd=3, col=1,
xlab="posterior weight contrast (kg)")

W_up <- sum(W_contrast>0)/1000
W_down <- sum(W_contrast<0)/1000
cat("W_up:",W_up," and W_down:",W_down)

df <- data.frame (
W1 = W1,
W2 = W2,
W_contrast = W_contrast,
W_flag = factor(W_contrast>W_up)
)

ggplot(df, aes(x=W_contrast,fill=W_flag))+
geom_density()+
theme_bw()
```
34 changes: 34 additions & 0 deletions .Rproj.user/B584AE7E/sources/session-605e3cd7/884F836F
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"id": "884F836F",
"path": null,
"project_path": null,
"type": "r_dataframe",
"hash": "0",
"contents": "",
"dirty": false,
"created": 1674169732483.0,
"source_on_save": false,
"relative_order": 4,
"properties": {
"expression": "df",
"caption": "df",
"totalObservations": "1000",
"displayedObservations": "1000",
"variables": "4",
"cacheKey": "993CD89E",
"object": "df",
"environment": "",
"contentUrl": "grid_resource/gridviewer.html?env=&obj=df&cache_key=993CD89E&max_cols=50",
"preview": "0",
"source_window_id": "",
"Source": "Source"
},
"folds": "",
"lastKnownWriteTime": 2195971685464,
"encoding": "",
"collab_server": "",
"source_window": "",
"last_content_update": 1674169732483,
"read_only": false,
"read_only_alternatives": []
}
Empty file.
34 changes: 34 additions & 0 deletions .Rproj.user/B584AE7E/sources/session-605e3cd7/9F66EA64
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"id": "9F66EA64",
"path": null,
"project_path": null,
"type": "r_dataframe",
"hash": "0",
"contents": "",
"dirty": false,
"created": 1674164054122.0,
"source_on_save": false,
"relative_order": 2,
"properties": {
"expression": "Howell1",
"caption": "Howell1",
"totalObservations": 544,
"displayedObservations": 544,
"variables": 4,
"cacheKey": "FBB44727",
"object": "Howell1",
"environment": "",
"contentUrl": "grid_resource/gridviewer.html?env=&obj=Howell1&cache_key=FBB44727&max_cols=50",
"preview": 0,
"source_window_id": "",
"Source": "Source"
},
"folds": "",
"lastKnownWriteTime": 6157667972546457443,
"encoding": "",
"collab_server": "",
"source_window": "",
"last_content_update": 1674164054122,
"read_only": false,
"read_only_alternatives": []
}
Empty file.
24 changes: 24 additions & 0 deletions .Rproj.user/B584AE7E/sources/session-605e3cd7/E0A14F48
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"id": "E0A14F48",
"path": "C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/20. Project 2019/StockMarket/Asset_Analysis_v23.Rmd",
"project_path": null,
"type": "r_markdown",
"hash": "0",
"contents": "",
"dirty": false,
"created": 1674164268355.0,
"source_on_save": false,
"relative_order": 3,
"properties": {
"source_window_id": "",
"Source": "Source"
},
"folds": "",
"lastKnownWriteTime": 1674007307,
"encoding": "UTF-8",
"collab_server": "",
"source_window": "",
"last_content_update": 1674007307,
"read_only": false,
"read_only_alternatives": []
}
Loading

0 comments on commit cb57df6

Please sign in to comment.