I'm working through Faraway's 2016 book Extending the Linear Model with R and have encountered an issue with the code that I don't know how to fix. Here is the relevant syntax leading up to the error:
#### Load Data & Libraries ####
library(faraway)
library(tidyverse)
data(wcgs)
#### Add Variables ####
wcgs$y <- ifelse(wcgs$chd == "no",0,1) # create binary response from chd
wcgs$bmi <- with(wcgs,
703*wcgs$weight/(wcgs$height^2)) # create BMI variable
#### Create GLM Model ####
lmod <- glm(chd ~ height + cigs,
family = binomial,
wcgs)
#### Mutate Data ####
wcgs <- mutate(wcgs,
residuals=residuals(lmod),
linpred=predict(lmod)) # create residuals/pred values
And this is the part where the error arises (the third line which includes a mutate
function:
#### Error Code (Last Line) ####
wcgsm <- na.omit(wcgs) # omit NA values
wcgsm <- mutate(wcgsm,
predprob=predict(lmod,
type="response")) # make pred data
gdf <- group_by(wcgsm,
cut(linpred,
breaks=unique(quantile(linpred,
(1:100)/101)))) # bin NA
Which gives me this error:
Error in `group_by()`:
! Problem adding computed columns.
Caused by error in `mutate()`:
! Problem while computing `..1 = cut(linpred, breaks = unique(quantile(linpred,
(1:100)/101)))`.
✖ `..1` must be size 3140 or 1, not 3154.
I dont understand what this error means. When I run dim(wcgs)
, I get there are 3154 rows, and when I run dim(na.omit(wcgs))
I get 3140 rows. The only thing I can think of is that the predicted model values dont line up with the new na.omit data, but I'm not sure now how to work around that given the rest of this chapter uses this data manipulation.