Skip to content

Commit

Permalink
updates till chap 6
Browse files Browse the repository at this point in the history
  • Loading branch information
b-rodrigues committed Jun 11, 2022
1 parent f529558 commit 8300a43
Show file tree
Hide file tree
Showing 233 changed files with 2,743 additions and 2,498 deletions.
20 changes: 18 additions & 2 deletions 04-descriptives.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2032,7 +2032,23 @@ gss_cat %>%

Much better! In Chapter 6, we are going to learn about `{ggplot2}`.

`{forcats}` contains other very useful functions, so I urge you to go through the documentation.
The last family of function I'd like to mention are the `fct_lump*()` functions. These make it possible
to lump several levels of a factor into a new *other* level:

```{r}
gss_cat %>%
mutate(
# Description of the different functions taken from help(fct_lump)
denom_lowfreq = fct_lump_lowfreq(denom), # lumps together the least frequent levels, ensuring that "other" is still the smallest level.
denom_min = fct_lump_min(denom, min = 10), # lumps levels that appear fewer than min times.
denom_n = fct_lump_n(denom, n = 3), # lumps all levels except for the n most frequent (or least frequent if n < 0)
denom_prop = fct_lump_prop(denom, prop = 0.10) # lumps levels that appear in fewer prop * n times.
)
```

There's many other, so I'd advise you go through the package's function [reference](https://forcats.tidyverse.org/reference/index.html).

### Get your dates right with `{lubridate}`

Expand Down Expand Up @@ -2070,7 +2086,7 @@ independence <- readRDS("datasets/independence.rds")
```

This dataset was scraped from the following Wikipedia [page](https://en.wikipedia.org/wiki/Decolonisation_of_Africa#Timeline).
It shows when African countries gained independence from which colonial powers. In Chapter 11, I
It shows when African countries gained independence and from which colonial powers. In Chapter 10, I
will show you how to scrape Wikipedia pages using R. For now, let's take a look at the contents
of the dataset:

Expand Down
35 changes: 28 additions & 7 deletions 05-graphs.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ resources that I keep coming back to:

* [ggplot2 extensions](https://exts.ggplot2.tidyverse.org/)

* [ggthemes Vignette](https://cran.r-project.org/web/packages/ggthemes/vignettes/ggthemes.html)
* [ggthemes function reference](https://jrnold.github.io/ggthemes/reference/index.html)

* [ggplot2 cheatsheet](https://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf)

Expand Down Expand Up @@ -115,12 +115,34 @@ starwars %>%
geom_point(aes(height, mass))
```

There is a positive correlation between height and mass. Later, we are going to see how we can also plot a regression
line that goes through the scatter plot, but first, let's discover some other `geom_*()` functions.
There is a positive correlation between height and mass, by adding `geom_smooth()` with the option `method = "lm"`:

```{r, warning = FALSE}
starwars %>%
filter(!str_detect(name, "Jabba")) %>%
ggplot(aes(height, mass)) +
geom_point(aes(height, mass)) +
geom_smooth(method = "lm")
```

I've moved the `aes(height, mass)` up to the `ggplot()` function because both `geom_point()` and `geom_smooth()`
need them, and as explained in the begging of this section, the aesthetics listed in `ggplot()` get passed down
to the other geoms.

If you omit `method = "lm`, you get a non-parametric curve:

```{r, warning = FALSE}
starwars %>%
filter(!str_detect(name, "Jabba")) %>%
ggplot(aes(height, mass)) +
geom_point(aes(height, mass)) +
geom_smooth()
```


### Density

`geom_density()` is the *geom* that allows you to get density plots:
Use `geom_density()` to get density plots:

```{r}
ggplot(starwars, aes(height)) +
Expand Down Expand Up @@ -173,8 +195,7 @@ ggplot(filtered_data) +
For the line plots, we are going to use official unemployment data (the same as in the previous
chapter, but with all the available years). Get it from
[here](https://github.com/b-rodrigues/modern_R/tree/master/datasets/unemployment/all)
(downloaded from:
http://www.statistiques.public.lu/stat/TableViewer/tableView.aspx?ReportId=12950&IF_Language=eng&MainTheme=2&FldrName=3&RFPath=91).
(downloaded from the website of the [Luxembourguish national statistical institute](https://lustat.statec.lu/vis?pg=0&df[ds]=release&df[id]=DF_X026&df[ag]=LU1&df[vs]=1.0&pd=2021%2C&dq=..A&ly[rw]=SPECIFICATION&ly[cl]=VARIABLE&lc=en).

Let's plot the unemployment for the canton of Luxembourg only:

Expand Down Expand Up @@ -666,7 +687,7 @@ There are two ways to save plots on disk; one through the *Plots* plane in RStud
`ggsave()` function. Using RStudio, navigate to the *Plots* pane and click on *Export*. You can
then choose where to save the plot and other various options:

```{r}
```{r, echo = FALSE}
knitr::include_graphics("pics/rstudio_save_plots.gif")
```

Expand Down
20 changes: 18 additions & 2 deletions 06-statistical_models.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ model3 <- lm(price ~ ., data = Housing)
summary(model3)
```

You can access different elements of `model3` with `$`, because the result of `lm()` is a list:
You can access different elements of `model3` with `$`, because the result of `lm()` is a list
(you can check this claim with `typeof(model3)`:

```{r}
print(model3$coefficients)
Expand Down Expand Up @@ -215,13 +216,28 @@ The first `.` in the `lm()` function is used to indicate that we wish to use all
used to *place* the result from the two `dplyr` instructions that preceded is to be placed there.
The picture below should help you understand:

```{r}
```{r, echo = FALSE}
knitr::include_graphics("pics/pipe_to_second_position.png")
```

You have to specify this, because by default, when using `%>%` the left hand side argument gets
passed as the first argument of the function on the right hand side.

Since version 4.2, R now also natively includes a placeholder, `_`:

```{r}
Housing |>
select(-driveway, -stories) |>
lm(price ~ ., data = _) |>
broom::tidy()
```

For the example above, I've also switched from `%>%` to `|>`, or else I can't use the `_` placeholder.
The advantage of the `_` placeholder is that it disambiguates `.`. So here, the `.` is a placeholder for
all the variables in the dataset, and `_` is a placeholder for the dataset.


## Diagnostics

Diagnostics are useful metrics to assess model fit. You can read some of these diagnostics, such as
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.
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.
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.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-326-1.png
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.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-4-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-448-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-462-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-467-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-5-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-6-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-7-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-726-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-729-1.png
24 changes: 23 additions & 1 deletion docs/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<meta name="author" content="Bruno Rodrigues" />


<meta name="date" content="2022-06-10" />
<meta name="date" content="2022-06-11" />

<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
Expand Down Expand Up @@ -121,6 +121,28 @@
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<style type="text/css">
/* Used with Pandoc 2.11+ new --citeproc when CSL is used */
div.csl-bib-body { }
div.csl-entry {
clear: both;
}
.hanging div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}
</style>

<link rel="stylesheet" href="style.css" type="text/css" />
</head>
Expand Down
Loading

0 comments on commit 8300a43

Please sign in to comment.