カテゴリ型の説明変数

Rで学ぶ回帰入門

Richie Cotton

Data Evangelist at DataCamp

魚データセット

  • 各行は1匹の魚を表します。
  • データは128行です。
  • 魚は4種です。
species mass_g
Bream 242.0
Perch 5.9
Pike 200.0
Roach 40.0
... ...
Rで学ぶ回帰入門

数値1・カテゴリ1の可視化

library(ggplot2)

ggplot(fish, aes(mass_g)) +
  geom_histogram(bins = 9) +
  facet_wrap(vars(species))

魚の重さに対する度数のファセット付きヒストグラム。各パネルは種(bream、perch、pike、roach)を示す。

Rで学ぶ回帰入門

要約統計: 種別の平均質量

fish %>% 
  group_by(species) %>% 
  summarize(mean_mass_g = mean(mass_g))
# A tibble: 4 x 2
  species mean_mass_g
  <chr>         <dbl>
1 Bream          618.
2 Perch          382.
3 Pike           719.
4 Roach          152.
Rで学ぶ回帰入門

線形回帰

lm(mass_g ~ species, data = fish)
Call:
lm(formula = mass_g ~ species, data = fish)

Coefficients:
 (Intercept)  speciesPerch   speciesPike  speciesRoach  
       617.8        -235.6         100.9        -465.8 
Rで学ぶ回帰入門

切片なし

lm(mass_g ~ species + 0, data = fish)
Call:
lm(formula = mass_g ~ species + 0, data = fish)

Coefficients:
speciesBream  speciesPerch   speciesPike  speciesRoach  
       617.8         382.2         718.7         152.0 
Rで学ぶ回帰入門

練習しましょう!

Rで学ぶ回帰入門

Preparing Video For Download...