Skip to content

Commit

Permalink
Push Jan 29th
Browse files Browse the repository at this point in the history
Creation STAN model for Chap 4.
  • Loading branch information
lacorreia65 committed Jan 29, 2023
1 parent d314c79 commit 9514f00
Show file tree
Hide file tree
Showing 124 changed files with 4,400 additions and 764 deletions.
Binary file modified .RData
Binary file not shown.
1,006 changes: 503 additions & 503 deletions .Rhistory

Large diffs are not rendered by default.

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": 129,
"splitterpos": 128,
"topwindowstate": "NORMAL",
"panelheight": 803,
"windowheight": 841
"panelheight": 959,
"windowheight": 997
},
"right": {
"splitterpos": 504,
"splitterpos": 601,
"topwindowstate": "NORMAL",
"panelheight": 803,
"windowheight": 841
"panelheight": 959,
"windowheight": 997
}
}
34 changes: 34 additions & 0 deletions .Rproj.user/B584AE7E/sources/per/t/18D958F7
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"id": "18D958F7",
"path": null,
"project_path": null,
"type": "r_dataframe",
"hash": "0",
"contents": "",
"dirty": false,
"created": 1674955646734.0,
"source_on_save": false,
"relative_order": 3,
"properties": {
"expression": "d",
"caption": "d",
"totalObservations": "544",
"displayedObservations": "544",
"variables": "6",
"cacheKey": "1A4706DF",
"object": "d",
"environment": "",
"contentUrl": "grid_resource/gridviewer.html?env=&obj=d&cache_key=1A4706DF&max_cols=50",
"preview": "0",
"source_window_id": "",
"Source": "Source"
},
"folds": "",
"lastKnownWriteTime": 7305808869231650162,
"encoding": "",
"collab_server": "",
"source_window": "",
"last_content_update": 1674955646734,
"read_only": false,
"read_only_alternatives": []
}
Empty file.
13 changes: 7 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": "3741477333",
"hash": "59382345",
"contents": "",
"dirty": false,
"created": 1673908998203.0,
Expand All @@ -16,16 +16,17 @@
"Source": "Source",
"docOutlineVisible": "1",
"rmdVisualCollapsedChunks": "",
"rmdVisualModeLocation": "359:954",
"cursorPosition": "448,0",
"scrollLine": "0"
"rmdVisualModeLocation": "14113:13929",
"cursorPosition": "734,0",
"scrollLine": "0",
"docOutlineSize": "118"
},
"folds": "",
"lastKnownWriteTime": 1674288067,
"lastKnownWriteTime": 1674978203,
"encoding": "UTF-8",
"collab_server": "",
"source_window": "",
"last_content_update": 1674701597251,
"last_content_update": 1674978203724,
"read_only": false,
"read_only_alternatives": []
}
166 changes: 164 additions & 2 deletions .Rproj.user/B584AE7E/sources/per/t/77F68D30-contents
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ library(rethinking)
library(tidyverse)
library(dplyr)
library(ggplot2)
library(rstan)
```

## Lecture 1
Expand Down Expand Up @@ -459,9 +460,9 @@ percs[SSlice][i]
### Workflow

```{r}

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

```{r}
Expand Down Expand Up @@ -661,3 +662,164 @@ dens(post$b[,2],lwd=3,col=4,add=TRUE)
```{r}

```

### Polynomials

```{r}
plot(height~weight,d)
```

```{r}
d$weight_s <-(d$weight-mean(d$weight))/sd(d$weight)
d$weight_s2 <-d$weight_s^2
m4.5 <-quap(
alist(
height ~dnorm(mu,sigma),
mu <-a+b1*weight_s+b2*weight_s2,
a ~dnorm(178,20),
b1 ~dlnorm(0,1),
b2 ~dnorm(0,1),
sigma ~dunif(0,50)
) ,data=d)
```

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

```{r}
# Not working - To be Reviewed
stan_m4.5 <- "
data {
int<lower = 1> N;
vector[N] height;
vector[N] weight_s;
vector[N] weight_s2;
}
parameters {
real a;
real b1;
real b2;
real <lower=0> sigma;
}

transformed parameters {
vector[N] mu;

for (i in 1:N)
mu[i] = a + b1*weight_s[i] + b2*weight_s2[i];
}

