Skip to content

Commit

Permalink
Update Feb 27th
Browse files Browse the repository at this point in the history
General update
  • Loading branch information
lacorreia65 committed Feb 27, 2023
1 parent 0604033 commit 1de268c
Show file tree
Hide file tree
Showing 23 changed files with 185 additions and 81 deletions.
130 changes: 65 additions & 65 deletions .Rhistory
Original file line number Diff line number Diff line change
@@ -1,68 +1,3 @@
) ,data=d)
precis(m4.5)
# 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 ~ uniform(1,7);
// 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)
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)
traceplot(fit_m4.6, pars = "sigma")
# Same Model in STAN
stan_m4.6 <- "
Expand Down Expand Up @@ -510,3 +445,68 @@ 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)
library(rethinking)
library(tidyverse)
library(dplyr)
library(ggplot2)
library(rstan)
data(WaffleDivorce)
d <-WaffleDivorce
# standardizevariables
d$D <-standardize(d$Divorce)
d$M <-standardize(d$Marriage)
d$A <-standardize(d$MedianAgeMarriage)
View(d)
standardize(d$Divorce)
help(standardize)
??standardize
data(WaffleDivorce)
d <-WaffleDivorce
# standardizevariables
d$D <-standardize(d$Divorce)
d$M <-standardize(d$Marriage)
d$A <-standardize(d$MedianAgeMarriage)
View(d)
mean(d$D)
sd(d$D)
density(d$D)
plot(density(d$D))
marriage
density(d$Marriage)
plot(density(d$Marriage))
m5.1 <-quap(
alist(
D ~dnorm(mu,sigma),
mu <-a+bA*A,
a ~dnorm(0,0.2),
bA ~dnorm(0,0.5),
sigma ~dexp(1)
) ,data=d)
precis(m5.1)
data(WaffleDivorce)
d <-WaffleDivorce
# standardizevariables
d$D <-standardize(d$Divorce)
d$M <-standardize(d$Marriage)
d$A <-standardize(d$MedianAgeMarriage)
dat <- list(
D = d$D,
M = d$M,
A = d$A
)
m_DMA <- quap(
alist(
D ~ dnorm(mu, sigma)
mu <- a + bM*M + bA*A,
m_DMA <- quap(
alist(
D ~ dnorm(mu, sigma),
mu <- a + bM*M + bA*A,
a ~dnorm(0,.2),
bM~dnorm(0,.5),
bA~dnorm(0,.5),
sigma~dexp(1)
), data=dat )
precis(m_DMA)
precis(m_DMA)
plot(precis(m_DMA))
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": 107,
"splitterpos": 127,
"topwindowstate": "NORMAL",
"panelheight": 803,
"windowheight": 841
"panelheight": 959,
"windowheight": 997
},
"right": {
"splitterpos": 503,
"splitterpos": 600,
"topwindowstate": "NORMAL",
"panelheight": 803,
"windowheight": 841
"panelheight": 959,
"windowheight": 997
}
}
10 changes: 5 additions & 5 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": "1647904797",
"hash": "4113012867",
"contents": "",
"dirty": false,
"created": 1673908998203.0,
Expand All @@ -16,17 +16,17 @@
"Source": "Source",
"docOutlineVisible": "1",
"rmdVisualCollapsedChunks": "",
"rmdVisualModeLocation": "16884:27202",
"cursorPosition": "924,0",
"rmdVisualModeLocation": "17531:17209",
"cursorPosition": "983,0",
"scrollLine": "0",
"docOutlineSize": "118"
},
"folds": "",
"lastKnownWriteTime": 1675828299,
"lastKnownWriteTime": 1675884203,
"encoding": "UTF-8",
"collab_server": "",
"source_window": "",
"last_content_update": 1675828299557,
"last_content_update": 1675884203764,
"read_only": false,
"read_only_alternatives": []
}
51 changes: 51 additions & 0 deletions .Rproj.user/B584AE7E/sources/per/t/77F68D30-contents
Original file line number Diff line number Diff line change
Expand Up @@ -938,3 +938,54 @@ abline(lm(Y[Z==0]~X[Z==0]), col=4, lwd=3)

abline(lm(Y~X), lwd=3)
```

Divorce Database and Waffle Houses

```{r}
data(WaffleDivorce)

d <-WaffleDivorce

# standardizevariables
d$D <-standardize(d$Divorce)
d$M <-standardize(d$Marriage)
d$A <-standardize(d$MedianAgeMarriage)

