パッケージ

library(magrittr)
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() ──
## ✖ tidyr::extract()   masks magrittr::extract()
## ✖ dplyr::filter()    masks stats::filter()
## ✖ dplyr::lag()       masks stats::lag()
## ✖ purrr::set_names() masks magrittr::set_names()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test

データ読み込み

PY <- 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.
PM  <- read_csv("/Users/myang/Desktop/23A/社会調査/社会調査/data/PM110.csv")
## Rows: 1433 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.
JLPS <- rbind(PY,PM)
  1. 社研パネル問い35「主観的階層帰属意識」について,95%信頼区間を求めなさい(“99”を欠損値に変換して除くこと).
JLPS$ZQ35[JLPS$ZQ35==99] <- NA_real_

サンプルサイズ

n <- sum(!is.na(JLPS$ZQ35))

平均

x_bar <- mean(JLPS$ZQ35, na.rm = TRUE)

標準偏差

sigma <- sd(JLPS$ZQ35, na.rm = TRUE)  

信頼区間

ll <- x_bar + qnorm(0.025) * sigma / sqrt(n)
ul <- x_bar + qnorm(0.975) * sigma / sqrt(n)
list("タイトル" = "主観的階層帰属意識", 
     "点推定" = x_bar,
     "区間推定" = c(ll,ul))
## $タイトル
## [1] "主観的階層帰属意識"
## 
## $点推定
## [1] 5.932625
## 
## $区間推定
## [1] 5.882476 5.982774
  1. サイズが10の調査をしたところ, 標本平均が7, 標本から推定した不偏分散が1.5であった.t分布を用いて99%信頼区間を求めなさい.
ll1 <- 7 + qt(0.005, 10-1) * 1.5 / sqrt(10)
ul1 <- 7 + qt(0.995, 10-1) * 1.5 / sqrt(10)
list("タイトル" = "t分布", 
     "点推定" = 7,
     "区間推定" = c(ll1,ul1))
## $タイトル
## [1] "t分布"
## 
## $点推定
## [1] 7
## 
## $区間推定
## [1] 5.458468 8.541532
  1. サンプルサイズ20でK内閣の支持率を調査したところ,支持が5人,不支持が15人であった.

3-1. 二項分布を正規分布で近似して,支持率の95%信頼区間を求めなさい.

x_bar2 <- 5/20 #支持=1、不支持=0
sigma2 <- sqrt(x_bar2*(1-x_bar2)/20)   
ll2 <- x_bar2 + qnorm(0.025) * sigma2
ul2 <- x_bar2 + qnorm(0.975) * sigma2
list("タイトル" = "K内閣の支持率", 
     "点推定" = x_bar2,
     "区間推定" = c(ll2,ul2))
## $タイトル
## [1] "K内閣の支持率"
## 
## $点推定
## [1] 0.25
## 
## $区間推定
## [1] 0.0602273 0.4397727

3-2. 二項分布を用いた正確な95%信頼区間をRの関数を用いて計算しなさい.

binom.test(5,20)
## 
##  Exact binomial test
## 
## data:  5 and 20
## number of successes = 5, number of trials = 20, p-value = 0.04139
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
##  0.08657147 0.49104587
## sample estimates:
## probability of success 
##                   0.25