model {
// priors
a ~ normal(178,20);
b1 ~ normal(0,1);
b2 ~ normal(0,1);
sigma ~ uniform(0,50);

// model
height ~normal(mu,sigma);
}"
data_m4.5 <- list(
N = nrow(d),
height = d$height,
weight_s = d$weight_s,
weight_s2 = d$weight_s2
)

#fit_m4.5 <- stan(data = data_m4.5, model_code = stan_m4.5,
# chains = 3, iter = 1000)

```

```{r}
# Same Model in STAN
stan_m4.6 <- "
data {
int<lower = 1> N;
vector[N] height;
vector[N] weight_s;
vector[N] weight_s2;
}

transformed data{
real bar_x1;
real x1_sd;
vector[N] x1_std;
real y_sd;

bar_x1 = mean(weight_s);
x1_sd = sd(weight_s);
x1_std = (weight_s - bar_x1)/x1_sd; // centered and scaled

y_sd = sd(height);
}

parameters {
real alpha_std;
real beta1_std;
real beta3_std;
real <lower=0> sigma;
}

transformed parameters {
vector[N] mu;

for (i in 1:N)
mu[i] = alpha_std + beta1_std*weight_s[i] +
beta3_std*weight_s2[i];
}

model {
// priors
alpha_std ~ normal(0,20);
beta1_std ~ normal(0,2);
beta3_std ~ normal(0,2);
sigma ~ normal(5,1.5);
// sigma ~ exponential(1/y_sd);

// model
height ~normal(mu,sigma);
}

generated quantities {
real alpha;
real beta1;
real beta3;

alpha = alpha_std - (beta1_std*bar_x1)/x1_sd
+ (beta3_std*bar_x1^2)/x1_sd^2;

beta1 = beta1_std/x1_sd - 2*(beta3_std*bar_x1)/x1_sd^2;

beta3 = beta3_std/x1_sd^2;
}"

data_m4.6 <- list(
N = nrow(d),
height = d$height,
weight_s = d$weight_s,
weight_s2 = d$weight_s2
)

fit_m4.6 <- stan(data = data_m4.6,
model_code = stan_m4.6,
chains = 4,
iter = 2000)

```

```{r}
print(fit_m4.6, pars = c("alpha","beta1", "beta3", "sigma"))
print(fit_m4.6, pars = c("alpha_std","beta1_std", "beta3_std", "sigma"))
precis(m4.5)
```

```{r}
traceplot(fit_m4.6, pars = "sigma")
```

```{r}
sigma_sample <- extract(fit_m4.6)[["sigma"]]
hist(sigma_sample)
```
28 changes: 28 additions & 0 deletions .Rproj.user/B584AE7E/sources/per/t/AC03FAA1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"id": "AC03FAA1",
"path": null,
"project_path": null,
"type": "object_explorer",
"hash": "0",
"contents": "",
"dirty": false,
"created": 1674957271066.0,
"source_on_save": false,
"relative_order": 4,
"properties": {
"id": "4e0121cfc9e74692bb952169ff077b78",
"name": "data_m4.5",
"title": "data_m4.5",
"language": "R",
"source_window_id": "",
"Source": "Source"
},
"folds": "",
"lastKnownWriteTime": 8462582696615503216,
"encoding": "",
"collab_server": "",
"source_window": "",
"last_content_update": 1674957271066,
"read_only": false,
"read_only_alternatives": []
}
Empty file.
26 changes: 26 additions & 0 deletions .Rproj.user/B584AE7E/sources/per/t/FA83A758
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"id": "FA83A758",
"path": "C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/20. Project 2019/Bayesian-Inference/MetaPolls_vFINAL.Rmd",
"project_path": null,
"type": "r_markdown",
"hash": "3543462037",
"contents": "",
"dirty": false,
"created": 1674958961238.0,
"source_on_save": false,
"relative_order": 5,
"properties": {
"source_window_id": "",
"Source": "Source",
"cursorPosition": "662,35",
"scrollLine": "649"
},
"folds": "",
"lastKnownWriteTime": 1667249148,
"encoding": "UTF-8",
"collab_server": "",
"source_window": "",
"last_content_update": 1667249148,
"read_only": false,
"read_only_alternatives": []
}
Loading

0 comments on commit 9514f00

Please sign in to comment.