dat <- list(
D = d$D,
M = d$M,
A = d$A
)
```

```{r}
m_DMA <- quap(
alist(
D ~ dnorm(mu, sigma),
mu <- a + bM*M + bA*A,
a ~dnorm(0,.2),
bM~dnorm(0,.5),
bA~dnorm(0,.5),
sigma~dexp(1)
), data=dat )
```

```{r}
m5.1 <-quap(
alist(
D ~dnorm(mu,sigma),
mu <-a+bA*A,
a ~dnorm(0,0.2),
bA ~dnorm(0,0.5),
sigma ~dexp(1)
) ,data=d)
```

```{r}
precis(m_DMA)
plot(precis(m_DMA))
```

```{r}
precis(m5.1)
```
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,8 +5,8 @@
"Source": "Source",
"docOutlineVisible": "1",
"rmdVisualCollapsedChunks": "",
"rmdVisualModeLocation": "16884:27202",
"cursorPosition": "924,0",
"rmdVisualModeLocation": "17531:17209",
"cursorPosition": "983,0",
"scrollLine": "0",
"docOutlineSize": "118"
}
1 change: 1 addition & 0 deletions .Rproj.user/B584AE7E/sources/prop/INDEX
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
C%3A%2FUsers%2Flacor%2FOneDrive%2F100.%20Personal%2FSelfDevelopment%2F20.%20Project%202019%2FBayesian-Inference%2FCausalAnalysis_v9.Rmd="B1529C46"
C%3A%2FUsers%2Flacor%2FOneDrive%2F100.%20Personal%2FSelfDevelopment%2F20.%20Project%202019%2FBayesian-Inference%2FMetaPolls_vFINAL.Rmd="12DA31F2"
C%3A%2FUsers%2Flacor%2FOneDrive%2F100.%20Personal%2FSelfDevelopment%2F20.%20Project%202019%2FBayesian-Inference%2FSamplinginPractice_v7.Rmd="706557E6"
C%3A%2FUsers%2Flacor%2FOneDrive%2F100.%20Personal%2FSelfDevelopment%2F20.%20Project%202019%2FStockMarket%2FAsset_Analysis_v23.Rmd="8C7CB8DC"
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Attaching package: ‘rethinking’
print.tbl_lazy
print.tbl_sql
"
"2","── Attaching packages ───────────────────────────────────────────────────── tidyverse 1.3.2 ──"
"2","── Attaching packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.2 ──"
"2","✔ ggplot2 3.4.0 ✔ purrr  0.3.5
✔ tibble  3.1.8 ✔ dplyr  1.0.10
✔ tidyr  1.2.1 ✔ stringr 1.4.1
Expand All @@ -62,7 +62,7 @@ Attaching package: ‘rethinking’
"2","Warning: package ‘readr’ was built under R version 4.2.2"
"2","Warning: package ‘purrr’ was built under R version 4.2.2"
"2","Warning: package ‘dplyr’ was built under R version 4.2.2"
"2","── Conflicts ──────────────────────────────────────────────────────── tidyverse_conflicts() ──
"2","── Conflicts ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ tidyr::extract() masks rstan::extract()
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions .Rproj.user/shared/notebooks/paths
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/20. Project 2019/Bayesian-Inference/CausalAnalysis_v9.Rmd="BBEEF53F"
C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/20. Project 2019/Bayesian-Inference/MetaPolls_vFINAL.Rmd="040CBFF0"
C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/20. Project 2019/Bayesian-Inference/SamplinginPractice_v7.Rmd="C44D6AD8"
C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/20. Project 2019/StockMarket/Asset_Analysis_v23.Rmd="0B603800"
Expand Down
51 changes: 51 additions & 0 deletions Statistical Thinking 2023_v2.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -938,3 +938,54 @@ abline(lm(Y[Z==0]~X[Z==0]), col=4, lwd=3)
abline(lm(Y~X), lwd=3)
```

Divorce Database and Waffle Houses

```{r}
data(WaffleDivorce)
d <-WaffleDivorce
# standardizevariables
d$D <-standardize(d$Divorce)
d$M <-standardize(d$Marriage)
d$A <-standardize(d$MedianAgeMarriage)
dat <- list(
D = d$D,
M = d$M,
A = d$A
)
```

```{r}
m_DMA <- quap(
alist(
D ~ dnorm(mu, sigma),
mu <- a + bM*M + bA*A,
a ~dnorm(0,.2),
bM~dnorm(0,.5),
bA~dnorm(0,.5),
sigma~dexp(1)
), data=dat )
```

```{r}
m5.1 <-quap(
alist(
D ~dnorm(mu,sigma),
mu <-a+bA*A,
a ~dnorm(0,0.2),
bA ~dnorm(0,0.5),
sigma ~dexp(1)
) ,data=d)
```

```{r}
precis(m_DMA)
plot(precis(m_DMA))
```

```{r}
precis(m5.1)
```

0 comments on commit 1de268c

Please sign in to comment.