knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(bis557)
library(palmerpenguins)
library(DT)

data(penguins)

datatable(penguins)



Above is the penguins data set form the {palmerpenguins} package.

Selecting variables and creating a table

library(dplyr)
library(ggplot2)

# The "base way".
table(penguins$species, penguins$island)


# The "tidy" way.
penguins %>%
  select(species, island)%>%
  table()

Filtering rows

base

table(penguins[penguins$species == "Adelie", "sex"])

tidy

penguins%>%
  filter(species == "Adelie")%>%
  select(sex)%>%
  table()

Building a model

Let's fit the model:

$$ \text{bill_length_mm} \sim \text{specied} \, \ beta_1 + \text{island} \beta_2 +\text{sex} \beta_3 + \beta_0 $$

To fit the medol we'll run the following code:

fit<-lm(bill_length_mm~ species+island+sex, data = penguins)
summary(fit)
qqnorm(fit$residuals)

Plot the residuals

library(ggplot2)
# base
qs<-qqnorm(fit$residuals,plot.it = FALSE)
qsd<-as.data.frame(qs)
ggplot(qsd, aes(x=x, y=y))+
  geom_point()+
  ylab("Sample Quantiles")+
  xlab("Theoretical Quantiles")+
  theme_minimal()
# tidy
qqnorm(fit$residuals, plot.it = FALSE)%>%
  as_tibble()%>%
  ggplot(aes(x=x, y=y)) +
  geom_point()+
  ylab("Sample Quantiles")+
  xlab("Theoretical Quantiles")+
  theme_minimal()


xy272/bis557 documentation built on Sept. 18, 2020, 12:57 p.m.