--- title: "How good of a model do you need? Accounting for classification errors in machine assisted content analysis." author: Nathan TeBlunthuis date: May 24 2022 template: "../resources/template.html" output: xaringan::moon_reader: lib_dir: libs seal: false nature: highlightStyle: github ratio: 16:9 countIncrementalSlides: true slideNumberFormat: |
self_contained: false css: [default, my-theme.css, fontawesome.min.css] chakra: libs/remark-latest.min.js --- ```{r echo=FALSE, warning=FALSE, message=FALSE} library(knitr) library(ggplot2) library(data.table) f <- function (x) {formatC(x, format="d", big.mark=',')} theme_set(theme_bw()) r <- readRDS('remembr.RDS') attach(r) ``` class: center, middle, narrow
### .title-heading[Unlocking the power of big data: The importance of measurement error in machine assisted content analysis] ## Nathan TeBlunthuis nathan.teblunthuis@northwestern.edu [https://teblunthuis.cc](https://teblunthuis.cc) ??? This talk will be me presenting my "lab notebook" and not a polished research talk. Maybe it would be a good week of a graduate seminar? In sum, machine assisted content analysis has unique limitations and threats to validity that I wanted to understand better. I've learned how the noise introduced by predictive models can result in misleading statistical inferences, but that a sample of human-labeled validation data can often be used to account for this noise and obtain accurate inferences in the end. Statistical knowledge of this problem and computational tools for addressing are still in development. My goals for this presentation are to start sharing this information with the community and hopeful to stimulate us to work on extending existing approaches or using them in our work. This is going to be a boring talk about some *very* technical material. If you're not that interested please return to your hackathon. Please interrupt me if I'm going too fast for you or if you don't understand something. I will try to move quickly in the interests of those wishing to wrap up their hackathon projects. I will also ask you to show hands once or twice, if you are already familiar with some concepts that it might be expedient to skip. --- class:center, middle, inverse ## Machine assistent content analysis (MACA) ??? I'm going to start by defining a study design that is increasingly common, especially in Communication and Political Science, but also across the social sciences and beyond. I call it *machine assisted content analysis* (MACA). ---
### .border[Machine assisted content analysis (MACA) uses machine learning for scientific measurement.] .emph[Content analysis:] Statistical analysis of variables measured by human labeling ("coding") of content. This might be simple categorical labels, or maybe more advanced annotations. -- *Downside:* Human labeling is *a lot* of work. -- .emph[Machine assisted content analysis:] Use a *predictive algorithm* (often trained on human-made labels) to measure variables for use in a downstream *primary analysis.* -- *Downside:* Algorithms can be *biased* and *inaccurate* in ways that could invalidate the statistical analysis. ??? A machine assisted content analysis can be part of a more complex or more powerful study design (e.g., an experiment, time series analysis &c). --- --- class:large
### .border[How can MACA go wrong?] Algorithms can be *biased* and *error prone* (*noisy*). -- Predictor bias is a potentially difficult problem that requires causal inference methods. I'll focus on *noise* for now. -- Noise in the predictive model introduces bias in the primary analysis. -- .indent[We can reduce and sometimes even *eliminate* this bias introduced by noise.] --- layout:true
### .border[Example 1: An unbiased, but noisy classifier] .large[.left-column[![](images/example_1_dag.png)]] ??? Please show hands if you are familiar with causal graphs or baysian networks. Should I explain what this diagram means? --- .right-column[ $x$ is *partly observed* because we have *validation data* $x^*$. ] --- .right-column[ $x$ is *partly observed* because we have *validation data* $x^*$. $k$ are the *features* used by the *predictive model* $g(k)$. ] --- .right-column[ $x$ is *partly observed* because we have *validation data* $x^*$. $k$ are the *features* used by the *predictive model* $g(k)$. The predictions $w$ are a *proxy variable* $g(k) = \hat{x} = w$. ] --- .right-column[ $x$ is *partly observed* because we have *validation data* $x^*$. $k$ are the *features* used by the *predictive model* $g(k)$. The predictions $w$ are a *proxy variable* $g(k) = \hat{x} = w$. $x = w + \xi$ because the predictive model makes errors. ] --- layout:true
### .border[Noise in a *covariate* creates *attenuation bias*.] .large[.left-column[![](images/example_1_dag.png)]] --- .right-column[ We want to estimate, $y = Bx + \varepsilon$, but we estimate $y = Bw + \varepsilon$ instead. $x = w + \xi$ because the predictive model makes errors. ] --- .right-column[ We want to estimate, $y = Bx + \varepsilon$, but we estimate $y = Bw + \varepsilon$ instead. $x = w + \xi$ because the predictive model makes errors. Assume $g(k)$ is *unbiased* so $E(\xi)=0$. Also assume error is *nondifferential* so $E(\xi y)=0$: ] --- .right-column[ We want to estimate, $y = Bx + \varepsilon$, but we estimate $y = Bw + \varepsilon$ instead. $x = w + \xi$ because the predictive model makes errors. Assume $g(k)$ is *unbiased* so $E(\xi)=0$. Also assume error is *nondifferential* so $E(\xi y)=0$: $$\widehat{B_w}^{ols}=\frac{\sum^n_{j=j}{(x_j + \xi_j - \overline{(x + \xi)})}(y_j - \bar{y})}{\sum_{j=1}^n{(x_j + \xi_j - \overline{(x+\xi)})^2}} = \frac{\sum^n_{j=j}{(x_j - \bar{x})(y_j - \bar{y})}}{\sum_{j=1}^n{(x_j + \xi_j - \bar{x}){^2}}}$$ ] --- .right-column[ We want to estimate, $y = Bx + \varepsilon$, but we estimate $y = Bw + \varepsilon$ instead. $x = w + \xi$ because the predictive model makes errors. Assume $g(k)$ is *unbiased* so $E(\xi)=0$. Also assume error is *nondifferential* so $E(\xi y)=0$: $$\widehat{B_w}^{ols}=\frac{\sum^n_{j=j}{(x_j + \xi_j - \overline{(x + \xi)})}(y_j - \bar{y})}{\sum_{j=1}^n{(x_j + \xi_j - \overline{(x+\xi)})^2}} = \frac{\sum^n_{j=j}{(x_j - \bar{x})(y_j - \bar{y})}}{\sum_{j=1}^n{(x_j + \color{red}{\xi_j} - \bar{x})\color{red}{^2}}}$$ In this scenario, it's clear that $\widehat{B_w}^{ols} < B_x$. ] ??? Please raise your hands if you're familiar with attenuation bias. I expect that its covered in some graduate stats classes, but not universally. --- class:large layout:false
### .border[Beyond attenuation bias] .larger[Measurement error can theaten validity because:] - Attenuation bias *spreads* (e.g., to marginal effects as illustrated later). -- - Measurement error can be *differential*— not distributed evenly and possible correlated with $x$, $y$, or $\varepsilon$. -- - *Bias can be away from 0* in GLMs and nonlinear models or if measurement error is differential. -- - *Confounding* if the *predictive model is biased* introducing a correlation the measurement error and the residuals $(E[\xi\varepsilon]=0)$. --- class:large layout:false
### .border[Correcting measurement error] There's a vast literature in statistics on measurement error. Mostly about noise you'd find in sensors. Lots of ideas. No magic bullets. -- I'm going to briefly cover 3 different approaches: *multiple imputation*, *regression calibration* and *2SLS+GMM*. -- These all depend on *validation data*. I'm going to ignore where this comes from, but assume it's a random sample of the hypothesis testing dataset. -- You can *and should* use it to improve your statistical estimates. ---
### .border[Multiple Imputation (MI) treats Measurement Error as a Missing Data Problem] 1. Use validation data to estimate $f(x|w,y)$, a probabilistic model of $x$. -- 2. *Sample* $m$ datasets from $\widehat{f(x|w,y)}$. -- 3. Run your analysis on each of the $m$ datasets. -- 4. Average the results from the $m$ analyses using Rubin's rules. -- .e[Advantages:] *Very flexible!* Sometimes can work if the predictor $g(k) $ is biased. Good R packages (**`{Amelia}`**, `{mi}`, `{mice}`, `{brms}`). -- .e[Disadvantages:] Results depend on quality of $\widehat{f(x|w,y)}$; May require more validation data, computationally expensive, statistically inefficient and doesn't seem to benefit much from larger datasets. --- ### .border[Regression calibration directly adjusts for attenuation bias.] 1. Use validation data to estimate the errors $\hat{\xi}$. -- 2. Use $\hat{\xi}$ to correct the OLS estimate. -- 3. Correct the standard errors using MLE or bootstrapping. -- .e[Advantages:] Simple, fast. -- .e[Disadvantages:] Limited to OLS models. Requires an unbiased predictor $g(k)$. R support (`{mecor}` R package) is pretty new. --- layout:true ### .border[2SLS+GMM is designed for this specific problem] .left-column[![](images/Fong_Taylor.png)] *Regression calibration with a trick.* --- .right-column[ 1. Estimate $x = w + \xi$ to obtain $\hat{x}$. (First-stage LS). ] --- .right-column[ 1. Estimate $x = w + \xi$ to obtain $\hat{x}$. (First-stage LS). 2. Estimate $y = B^{2sls}\hat{x} + \varepsilon^{2sls}$. (Second-stage LS / regression calibration). ] --- .right-column[ 1. Estimate $x = w + \xi$ to obtain $\hat{x}$. (First-stage LS). 2. Estimate $y = B^{2sls}\hat{x} + \varepsilon^{2sls}$. (Second-stage LS / regression calibration). 3. Estimate $y = B^{val}x^* + \varepsilon^{val}$. (Validation dataset model). ] --- .right-column[ 1. Estimate $x = w + \xi$ to obtain $\hat{x}$. (First-stage LS). 2. Estimate $y = B^{2sls}\hat{x} + \varepsilon^{2sls}$. (Second-stage LS / regression calibration). 3. Estimate $y = B^{val}x^* + \varepsilon^{val}$. (Validation dataset model). 4. Combine $B^{val}$ and $B^{2sls}$ using the generalized method of moments (GMM). ] --- .right-column[ 1. Estimate $x = w + \xi$ to obtain $\hat{x}$. (First-stage LS). 2. Estimate $y = B^{2sls}\hat{x} + \varepsilon^{2sls}$. (Second-stage LS / regression calibration). 3. Estimate $y = B^{val}x^* + \varepsilon^{val}$. (Validation dataset model). 4. Combine $B^{val}$ and $B^{2sls}$ using the generalized method of moments (GMM). Advantages: Accurate. Sometimes robust if biased predictor $g(k)$ is biased. In theory, flexible to any models that can be fit using GMM. ] --- .right-column[ 1. Estimate $x = w + \xi$ to obtain $\hat{x}$. (First-stage LS). 2. Estimate $y = B^{2sls}\hat{x} + \varepsilon^{2sls}$. (Second-stage LS / regression calibration). 3. Estimate $y = B^{val}x^* + \varepsilon^{val}$. (Validation dataset model). 4. Combine $B^{val}$ and $B^{2sls}$ using the generalized method of moments (GMM). Advantages: Accurate. Sometimes robust if biased predictor $g(k)$ is biased. In theory, flexible to any models that can be fit using GMM. Disadvantages: Implementation (`{predictionError}`) is new. API is cumbersome and only supports linear models. Not robust if $E(w\varepsilon) \ne 0$. GMM may be unfamiliar to audiences. ] --- layout:false ### .border[Testing attention bias correction]
I've run simulations to test these approaches in several scenarios. The model is not very good: about 70% accurate. Most plausible scenario: y is continuous and normal-ish. -- $x$ is binary (human labels) $P(x)=0.5$. -- $w$ is the *continuous predictor* (e.g., probability) output of $f(x)$ (not binary predictions). -- if $w$ is binary, most methods struggle, but regression calibration and 2SLS+GMM can do okay. --- layout:false ### .border[Example 1: estimator of the effect of x] .right-column[ ```{r echo=FALSE, message=FALSE, warning=FALSE, result='asis', dev='svg', fig.width=7.5, fig.asp=.625,cache=F} #plot.df <- plot.df <- plot.df.example.1[,':='(method=factor(method,levels=c("Naive","Multiple imputation", "Multiple imputation (Classifier features unobserved)","Regression Calibration","2SLS+gmm","Feasible"),ordered=T), N=factor(N), m=factor(m))] plot.df <- plot.df[(variable=='x') & (m != 1000) & (m!=500) & (N!=10000) & !is.na(p.true.in.ci) & (method!="Multiple imputation (Classifier features unobserved)")] p <- ggplot(plot.df, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method)) p <- p + geom_hline(aes(yintercept=0.2),linetype=2) p <- p + geom_pointrange() + facet_grid(m~N,as.table=F) + scale_x_discrete(labels=label_wrap_gen(4)) print(p) # get gtable object ``` ] .left-column[ All methods work in this scenario Multiple imputation is inefficient. ] --- ### .border[What about bias?] .left-column[ .large[![](images/example_2_dag.png)] ] .right-column[ A few notes on this scenario. $B_x = 0.2$, $B_g=-0.2$ and $sd(\varepsilon)=3$. So the signal-to-noise ratio is high. $r$ can be concieved of as a missing feature in the predictive model $g(k)$ that is also correlated with $y$. For example $r$ might be the *race* of a commentor, $x$ could be *racial harassment*, $y$ whether the commentor gets banned and $k$ only has textual features but human coders can see user profiles to know $r$. ] --- layout:false ### .border[Example 2: Estimates of the effect of x ] .center[ ```{r echo=FALSE, message=FALSE, warning=FALSE, result='asis', dev='svg', fig.width=8, fig.asp=.625,cache=F} #plot.df <- plot.df <- plot.df.example.2B[,':='(method=factor(method,levels=c("Naive","Multiple imputation", "Multiple imputation (Classifier features unobserved)","Regression Calibration","2SLS+gmm","Feasible"),ordered=T), N=factor(N), m=factor(m))] plot.df <- plot.df[(variable=='x') & (m != 1000) & (m!=500) & (N!=10000) & !is.na(p.true.in.ci) & (method!="Multiple imputation (Classifier features unobserved)")] p <- ggplot(plot.df, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method)) p <- p + geom_hline(aes(yintercept=0.2),linetype=2) p <- p + geom_pointrange() + facet_grid(m~N,as.table=F) + scale_x_discrete(labels=label_wrap_gen(4)) print(p) # get gtable object ``` ] --- layout:false ### .border[Example 2: Estimates of the effect of r] .center[ ```{r echo=FALSE, message=FALSE, warning=FALSE, result='asis', dev='svg', fig.width=8, fig.asp=.625,cache=F} #plot.df <- plot.df <- plot.df.example.2B[,':='(method=factor(method,levels=c("Naive","Multiple imputation", "Multiple imputation (Classifier features unobserved)","Regression Calibration","2SLS+gmm","Feasible"),ordered=T), N=factor(N), m=factor(m))] plot.df <- plot.df[(variable=='g') & (m != 1000) & (m!=500) & (N!=10000) & !is.na(p.true.in.ci) & (method!="Multiple imputation (Classifier features unobserved)")] p <- ggplot(plot.df, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method)) p <- p + geom_hline(aes(yintercept=-0.2),linetype=2) p <- p + geom_pointrange() + facet_grid(m~N,as.table=F) + scale_x_discrete(labels=label_wrap_gen(4)) print(p) ``` ] --- layout:false class:large ###.border[Takeaways from example 2] Bias in the predictive model creates bias in hypothesis tests. -- Bias can be corrected *in this case*. -- The next scenario has bias that's more tricky. -- Multiple imputation helps, but doesn't fully correct the bias. --- layout:false ### .border[When will GMM+2SLS fail?] .large[.left-column[![](images/example_3_dag.png)]] .right-column[The catch with GMM: .emph[Exclusion restriction:] $E[w \varepsilon] = 0$. The restriction is violated if a variable $U$ causes both $K$ and $Y$ and $X$ causes $K$ (not visa-versa). ] ??? GMM optimizes a model to a system of equations of which the exclusion restriction is one. So if that assumption isn't true it will biased. This is a different assumption than that of OLS or GLM models. --- layout:false ### .border[Example 3: Estimates of the effect of x] .center[ ```{r echo=FALSE, message=FALSE, warning=FALSE, result='asis', dev='svg', fig.width=8, fig.asp=.625,cache=F} #plot.df <- plot.df <- plot.df.example.3[,':='(method=factor(method,levels=c("Naive","Multiple imputation", "Multiple imputation (Classifier features unobserved)","Regression Calibration","2SLS+gmm","Feasible"),ordered=T), N=factor(N), m=factor(m))] plot.df <- plot.df[(variable=='x') & (m != 1000) & (m!=500) & (N!=10000) & (method!="Multiple imputation (Classifier features unobserved)")] p <- ggplot(plot.df, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method)) p <- p + geom_hline(aes(yintercept=0.2),linetype=2) p <- p + geom_pointrange() + facet_grid(m~N,as.table=F) + scale_x_discrete(labels=label_wrap_gen(4)) print(p) ``` ] --- ### .border[Takaways] - Attenuation bias can be a big problem with noisy predictors—leading to small and biased estimates. - For more general hypothesis tests or if the predictor is biased, measurement error can lead to false discovery. - It's fixable with validation data—you may not need that much and you should already be getting it. - This means it can be okay poor predictors for hypothesis testing. - The ecosystem is underdeveloped, but a lot of methods have been researched. - Take advantage of machine learning + big data and get precise estimates when the signal-to-noise ratio is high! --- layout:false ### .border[Future work: Noise in the *outcome*] I've been focusing on noise in *covariates.* What if the predictive algorithm is used to measure the *outcome* $y$? -- This isn't a problem in the simplest case (linear regression with homoskedastic errors). Noise in $y$ is projected into the error term. -- Noise in the outcome is still a problem if errors are heteroskedastic and for GLMs / non-linear regression (e.g., logistic regression). -- Multiple imputation (in theory) could help here. The other method's aren't designed for this case. -- Solving this problem could be an important methodological contribution with a very broad impact. --- # .border[Questions?] Links to slides:[html](https://teblunthuis.cc/~nathante/slides/ecological_adaptation_ica_2022.html) [pdf](https://teblunthuis.cc/~nathante/slides/ecological_adaptation_ica_2022.pdf) Link to a messy git repository:[https://code.communitydata.science/ml_measurement_error_public.git](https://code.communitydata.science/ml_measurement_error_public.git) nathan.teblunthuis@northwestern.edu @groceryheist [https://communitydata.science](https://communitydata.science)