10  Tutorat Woche 3

pacman::p_load("sjPlot", "texreg", "haven", "readr", "readxl", "fixest", "MASS",
               "glm.predict", "scales", "tidyverse")

#Anfang - Tydiverse aktivieren
library(tidyverse)

CO2_gesetz = read.csv(choose.files())

# neue Variable "Ländlichkeit"

# Variablen zu Datensatz hinzufügen

co2_mut = CO2_gesetz %>% #Hier wird es gerade gespeichert mit df_xy = df_xy
  mutate(Ländlichkeit = (AgriculturalAreaHa / TotalAreaHa ))# mutate macht neue Variablen
# Streudiagram Ländlichkeit vs. Stimmbeteiligung

#Streudiagramm erstellen

co2_mut %>% 
  ggplot(aes(x = stimmbeteiligungInProzent, y = Ländlichkeit, color = stimmbeteiligungInProzent)) +
  geom_point() + #geom_point = Streudiagramm 
  theme_bw() +
  labs(x = "Stimmbeteiligung", y = "Ländlichkeit", color = "Stimmbeteiligung") + 
  geom_smooth(show.legend = TRUE, method = lm, color = "red")

lm(stimmbeteiligungInProzent ~ Ländlichkeit, co2_mut)

mean1 = mean(co2_mut$stimmbeteiligungInProzent)

co2_mut2 = co2_mut %>% 
  filter(Ländlichkeit != Inf) #In TUtorat fragen, ob Undendlichkeit rausfiltern Sinn macht.

mean1 = mean(co2_mut2$stimmbeteiligungInProzent)

mean2 = mean(co2_mut2$Ländlichkeit)

co2_mut2 %>% 
  ggplot(aes(x = stimmbeteiligungInProzent, y = Ländlichkeit, color = stimmbeteiligungInProzent)) +
  geom_point() + #geom_point = Streudiagramm 
  theme_bw() +
  labs(x = "Stimmbeteiligung", y = "Ländlichkeit", color = "Stimmbeteiligung") + 
  geom_smooth(show.legend = TRUE, method = lm, color = "red") +
  geom_vline(xintercept = mean1, color = "yellow") +
  geom_hline(yintercept = mean2, color = "green")

# Aufgabe 2 wieder Streudiagramm

co2_mut2 %>% 
  ggplot(aes(x = AgriculturalAreaHa, y = jaStimmenInProzent, color = stimmbeteiligungInProzent)) +
  geom_point() + #geom_point = Streudiagramm 
  theme_bw() +
  labs(x = "Fläche Agrarisch genutzt", y = "Ja Stimmen in %", color = "Ja Stimmen in %") +
  geom_smooth(show.legend = TRUE, method = lm, color = "pink")

# Aufgabe 3

mean_turnout = read.csv(choose.files())

joined = left_join(
  co2_mut2,
  mean_turnout[, c("mun_id", "mean_turnout_pct")],
  by = "mun_id"
)

# Stimmbeteiligung Vergleichen

joined %>% 
  ggplot(aes(x = stimmbeteiligungInProzent, y = mean_turnout_pct, color = "Regressionsgerade", size = Ländlichkeit)) +
  geom_point(alpha = 0.1)+ #geom_point = Streudiagramm 
  theme_bw() +
  labs(x = "CO2 Stimmbeteiligung", y = "Vorherige Sechs Abstimmungen", color = "CO2 Stimmbeteiligung") +
  geom_smooth(show.legend = TRUE, method = lm, color = "orange")
# es sieht korrekt aus, aber wird wirklich das gefragt??? -> Tutorat fragen!!

#Aufgabe 4

# Filtern in 1000er Schritte
co2_mut3 = co2_mut2 %>% #Hier wird es gerade gespeichert mit df_xy = df_xy
  mutate(anzahlStimmberechtigte = round(anzahlStimmberechtigte / 1000) * 1000) # mutate macht neue Variablen

# Regression
co2_mut3 %>% 
  ggplot(aes(y = stimmbeteiligungInProzent, x = anzahlStimmberechtigte, color = "Regressionsgerade")) +
  geom_point(alpha = 1)+ #geom_point = Streudiagramm 
  theme_bw() +
  labs(x = "CO2 Stimmbeteiligung", y = "ANzahl Stimmberechtigte", color = "CO2 Stimmbeteiligung") +
  geom_smooth(show.legend = TRUE, method = lm, color = "orange")


#ignore
streu1 = co2_mut3 %>% 
  ggplot(aes(y = stimmbeteiligungInProzent, x = anzahlStimmberechtigte, color = "Regressionsgerade")) +
  geom_point(alpha = 1)+ #geom_point = Streudiagramm 
  theme_bw() +
  labs(y = "CO2 Stimmbeteiligung", x = "ANzahl Stimmberechtigte", color = "CO2 Stimmbeteiligung") +
  geom_smooth(show.legend = TRUE, method = lm, color = "orange") + ggtitle("Mit Stadt ZH")

# filter nach Zürich
co2_mut4 = co2_mut3 %>% 
  filter(mun_name != "Zürich") 

# richtig
co2_mut3 %>% 
  ggplot(aes(y = stimmbeteiligungInProzent, x = anzahlStimmberechtigte, color = "Regressionsgerade")) +
  geom_point(alpha = 1) + #geom_point = Streudiagramm 
  theme_bw() +
  labs(y = "CO2 Stimmbeteiligung", x = "ANzahl Stimmberechtigte", color = "CO2 Stimmbeteiligung") +
  geom_smooth(show.legend = TRUE, method = lm, color = "blue", data = co2_mut3 %>% 
                filter(mun_name != "Zürich") ) + 
  geom_smooth(show.legend = TRUE, method = lm, color = "orange") +
  ggtitle("Vergleich Stadt ZH / ohne Stadt ZH")

# koeffizienten
lm(stimmbeteiligungInProzent ~ anzahlStimmberechtigte, co2_mut3)
lm(stimmbeteiligungInProzent ~ anzahlStimmberechtigte, co2_mut4)

# ignorieren
library(cowplot)
plot_grid(streu1, streu2, ncol = 1)

# Da weitermachen im TUTORAT -> was genau wie soll ich machen???
lm(stimmbeteiligungInProzent ~ co2_mut4, mun_id)


predict(modell1, newdata = data.frame(anzahlStimmberechtigte = Lugano))

# lm Syntax sehr wichtig!!!


# Lugano

# AnzahlStimmberechtige (mit lineare Regression)

64.511 + (-0.0002161)* 35000

# 28 % Stimmbeteiligung

28 - 44

64.511 + (-0002161) * 500000

# ich traue dem nicht
?

Lernuhr
Uhrzeit
Lerntimer