Skip to content

Commit

Permalink
Push Lec4-02
Browse files Browse the repository at this point in the history
Ongoing Lecture 4
  • Loading branch information
lacorreia65 committed Jan 20, 2023
1 parent cb57df6 commit c69ea2a
Show file tree
Hide file tree
Showing 39 changed files with 667 additions and 492 deletions.
Binary file modified .RData
Binary file not shown.
981 changes: 500 additions & 481 deletions .Rhistory

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .Rproj.user/B584AE7E/pcs/windowlayoutstate.pper
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"left": {
"splitterpos": 401,
"splitterpos": 155,
"topwindowstate": "NORMAL",
"panelheight": 959,
"windowheight": 997
Expand Down
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": "1895704522",
"hash": "799310168",
"contents": "",
"dirty": false,
"created": 1673908998203.0,
Expand All @@ -16,16 +16,16 @@
"Source": "Source",
"docOutlineVisible": "1",
"rmdVisualCollapsedChunks": "",
"rmdVisualModeLocation": "10046:19198",
"cursorPosition": "527,0",
"rmdVisualModeLocation": "11232:22202",
"cursorPosition": "602,0",
"scrollLine": "0"
},
"folds": "",
"lastKnownWriteTime": 1674170667,
"lastKnownWriteTime": 1674187233,
"encoding": "UTF-8",
"collab_server": "",
"source_window": "",
"last_content_update": 1674170667303,
"last_content_update": 1674187233984,
"read_only": false,
"read_only_alternatives": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,57 @@ ggplot(df, aes(x=W_contrast,fill=W_flag))+
geom_density()+
theme_bw()
```

Now, what is the direct effect of sex over weight?

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

# Indirect effect (b) is the same for men and women
dat |>
ggplot(aes(x=H, y=W, color=factor(S)))+
geom_point()+
geom_smooth(method = lm)+
# facet_wrap(~S)+
theme_bw()
```

```{r, message=FALSE}
dat <- list(
W = d$weight,
H = d$height,
Hbar = mean(d$height),
S = d$male+1 )

m_SHW <- quap(
alist(
W ~ dnorm(mu,sigma),
mu <- a[S]+b[S]*(H-Hbar),
a[S] ~ dnorm(60,10),
b[S] ~ dunif(0,1),
sigma ~ dunif(0,10)
), data=dat
)
data.frame(dat) |>
ggplot(aes(x=H, y=W, color=factor(S)))+
geom_point()+
geom_smooth(method = lm)+
# facet_wrap(~S)+
theme_bw()

precis(m_SHW,depth=2)
```

Note that regression line's slopes are almost the same for both male and female.

```{r}
post <- extract.samples(m_SHW)
dens(post$b[,1],xlim=c(.3, .9),ylim=c(0,8),
lwd=3,col=2,xlab="posterior mean weight(kg)")
dens(post$b[,2],lwd=3,col=4,add=TRUE)
```

