library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ 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
getwd()
## [1] "C:/Users/홍지민/Desktop/24-1/데이터/PS2"
"C:/Users/홍지민/Desktop/24-1/데이터/PS2"
## [1] "C:/Users/홍지민/Desktop/24-1/데이터/PS2"
setwd("C:/Users/홍지민/Desktop/24-1/데이터/PS2")
leaders <- read.csv("leaders.csv")
#1
library(ggplot2)
ggplot(data = leaders, aes(x = year, y = polityafter, label = country)) +
geom_point() +
geom_smooth() +
geom_text(size = 2)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: The following aesthetics were dropped during statistical transformation: label.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
#2
library(forcats)
library(dplyr)
leaders <- leaders %>%
mutate(result = fct_recode(result,
"dies" = "dies between a day and a week",
"dies" = "dies within a day after the attack",
"dies" = "dies between a week and a month",
"dies" = "dies, timing unknown",
"survives" = "hospitalization but no permanent disability",
"survives" = "not wounded",
"survives" = "plot stopped",
"survives" = "survives but wounded severely",
"survives" = "survives, whether wounded unknown",
"survives" = "wounded lightly")) %>%
mutate(success = ifelse(result == "dies", 1, 0))
success_rate <- mean(leaders$success)
Overall success rate is 21.6%.
#3
library(ggplot2)
library(dplyr)
successful_attempts <- filter(leaders, success == 1)
failed_attempts <- filter(leaders, success == 0)
ggplot(data = leaders) +
geom_point(mapping = aes(x = 0, y = politybefore, color = factor(success))) +
labs(title = "Polity Scores Over 3 Years Prior to Assassination Attempt",
x = "Success of Assassination Attempt",
y = "Polity Score 3 Years Before",
color = "Success")
One that has failed, thus survives tend to have higher polity scores.
summary(successful_attempts$politybefore)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -10.0000 -6.0000 -3.0000 -0.7037 5.5000 10.0000
summary(failed_attempts$politybefore)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -10.000 -7.000 -3.000 -1.743 3.000 10.000
sd(successful_attempts$politybefore)
## [1] 6.541808
sd(failed_attempts$politybefore)
## [1] 6.417421
However, although they have same minimum value of -10 and maximum value of 10, mean for successful attempts is -0.7037 and mean for failed attempts is -1.743. However, since the standard deviation for successful attempt is 6.54 and failed attempt is 6.42, it doesn’t have major difference.
#4
successful_change <- mean(successful_attempts$polityafter - successful_attempts$politybefore)
failed_change <- mean(failed_attempts$polityafter - failed_attempts$politybefore)
No. There is only 0.059 difference between after and before the assassination. Also, even though the assassination fails, there is 0.15 difference for the average polity scores.