ライブラリ

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readr)
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## 
## The following object is masked from 'package:dplyr':
## 
##     recode
## 
## The following object is masked from 'package:purrr':
## 
##     some
library(magrittr)
## 
## Attaching package: 'magrittr'
## 
## The following object is masked from 'package:purrr':
## 
##     set_names
## 
## The following object is masked from 'package:tidyr':
## 
##     extract
library(gtsummary)
## #BlackLivesMatter
library(psych)
## 
## Attaching package: 'psych'
## 
## The following object is masked from 'package:car':
## 
##     logit
## 
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(vcd)
## Loading required package: grid

ワーキングディレクトリの設定

PY110 <- read_csv("/Users/myang/Desktop/23A/社会調査/社会調査/data/PY110.csv")
## Rows: 3367 Columns: 5377
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (5377): PanelID, BLOCK, SIZE, sex, ybirth, mbirth, ZQ02A, zq02ay48, zq02...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
dim(PY110)
## [1] 3367 5377

1. 仕事満足度と生活満足度について,1-2(high),3(middle),4-5(low)の3カテゴリに分けて、クロス表を作ってください。

PY110 %<>%
  mutate(satisfaction_work = case_when(
    ZQ30A %in% 1:2 ~ 1,
    ZQ30A %in% 3 ~ 2,
    ZQ30A %in% 4:5 ~ 3,
    TRUE ~ NA_real_),
    satisfaction_work = factor(satisfaction_work,
                  levels = 1:3,
                  labels = c('high','middle','low')
    ),
  )

PY110 %<>%
  mutate(satisfaction_life = case_when(
    ZQ30D %in% 1:2 ~ 1,
    ZQ30D %in% 3 ~ 2,
    ZQ30D %in% 4:5 ~ 3,
    TRUE ~ NA_real_),
    satisfaction_life = factor(satisfaction_life,
                               levels = 1:3,
                               labels = c('high','middle','low')
    ),
  )

table <- table(PY110$satisfaction_work, PY110$satisfaction_life)
table
##         
##          high middle low
##   high    977    175  51
##   middle  380    316  99
##   low     258    241 255

2. ライブラリvcdを用いて、モザイクプロットを書き、クラメールのVを計算してください。

mosaic(~ satisfaction_work + satisfaction_life,shade = TRUE, data = PY110)

assocstats(table(PY110$satisfaction_work, PY110$satisfaction_life))
##                     X^2 df P(> X^2)
## Likelihood Ratio 596.12  4        0
## Pearson          602.01  4        0
## 
## Phi-Coefficient   : NA 
## Contingency Coeff.: 0.424 
## Cramer's V        : 0.331

3. 分析結果から分かることを記述してください。 クラメールの連関係数で示されたように、仕事満足度と生活満足度の間には0.331と、中程度の正の相関関係が見られる。モザイクプロットからもある程度の関連が見られる。具体的にいうと、生活満足度の上昇に連れ、仕事満足度も高くなる。

4. 追加分析(健康状態と生活満足度)

PY110 %<>%
  mutate(health = case_when(
    ZQ25 %in% 1:2 ~ 1,
    ZQ25 %in% 3 ~ 2,
    ZQ25 %in% 4:5 ~ 3,
    TRUE ~ NA_real_),
    health = factor(health,
                               levels = 1:3,
                               labels = c('good','middle','bad')
    ),
  )

table <- table(PY110$satisfaction_work, PY110$health)
table
##         
##          good middle bad
##   high    738    392  74
##   middle  343    373  81
##   low     289    324 139
mosaic(~ satisfaction_work + health,shade = TRUE, data = PY110)

assocstats(table(PY110$satisfaction_work, PY110$health))
##                     X^2 df P(> X^2)
## Likelihood Ratio 149.49  4        0
## Pearson          152.94  4        0
## 
## Phi-Coefficient   : NA 
## Contingency Coeff.: 0.229 
## Cramer's V        : 0.167

5. 追加分析の分析結果 生活満足度と本人の健康状態の間には0.167と、弱い関連がある。具体的にいうと、健康状態の向上に連れ、生活満足度も高くなる。