```{r}

```
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"path": "C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/20. Project 2019/StockMarket/Asset_Analysis_v23.Rmd",
"project_path": null,
"type": "r_markdown",
"hash": "0",
"hash": "1870848255",
"contents": "",
"dirty": false,
"created": 1674164268355.0,
Expand Down
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": "10046:19198",
"cursorPosition": "527,0",
"rmdVisualModeLocation": "11232:22202",
"cursorPosition": "602,0",
"scrollLine": "0"
}
Empty file.
Empty file.
Empty file.
Binary file added .Rproj.user/B584AE7E/viewer-cache/993CD89E.Rdata
Binary file not shown.
Binary file added .Rproj.user/B584AE7E/viewer-cache/FBB44727.Rdata
Binary file not shown.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"0","S <- rbern(100)+1"
"0","dat <- sim_HW(S, b=c(.5,.5), a=c(0,10))"
"0",""
"0","# Indirect effect (b) is the same for men and women"
"0","dat |> "
"0"," ggplot(aes(x=H, y=W, color=factor(S)))+"
"0"," geom_point()+"
"0"," geom_smooth(method = lm)+"
"0"," # facet_wrap(~S)+"
"0"," theme_bw()"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"height":408.5290482076638,"width":661.0,"size_behavior":0,"conditions":[]}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"0","dat <- list("
"0"," W = d$weight,"
"0"," H = d$height,"
"0"," Hbar = mean(d$height),"
"0"," S = d$male+1 )"
"0",""
"0","m_SHW <- quap("
"0"," alist("
"0"," W ~ dnorm(mu,sigma),"
"0"," mu <- a[S]+b[S]*(H-Hbar),"
"0"," a[S] ~ dnorm(60,10),"
"0"," b[S] ~ dunif(0,1),"
"0"," sigma ~ dunif(0,10)"
"0"," ), data=dat"
"0",")"
"0","data.frame(dat) |> "
"0"," ggplot(aes(x=H, y=W, color=factor(S)))+"
"0"," geom_point()+"
"0"," geom_smooth(method = lm)+"
"0"," # facet_wrap(~S)+"
"0"," theme_bw()"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"height":408.5290482076638,"width":661.0,"size_behavior":0,"conditions":[]}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"0",""
"0","precis(m_SHW,depth=2)"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"classes":["data.frame"],"nrow":5,"ncol":4,"summary":{"Description":["df [5 × 4]"]}}
Binary file not shown.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"0","xseq <- seq(from=130,to=190, len=50)"
"0","muF <- "
"0"," link(m_adults2, data=list(S=rep(1,50),H=xseq,Hbar=mean(d$height)))"
"2","Error in h(simpleError(msg, call)) :
error in evaluating the argument 'fit' in selecting a method for function 'link': object 'm_adults2' not found
"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"frames":[{"func":"link(m_adults2, data = list(S = rep(1, 50), H = xseq, Hbar = mean(d$height)))","file":"","line_number":0,"end_line_number":0,"character_number":0,"end_character_number":0},{"func":".handleSimpleError(function (cond) \n.Internal(C_tryCatchHelper(addr, 1L, cond)), \"object 'm_adults2' not found\", \n base::quote(link(m_adults2, data = list(S = rep(1, 50), H = xseq, \n Hbar = mean(d$height)))))","file":"","line_number":0,"end_line_number":0,"character_number":0,"end_character_number":0},{"func":"h(simpleError(msg, call))","file":"","line_number":0,"end_line_number":0,"character_number":0,"end_character_number":0}],"message":"Error in h(simpleError(msg, call)) : \n error in evaluating the argument 'fit' in selecting a method for function 'link': object 'm_adults2' not found\n"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"0","post <- extract.samples(m_SHW)"
"0","dens(post$b[,1],xlim=c(.3, .9),ylim=c(0,8),"
"0"," lwd=3,col=2,xlab=""posterior mean weight(kg"")"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"0","dens(post$b[,2],lwd=3,col=4,add=TRUE)"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"height":408.5290482076638,"width":661.0,"size_behavior":0,"conditions":[]}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added Articles/Quadractic Approximation (1988).pdf
Binary file not shown.
54 changes: 54 additions & 0 deletions Statistical Thinking 2023_v2.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,57 @@ ggplot(df, aes(x=W_contrast,fill=W_flag))+
geom_density()+
theme_bw()
```

Now, what is the direct effect of sex over weight?

```{r, message=FALSE}
S <- rbern(100)+1
dat <- sim_HW(S, b=c(.5,.5), a=c(0,10))
# Indirect effect (b) is the same for men and women
dat |>
ggplot(aes(x=H, y=W, color=factor(S)))+
geom_point()+
geom_smooth(method = lm)+
# facet_wrap(~S)+
theme_bw()
```

```{r, message=FALSE}
dat <- list(
W = d$weight,
H = d$height,
Hbar = mean(d$height),
S = d$male+1 )
m_SHW <- quap(
alist(
W ~ dnorm(mu,sigma),
mu <- a[S]+b[S]*(H-Hbar),
a[S] ~ dnorm(60,10),
b[S] ~ dunif(0,1),
sigma ~ dunif(0,10)
), data=dat
)
data.frame(dat) |>
ggplot(aes(x=H, y=W, color=factor(S)))+
geom_point()+
geom_smooth(method = lm)+
# facet_wrap(~S)+
theme_bw()
precis(m_SHW,depth=2)
```

Note that regression line's slopes are almost the same for both male and female.

```{r}
post <- extract.samples(m_SHW)
dens(post$b[,1],xlim=c(.3, .9),ylim=c(0,8),
lwd=3,col=2,xlab="posterior mean weight(kg)")
dens(post$b[,2],lwd=3,col=4,add=TRUE)
```

```{r}
```

0 comments on commit c69ea2a

Please sign in to comment.