0
$\begingroup$

I was analyzing a dataset that contains data from a baking experiment (cakes data) from alr4 R package. The Y variable or the response is a “palatability score” (higher is tastier) and the explanatory variables baking time in minutes, X1 and baking temperature in degrees Fahrenheit, X2. Let us ignore the block variable.

mod = lm(Y~X1 * X2, data = cakes)

I fit a linear model and using ggplot, I plotted the two graphs: X1 and .resid and X2 and .resid and see linear regression is not appropriate to model the score.

Question: How can one know that the linear regression is not appropriate to model the score even before looking at the data?

$\endgroup$
8
  • 1
    $\begingroup$ In this case, I would make a 3D scatterplot of X1, X2, and Y to see if the data appears to lie on a flat plane. If visual inspection reveals that the data does not lie on a flat plane, that would indicate to me that a simple model of the form "Y = a * X1 + b * X2 + c" is not optimal. $\endgroup$ Commented Jun 4, 2019 at 23:26
  • 1
    $\begingroup$ Second that! The first steps should be understanding the data generating process and checking the data. These will probably involve (several) plots, and maybe writing code to do more complicated checks or transformations. There will often be iteration between these steps as data is rarely correct or in the right form for analysis. $\endgroup$
    – Sam Mason
    Commented Jun 4, 2019 at 23:41
  • 2
    $\begingroup$ It's worth mentioning that the example in the question has an interaction between X1 and X2, which would mean a curved planed in the 3D scatterplot. $\endgroup$
    – Marius
    Commented Jun 4, 2019 at 23:44
  • $\begingroup$ @Marius Right. I think the word "before looking at the data" confused me. I was trying to guess the type of regression by seeing the predictor variables or their type. $\endgroup$
    – Matthew
    Commented Jun 4, 2019 at 23:52
  • 2
    $\begingroup$ I think the answer to the question as written is "you can't". Looking at the data is the first step in every analysis. $\endgroup$
    – neilfws
    Commented Jun 5, 2019 at 0:05

2 Answers 2

4
$\begingroup$

You might start by looking at the data. This suggests that 35 minutes @ 350 is ideal, but you can get similar results going hotter shorter or if you give it a few more minutes. But it's not monotonically increasing in either dimension.

cakes <- alr4::cakes
cakes %>% 
  ggplot(aes(X1, X2)) + 
  # I used geom_point initially, but then realized the central point has multiple
  #   measurements which are individually perceptible using geom_jitter.
  geom_jitter(aes(color = Y), size = 5, width = 0.1, height = 2) +
  scale_color_viridis_c(direction = -1)

enter image description here

Consequently, a linear model doesn't perform particularly well...

> mod = lm(Y ~ X1 * X2, data = cakes)
> broom::glance(mod)
# A tibble: 1 x 11
  r.squared adj.r.squared sigma statistic p.value    df logLik   AIC   BIC deviance df.residual
      <dbl>         <dbl> <dbl>     <dbl>   <dbl> <int>  <dbl> <dbl> <dbl>    <dbl>       <int>
1     0.507         0.359  1.19      3.42  0.0606     4  -19.9  49.9  53.1     14.1          10

...and has messy residuals. We see here that the model error tends to be worst near the middle, where some cakes with central X1 and X2 values near the sweet spot are often underrated by this model, while cakes with values on the sides tend to be underrated.

broom::augment(mod) %>%
  select(X1:X2, .resid) %>%
  gather(col, val, -.resid) %>%
  ggplot(aes(val, .resid)) + geom_point() + 
  facet_grid(~col, scales = "free_x")

enter image description here

One approach would be to use a polynomial fit -- this should capture more of the "sweet spot" phenomenon here. Sure enough, this would provide a much higher adjusted r^2 and a lower AIC and BIC, suggesting it's a much better model, even after penalizing for using more terms.

> mod_quad = lm(formula = Y ~ poly(X1, 2) * poly(X2, 2), data = cakes)
> broom::glance(mod_quad)
# A tibble: 1 x 11
  r.squared adj.r.squared sigma statistic  p.value    df logLik   AIC   BIC deviance df.residual
      <dbl>         <dbl> <dbl>     <dbl>    <dbl> <int>  <dbl> <dbl> <dbl>    <dbl>       <int>
1     0.983         0.956 0.311      36.3 0.000517     9   3.68  12.6  19.0    0.485           5
$\endgroup$
0
$\begingroup$

You could separate your dataset into at least two parts, assuming your dataset is large enough. Do your exploratory analysis on one part. You could use it to decide what kind of a model would be most appropriate. You can then use the remaining part of the dataset to verify your hypothesis.

This way, in a sense, you get to decide on your model before looking at the data that you would use to draw conclusions about your model.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.