---
title: "Ling 202: session 05: bin. logistic regr. 2 (fill)"
author:
   - name: "[Stefan Th. Gries](https://www.stgries.info)"
     affiliation:
        - UC Santa Barbara
        - JLU Giessen
     orcid: 0000-0002-6497-3958
date: "2025-04-30 12:34:56"
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: 5
      fig-height: 5
      fig-format: png
      fig-dpi: 300
      fig-align: center
      embed-resources: true
execute:
   cache: false
   echo: true
   eval: true
   warning: false
---

# Intro

We are dealing with the same data set as last session; as a reminder, the data are in [_input/genitives.csv](_input/genitives.csv) and you can find information about the variables/columns in [_input/genitives.r](_input/genitives.r). However, this session, we will deal with this in a multifactorial way. Specifically, we are asking, does the choice of a genitive construction (*of* vs. *s*) vary as a function of

* a length-based short-before-long effect, but, this time, if we hypothesize a short-before-long effect, maybe we should not just be looking at the length of the possessor (`POR_LENGTH`), but how the length of the possessor compares to the length of the possessum (`PUM_LENGTH`); since such a comparison variable doesn't exist yet in our data set, we need to create it first;
* the degree/kind of animacy of the possessor (`POR_ANIMACY`: *animate* vs. *collective* vs. *inanimate* vs. *locative* vs. *temporal*);
* whether the speakers are non-native speakers or native speakers of English (`SPEAKER`: *nns* vs. *ns*);
* any pairwise interaction of these predictors;
* the three-way interaction of these predictors?

```{r}
rm(list=ls(all.names=TRUE))
library(car); library(effects); library(magrittr); library(multcomp)
source("_helpers/R2s.r"); source("_helpers/C.score.r") # load small 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
```

# Deviance & baseline(s)

Let's already compute the baselines for what will be the response variable in all case studies, `GENITIVE`:

```{r baselines}
(baselines <- # replace this with your answers/code
```

Let's compute the deviance of the null model:

```{r nulldeviance}
m_00 <- # replace this with your answers/code
# replace this with your answers/code
```

We see that the deviance of `m_00` is still the sum of this model's squared residuals and the model's "null deviance" (and, if a model is a null model, also what is called "residual deviance").

# Exploration & preparation

Some exploration of the relevant variables:

```{r}
# the predictor(s)/response on its/their own
# replace this with your answers/code
# the predictor(s) w/ the response
# replace this with your answers/code
```

# Modeling & numerical interpretation

Let's fit our initial regression model,

```{r}
summary(m_01 <- # replace this with your answers/code
```

... the significance of the model,

```{r}
# replace this with your answers/code
```

... the significance of the predictors:

```{r}
# replace this with your answers/code
```

How do we continue now?

```{r}
# replace this with your answers/code
```

And once we have a final model, what's next?

```{r}
# replace this with your answers/code
```

Does this look like a good model? # replace this with your answers/code

# Visual interpretation

## The interaction `LEN_PORmPUM_LOG:POR_ANIMACY`

Let's visualize the nature of the effect of `LEN_PORmPUM_LOG:POR_ANIMACY` based on the predictions:

```{r}
# replace this with your answers/code
```

This might be better with a one-panel kind of plot:

```{r}
# replace this with your answers/code
```

Do you recognize a couple of (related) general conceptual problems here?

### Excursus on predictions and slopes

replace this with your answers/code

## The interaction `POR_ANIMACY:SPEAKER`

Let's visualize the nature of the effect of `POR_ANIMACY:SPEAKER` based on the predictions, as always, we do both perspectives:

```{r}
# replace this with your answers/code
```

A multi-line plot might be better:

```{r}
# replace this with your answers/code
```

# Excursus 1: predictions that are most wrong

To explore which predictions are the worst -- wrong predictions made most confidently -- let's also compute the contributions to logloss; I'm doing this here in the very explicit two-step procedure just for didactic purposes:

```{r}
d$PRED_PP_OBS <- ifelse( # d$PRED_PP_OBS is determined by ifelse
   d$GENITIVE=="s",         # if the obs genitive is the 2nd level of the response
     d$PRED_PP_S,        # take its predicted probability
   1-d$PRED_PP_S)        # otherwise take 1 minus its predicted probability
d$CONTRIBS2LL <- -log(d$PRED_PP_OBS)
head(d)
```

How could we now explore the predictions that are most wrong? # replace this with your answers/code

# Excursus 2: prototypes

Something else that may well be worth your time involves taking a quick look at which combinations of predictor values (numeric as well as levels) yields the highest predicted probabilities for each level of the response variable. Why might that be interesting? Because of the connection it makes to prototypes. The prototype of a category can be defined "as an abstract entity -- i.e., an abstract notion that may not actually exist in the real world –- that exhibits the features (or properties) f1-n that have the highest cue validity for a category, where cue validity, [...] , can in turn be defined as follows: A feature/property (e.g., 'having a beak') has a high cue validity for a category (e.g., 'birds') if

* many members of the category have the feature (and yes, all birds have beaks);
* most non-members of the category do not have the feature (most non-birds have no beaks -- animals that are not birds but have beaks make up a small set of some turtles, octopuses, platypuses/echidnas, and pufferfish)." ([Gries, Slocum, & Tobia 2024](https://www.sciencedirect.com/science/article/pii/S2666799123000394?via%3Dihub))

Armed with this definition, we might follow [Gries (2003)](https://www.stgries.info/research/2003_STG_PrototypCxs_ARCL.pdf) and stipulate for our data here that

* # replace this with your answers/code;
* # replace this with your answers/code.

(according to our model at least). We can try and identify such prototypes in three places:

* # replace this with your answers/code;
* # replace this with your answers/code;
* # replace this with your answers/code.

The inferences from these three places will converge but, because of the presence of interactions, identifying the highest predicted probabilities from the model's coefficients can be tricky even in a relatively small final model like here (because of how you would need to add and subtract values). We will therefore immediately go to `nd` instead:

```{r}
# replace this with your answers/code
```

The most obvious results from looking at all theoretically possible combinations as generated in `nd` are that

* # replace this with your answers/code;
* # replace this with your answers/code.

The above was based on `nd`, i.e. all theoretically possible configurations of values. To be safe, I recommend you also look at `d` or, more specifically, `d$PRED_PP_S` because it is possible that many of the theoretically possible configurations in `nd` are actually never attested. That would be fine because we said above a prototype is "an abstract notion that may not actually exist in the real world" -- we're just looking at the combination(s) of features -- but wouldn't we want to know whether the abstract prototype we infer from our model actually exists? (Hint: the answer is "yes".) So let's check out `d`:

```{r}
# replace this with your answers/code
```

It seems that our prototypes as inferred from `nd` are also attested:

* # replace this with your answers/code;
* # replace this with your answers/code.

# Write-up

How would we write this up? # replace this with your answers/code

# Homework

To prepare for next session, read *SFLWR*^3^, Section 5.4.1 on multinomial regression.

# Session info

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