---
title: "Ling 204: session 01"
author:
   - name: "[Stefan Th. Gries](https://www.stgries.info)"
     affiliation:
        - UC Santa Barbara
        - JLU Giessen
     orcid: 0000-0002-6497-3958
date: "2026-01-07 12:34:56 PDT"
date-format: "DD MMM YYYY HH-mm-ss"
editor: source
format:
   html:
      page-layout: full
      code-fold: false
      code-link: true
      code-copy: true
      code-tools: true
      code-line-numbers: true
      code-overflow: scroll
      number-sections: true
      smooth-scroll: true
      toc: true
      toc-depth: 4
      number-depth: 4
      toc-location: left
      monofont: lucida console
      tbl-cap-location: top
      fig-cap-location: bottom
      fig-width: 4
      fig-height: 4
      fig-format: png
      fig-dpi: 300
      fig-align: center
      embed-resources: true
      link-external-newwindow: true
execute:
   cache: false
   echo: true
   eval: true
   warning: false
---

# Session 01: Recap of fixef regression modeling {#sec-s01}
## Linear modeling {#sec-s01_lin}

To do a quick recap of linear modeling, we will look at a data set in [_input/rts.csv](_input/rts.csv) involving reaction times as the response variable:

```{r}
rm(list=ls(all.names=TRUE))
pkgs2load <- c("car", "effects", "emmeans", "magrittr", "multcomp")
suppressMessages(sapply(pkgs2load, library,
   character.only=TRUE, logical.return=TRUE, quietly=TRUE)); rm(pkgs2load)
summary(d <- read.delim(   # summarize d, the result of loading
   file="_input/rts.csv", # this file
   stringsAsFactors=TRUE)) # change categorical variables into factors
```

You can see what the variables/columns contain in the file [_input/rts.r](_input/rts.r). Specifically, we are asking, does the (logged) reaction time to a word (in ms) vary as a function of

* the Zipf frequency of that word (`ZIPFFREQ`);
* the length of that word (`LENGTH`);
* the language that word was presented in (`LANGUAGE`: *spanish* vs. *english*);
* the speaker group that words was presented to (`GROUP`: *english* vs. *heritage*);
* any pairwise interaction of these predictors? (Specifically, we are expecting that the effect of `LENGTH` will be stronger for when `LANGUAGE` is *spanish*.)

### Exploration & preparation {#sec-s01_lin_explprep}

As always, a decent amount of exploration is required to prepare the data for a hopefully unproblematic modeling process.

#### The response variable {#sec-s01_lin_explprep_resp}

We begin our exploration with the response variable `RT`:

```{r}
# XXX
```

#### Numeric predictors {#sec-s01_lin_explprep_numpred}

Let's now look at the numeric predictors. We begin with `ZIPFFREQ`:

```{r}
# XXX
```

Let's now look at `LENGTH`:

```{r}
# XXX
```

#### Categorical predictors {#sec-s01_lin_explprep_catpred}

Let's now look at the categorical predictors. We begin with `LANGUAGE`:

```{r}
# XXX
```

Let's now look at `GROUP`:

```{r}
# XXX
```

Do we have a good distribution for the interaction of the categorical variables?

```{r}
# XXX
```

#### Deviance {#sec-s01_lin_explprep_dev}

First, we'll quickly compute the deviance of the null model:

```{r}
# XXX
```

### Model selection {#sec-s01_lin_modeling}

The initial model involves all predictors and all their interactions:

```{r}
# XXX
```

Let's ...

```{r}
# XXX
```

Let's ...

```{r}
# XXX
```

Let's ...

```{r}
# XXX
```

```{r}
# XXX
```

Normally, we would do some regression diagnostics **now** -- I have to skip those now to discuss how to interpret the model, but we will return to this later! For now, we compute the confidence intervals ...

```{r}
# XXX
```

... , add the predictions of the model to the original data frame:

```{r}
# XXX
```

..., and generate an often super-useful kind of data frame that I always call `nd` (for *new data*) that contains all combinations of

* all levels of all categorical predictors and
* useful values of all numeric predictors (where 'useful' is usually 0, 1 and the mean):

```{r}
# XXX
```

To that, we add the predictions the model makes for all these combinations and give it some nice(r) names:

```{r}
# XXX
```

### Model evaluation & interpretation {#sec-s01_lin_modeleval}

Let's look at the results.

#### `LANGUAGE:GROUP` {#sec-s01_lin_modeleval_catcat}

Let's visualize the nature of the effect of `LANGUAGE:GROUP` based on the predictions from `effects`:

```{r}
# XXX
```

#### `LANGUAGE:LENGTH` {#sec-s01_lin_modeleval_catnum}

Let's visualize the nature of the effect of `LANGUAGE:LENGTH` based on the predictions from `effects`:

```{r}
# XXX
```

#### `ZIPFFREQ:LENGTH` {#sec-s01_lin_modeleval_numnum}

Let's visualize the nature of the effect of `ZIPFFREQ:LENGTH` based on the predictions from `effects`:

```{r}
# XXX
```

### Write-up {#sec-s01_lin_writeup}

To determine whether the reaction time to a word (in ms) varies as a function of

