Skip to content

Commit

Permalink
rerender work
Browse files Browse the repository at this point in the history
  • Loading branch information
JoFrhwld committed Dec 18, 2023
1 parent 2ef59a8 commit d5cf47e
Show file tree
Hide file tree
Showing 5 changed files with 714 additions and 1,373 deletions.
2 changes: 2 additions & 0 deletions teaching/courses/2017_lsa/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
title: "LSA 2017 Statistical Modelling with R"
listing:
contents: lectures
type: table
fields: [image, order, title, reading-time]
---

- [Meeting 1: Introduction to R](lectures/Session_1.nb.html)
Expand Down
54 changes: 24 additions & 30 deletions teaching/courses/2017_lsa/lectures/Session_1.nb.qmd
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
---
title: "Introduction to R"
image: figures/cran_package.png
order: 1
knitr:
opts_chunk:
error: true
warning: false
---

## Hellos
Expand All @@ -27,7 +30,7 @@ These are some of the core areas I figure are necessary to getting good at stati

These are all skills you can achieve through practice, experience, and occasional guidance from someone more skilled than you. It is exactly like acquiring any other skill or craft. At first it will be confusing, you'll make some mistakes, and it won't look so good. I think

::: {layout="[40,-20,40]"}
::: {layout="[45,-10,45]"}
![The first hat I ever knit](figures/firsthat.jpg)

![The most recent hat I knit](figures/lasthat.jpg)
Expand All @@ -52,7 +55,7 @@ Most of the content of the course is devoted to core R programming (things you s
The course will follow the workflow outlined at the beginning: `begin → summarize → visualize → analyze`.

| Week | Monday | Thursday |
|------------------:|:--------------------------:|:------------------------:|
|------------------:|:-------------------------:|:------------------------:|
| 1 | -- | Intro - Basics & R Notebooks |
| 2 | Data Frames & Factors | Split-Apply-Combine, Reshaping |
| 3 | ggplot2 | Fitting Linear Models |
Expand Down Expand Up @@ -160,17 +163,15 @@ I'm going to recommend (for now at least) that you run all of your code though a

My earlier advice would have been to write all of your code in an R script file, but that also separates the code from its results, which can be hard for beginners to keep track of.

<hr>

</hr>
------------------------------------------------------------------------

## Installing R Packages

R comes with a lot of functionality installed, but one way that R is extensible is through users' ability to contribute new code & data through it's package management system. We're going to using a number of these packages in the course, especially since a few of them have fundamentally changed the way R programming works in the past 3 years. There's also a course R package I've created to easily distribute sample datasets.

Here's a basic diagram of how R packages work:

![](figures/cran_package.png)
![](figures/cran_package.png){fig-align="center" width="100%"}

### Installing Packages

Expand Down Expand Up @@ -200,14 +201,15 @@ library("ggplot2")
foo <- ggplot()
```

::: {.box .break}
[\~2 Minute Activity]{.big-label}
::: callout-note
## \~2 minute activity

Let's install all of the packages we're going to use in the course. Double check that you're connected to the internet.

Create a notebook for this lecture called `01_lecture.Rmd`. Copy-paste the following into an R code chunk and run it:

```{r}
#| eval: false
install.packages(
c("tidyverse",
"devtools")
Expand Down Expand Up @@ -281,10 +283,8 @@ In short, you can use these variables `x` and `y` like they *are* the values you
- You could have chosen *almost* any name for these variables.
- You can just as easily assign *new* values to these variables.

::: {.box .idiom}
[Idiom]{.label}

#### Naming Things
::: callout-tip
## Naming Things

`x` and `y` are lousy names for variables. When it comes to naming variables, there's a famous saying:

Expand All @@ -298,7 +298,7 @@ For best practices on naming variables, I'll refer you to [the tidyverse style g

Also, be guided by The Principle of Least Effort. Use the minimal amount of characters that are still clearly interpretable.

```{r}
```
# Good Names
model_1
model_full
Expand Down Expand Up @@ -457,8 +457,8 @@ Of course, if you now wanted to know what year these speakers turned 17, you cou
(interview_year - ages) + 17
```

::: {.box .break}
[\~5 Minute Activity]{.big-label}
::: callout-note
## \~5 minute activity

A Starbucks Grande filter coffee in the UK currently costs £1.85. The value of £1 before the Brexit vote was about \$1.49. After the vote, it dropped down to about \$1.31, and lately it's been closer to \$1.27.

Expand All @@ -474,7 +474,7 @@ If you have a bunch of values stored in a vector, and you want to pull out speci
Let's start by indexing by position.

::: {style="font-family:monospace;font-size:xx-large;text-align:center;"}
[vector]{style="color:#747474"}\[[</span>]{style="color:red"}[position]{style="color:#747474"}<span style="color:red"><span style="color:red">\]
[vector]{style="color:#747474"}[\[]{style="color:red"}[position]{style="color:#747474"}[\]]{style="color:red"}
:::

R has some built in vectors for you to use, like one called `letters`. We haven't defined `letters`, and it's not listed as being in your R environment, but it's there.
Expand Down Expand Up @@ -504,7 +504,7 @@ letters[abba]
You can also index by logical values.

::: {style="font-family:monospace;font-size:xx-large;text-align:center;"}
[vector]{style="color:#747474"}\[[</span>]{style="color:red"}[true false vector]{style="color:#747474"}<span style="color:red"><span style="color:red">\]
[vector]{style="color:#747474"}[\[]{style="color:red"}[true false vector]{style="color:#747474"}[\]]{style="color:red"}
:::

Let's come back to our vector of speaker's ages
Expand All @@ -529,8 +529,8 @@ ages > 40
ages[ages > 40]
```

::: {.box .break}
[\~2 Minute Activity]{.big-label}
::: callout-note
## \~2 minute activity

Let's assume our speakers had the following names:

Expand All @@ -545,17 +545,15 @@ Using logical indexing and the ages in `ages` and year of interview in `intervie

The following operators will return a vector of `TRUE` and `FALSE` values.

::: {style="width=50%"}
| Operator | Meaning |
|:---------|:-------------------------|
|----------|--------------------------|
| `==` | exactly equal to |
| `!=` | not equal to |
| `>` | greater than |
| `<` | less than |
| `>=` | greater than or equal to |
| `<` | less than |
| `<=` | less than or equal to |
:::

You can use these to compare vectors to single values, as we've seen, but you can also compare vectors to vectors *if they are the same length*. Comparison is done elementwise.

Expand All @@ -568,13 +566,11 @@ group_a < group_b

There are three more operators that have an effect on `TRUE` and `FALSE` vectors.

::: {style="width:50%"}
| Operator | Meaning |
|:---------|:-------------------------------------------------|
|----------|--------------------------------------------------|
| `!` | not x <br> changes all `T` to `F` and `F` to `T` |
| `|` | x or y |
| `&` | x and y |
:::
| \| | `x` or `y` |
| `&` | `x` and `y` |

```{r}
x <- c(T, T, F, F)
Expand Down Expand Up @@ -625,6 +621,4 @@ check_names[check_names %in% speaker_names]
check_names[!(check_names %in% speaker_names)]
```

<hr />

<!--- http://www.babynames1000.com/gender-neutral/ -->
------------------------------------------------------------------------
1,343 changes: 0 additions & 1,343 deletions teaching/courses/2017_lsa/lectures/Session_2.nb.html

This file was deleted.

Loading

0 comments on commit d5cf47e

Please sign in to comment.