* the Zipf frequency of that word (`ZIPFFREQ`);
* the length of that word (`LENGTH`);
* the language that word was presented in (`LANGUAGE`: *english* vs. *spanish*);
* the speaker group that words was presented to (`GROUP`: *english* vs. *heritage*);
* any pairwise interaction of these predictors with the specific expectation that the effect of `LENGTH` will be stronger for when `LANGUAGE` is *spanish*

XXX

## Binary logistic regression modeling {#sec-s01_binlog}

To do a quick recap of linear modeling, we will look at a data set in [_input/genitives.csv](_input/genitives.csv) involving genitive choices as the response variable:

```{r}
rm(list=ls(all.names=TRUE))
pkgs2load <- c("car", "effects", "emmeans", "magrittr", "multcomp")
suppressMessages(sapply(pkgs2load, library,
   character.only=TRUE, logical.return=TRUE, quietly=TRUE)); rm(pkgs2load)
source("_helpers/C.score.r"); source("_helpers/R2s.r");  # helper functions
summary(d <- read.delim(   # summarize d, the result of loading
   file="_input/genitives.csv", # this file
   stringsAsFactors=TRUE)) # change categorical variables into factors
```

You can see what the variables/columns contain in the file [_input/genitives.r](_input/genitives.r). Specifically, we want to see whether the choice of a genitive varies as a function of

* whether the speaker is a native speaker or not (`SPEAKER`);
* the length difference between possessor and possessum and whether native and non-native speakers react to it in the same way;
* the animacy of the possessor and whether native and non-native speakers react to it in the same way (`POR_ANIMACY`);
* whether the possessor ends in a sibilant or not and whether native and non-native speakers react to it in the same way (`POR_FINAL_SIB`);
* whether the modality is speaking or writing and whether native and non-native speakers react to it in the same way (`MODALITY`);
* the interaction of `POR_FINAL_SIB` with `MODALITY`.

### Exploration & preparation {#sec-s01_binlog_explprep}
#### All variables {#sec-s01_binlog_explprep_vars}

As always, a decent amount of exploration is required to prepare the data for a hopefully unproblematic modeling process. We will only focus on the four variables of interest (and even there will take some didactically motivated shortcuts). We begin our exploration with the creation of the length difference predictor:

Let's look at the numeric predictor first by exploring its 'components':

```{r}
# XXX
```

This looks ...

```{r}
# XXX
```

What if ...

```{r}
# XXX
```

That looks ...

```{r}
# XXX
```

Then we cross-tabulate for all interactions:

```{r}
# XXX
```

All looking ..., but what if we add the response?

```{r}
# XXX
```

#### Deviance {#sec-s01_binlog_explprep_dev}

First, we'll quickly compute the deviance of the null model:

```{r}
# XXX
```

### Model selection {#sec-s01_binlog_modeling}

Let's fit our initial regression model:

```{r}
# XXX
```

Let's ...

```{r}
# XXX
```

Let's ...

```{r}
# XXX
```

Let's ...

```{r}
# XXX
```

It seems ...

```{r}
# XXX
```

Here are ...

```{r}
# XXX
```

### Model evaluation & interpretation: numerical & visual {#sec-s01_binlog_modeleval}

We first assess the predictive power of the model in various ways. For that, we first compute all numeric and categorical predictions ...

```{r}
# XXX
```

... and then we evaluate the confusion matrix:

```{r}
# XXX
```

Also, we compute *R*^2^-values, Cohen's &kappa;, and *C*:

```{r}
# XXX
```

Let's now look at the effects/interactions one by one.

#### Main effect 1: `LEN_PORmPUM_LOG`

Here's the effects object for the first interaction, `LEN_PORmPUM_LOG`:

```{r}
# XXX
```

Let's visualize:

```{r}
# XXX
```

Interpretation: ...

#### Main effect 2: `POR_FINAL_SIB`

Here's the effects object for the second main effect, `POR_FINAL_SIB`:

```{r}
# XXX
```

Let's visualize:

```{r}
# XXX
```

Interpretation: ...

#### Interaction 1: `SPEAKER:POR_ANIMACY`

Here's the effects object for the first interaction, `SPEAKER:POR_ANIMACY`:

```{r}
# XXX
```

Let's visualize:

```{r}
# XXX
```

Interpretation: ...

#### Interaction 2: `SPEAKER:MODALITY`

Here's the effects object for the second interaction, `SPEAKER:MODALITY`:

```{r}
# XXX
```

Let's visualize:

```{r}
# XXX
```

Interpretation: ...

### Write-up {#sec-s01_binlog_writeup}

To determine whether the choice of a genitive varies as a function of

* whether the speaker is a native speaker or not;
* the length difference between possessor and possessum and whether native and non-native speakers react to it in the same way;
* the animacy of the possessor and whether native and non-native speakers react to it in the same way;
* whether the possessor ends in a sibilant or not and whether native and non-native speakers react to it in the same way;
* whether the modality is speaking or writing and whether native and non-native speakers react to it in the same way;
* the interaction of `POR_FINAL_SIB` with `MODALITY`.

...

```{r}
sessionInfo()
```
