Kapittel 7 Appendiks: R-kode

if(!require("broom")){install.packages("broom");  library(broom)}
if(!require("here")){install.packages("here");  library(here)}
if(!require("haven")){install.packages("haven");  library(haven)}
if(!require("kableExtra")){install.packages("kableExtra");  library(kableExtra)}
if(!require("knitr")){install.packages("knitr");  library(knitr)}
if(!require("naniar")){install.packages("naniar");  library(naniar)}
if(!require("patchwork")){install.packages("patchwork");  library(patchwork)}
if(!require("quanteda")){install.packages("quanteda");  library(quanteda)}
if(!require("readxl")){install.packages("readxl");  library(readxl)}
if(!require("sjlabelled")){install.packages("sjlabelled");  library(sjlabelled)}
if(!require("spacyr")){install.packages("spacyr");  library(spacyr)}
if(!require("stringr")){install.packages("stringr");  library(stringr)}
if(!require("srvyr")){install.packages("srvyr");  library(srvyr)}
if(!require("text2vec")){install.packages("text2vec");  library(text2vec)}
if(!require("tidytext")){install.packages("tidytext");  library(tidytext)}
if(!require("tidyverse")){install.packages("tidyverse");  library(tidyverse)}

ggsave2 <- function(filename, ...) {
  for (format in c(".pdf", ".png")) ggsave(filename = paste0("output/", filename, format), ...)
}

knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, digits = 0)
load('Data/demovate.Rdata')
# automatically create a bib database for R packages
knitr::write_bib(c(
  .packages(), 'bookdown', 'knitr', 'rmarkdown'
), 'packages.bib')

#Alder
alder <- bakgrunn %>%
select(Q32) %>%
filter(!is.na(Q32))

p1 <- ggplot(alder, aes(x = factor(Q32))) +
geom_bar(aes(y = (..count..) / sum(..count..))) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1L), breaks = seq(0,1, by = 0.2)) +
scale_x_discrete(labels = c('<1940', '1940-1949', '1950-1959', '1960-1969', '1970-1979', '1980-1989', '>1989'), guide = guide_axis(n.dodge = 2)) + #LABEL OK
labs(title = "Aldersfordeling", x = '', y = '', fill = '') +
theme_classic() +
scale_fill_grey(start = 0.8, end = 0)

# Kjønn
kjonn <- bakgrunn %>%
select(Q31) %>%
filter(!is.na(Q31))

p2 <- ggplot(kjonn, aes(x = factor(Q31))) +
geom_bar(aes(y = (..count..) / sum(..count..))) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1L), breaks = seq(0,1, by = 0.5)) +
scale_x_discrete(labels = c('Kvinne', 'Mann')) + #LABEL OK
labs(title = "Kjønnsfordeling", x = '', y = '', fill = '') +
theme_classic() +
scale_fill_grey(start = 0.8, end = 0)

patchwork <- p1 / p2
patchwork + plot_annotation(title = 'Deltakernes bakgrunn', subtitle = 'Alder og kjønn')

# Statsborgerskap
statsborger <- bakgrunn %>%
select(Q33) %>%
filter(!is.na(Q33))

p3 <- ggplot(statsborger, aes(x = factor(Q33))) +
geom_bar(aes(y = (..count..) / sum(..count..))) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1L), breaks = seq(0,1, by = 0.75)) +
scale_x_discrete(labels = c('Norsk statsborger', 'Statsborger i annet land', 'Begge deler'), guide = guide_axis(n.dodge = 2)) + 
labs(title = "Statsborgerskap", x = '', y = '', fill = '') +
theme_classic() +
scale_fill_grey(start = 0.8, end = 0)

#Inntekt
inntekt <- bakgrunn %>%
select(Q30) %>%
filter(!is.na(Q30))

p4 <- ggplot(inntekt, aes(x = factor(Q30))) +
geom_bar(aes(y = (..count..) / sum(..count..))) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1L), breaks = seq(0,1, by = 0.2)) +
scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8'),  labels = c('<150K', '150K-300K', '300K-400K', '400K-500K', '500K-600K', '600K-700k', '700K-800K', '800K-900K', '900K-1M', '>1M'), guide = guide_axis(n.dodge = 3)) + 
labs(title = "Inntekt", x = '', y = '', fill = '') +
theme_classic() +
scale_fill_grey(start = 0.8, end = 0)

#Bydel
bydel <- bakgrunn %>%
select(Q27) %>%
filter(!is.na(Q27))

p5 <- ggplot(bydel, aes(x = factor(Q27))) +
geom_bar(aes(y = (..count..) / sum(..count..)),
position = position_dodge()) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1L), breaks = seq(0,1, by = 0.25)) +
scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8'), labels = c('Arna', 'Åsane', 'Ytrebygda', 'Fana', 'Laksevåg', 'Fyllingsdalen', 'Årstad', 'Bergenhus'), guide = guide_axis(n.dodge = 2)) +
labs(title = "Bosted", x = '', y = '', fill = '') +
theme_classic() +
scale_fill_grey(start = 0.8, end = 0)
 
patchwork <- p3 / p4 / p5
patchwork + plot_annotation(title = 'Deltakernes bakgrunn', subtitle = 'Bosted, inntekt og statsborgerskap' )


dagsplan <- tibble(Start = c('10:00', '10:10', '11:00', '11:50', '12:30', '13:15', '14:10', '15:05', '15:50'),
                     Stopp = c('10:10', '10:55', '11:45', '12:30', '13:15', '14:00', '14:55', '15:50', '16:00'),
                         Aktivitet = c('Velkommen', "Dokken: Første gruppesamling", "Panel", "Dokken: Andre gruppesamling", "Lunsj", "Bilfrie områder: Første gruppesamling", "Panel", "Bilfrie områder: Andre gruppesamling", 'Avslutning'),
                   beskrivelse = c('',
                                   'Opptelling av deltakere, gjennomgang av agenda, forberede spørsmål til panel',
                                   'Moderator:  Anne Lise Fimreite. Paneldeltakere: Anders Nyland, Johanne Gillow, Gro Sandkjær Hanssen, Erling Dokk Holm',
                                   'Debrief og videre diskusjon i gruppen', ' ', 
                                   'Gjennomgang av agenda, forberede spørsmål til panel',
                                   'Moderator: Jacob Aars. Paneldeltakere: Oddrun Hagen, Håvard Haarstad, Rolf Knudsen',
                                   'Debrief og videre diskusjon i gruppen.', 'Avslutning')
                   )

dagsplan  %>%  select (-beskrivelse) %>% kable(linesep = "",
      booktabs = T, 
      escape = F) %>% 
  kable_styling() %>% 
  collapse_rows(3)  
  
knitr::include_graphics("ordsky-dokken.png")


post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q3 %in% 1:5 ~ "Mindretall",
                                          Q3 == 6 | Q3 == 12 ~ "Ikke medregnet",
                                          Q3 %in% 7:11 ~ "Flertall"))
plot <- ggplot(post_t_delib, aes(x = factor(Q3), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Cruisetrafikken ut av sentrum?", x = 'Svarfordeling fra 0 = Må bli værende til 10 = Må flyttes ut', y = 'Prosent', fill = '')  +
  theme_classic() +
  scale_fill_grey(start = 0, end = 0.9)
  
plot
post_t_delib <- post_t_delib %>% 
  filter(!is.na(Q4)) %>% 
  mutate(fill = case_when(Q4 %in% 1:5 ~ "Mindretall",
                                          Q4 == 6 | Q4 == 12 ~ "Ikke medregnet",
                                          Q4 %in% 7:11 ~ "Flertall"))
plot <- ggplot(post_t_delib, aes(x = factor(Q4), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Utenlandsferger ut av sentrum?", x = 'Svarfordeling fra 0 = Må bli værende til 10 = Må flyttes ut', y = 'Prosent', fill = '')  +
  theme_classic() +
  scale_fill_grey(start = 0, end = 0.9)
  
plot

post_t_delib <- post_t_delib %>% 
filter(!is.na(Q5)) %>%
mutate(fill = case_when(Q5 %in% 1:5 ~ "Mindretall",
                                          Q5 == 6 | Q5 == 12 ~ "Ikke medregnet",
                                          Q5 %in% 7:11 ~ "Flertall"))
plot <- ggplot(post_t_delib, aes(x = factor(Q5), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Utfylling i sjø?", x = 'Svarfordeling fra 0 = Motsetter seg til 10 = Støtter', y = 'Prosent', fill = '')  +
  theme_classic() +
  scale_fill_grey(start = 0, end = 0.9)
  
plot

post_t_delib <- post_t_delib %>% 
  filter(!is.na(Q6)) %>%
  mutate(fill = case_when(Q6 %in% 1:5 ~ "Mindretall",
                                          Q6 == 6 | Q6 == 12 ~ "Ikke medregnet",
                                          Q6 %in% 7:11 ~ "Flertall")) 

plot <- ggplot(post_t_delib, aes(x = factor(Q6), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Sjølinjen", x = 'Svarfordeling fra 0 = Fortsatt havneaktivitet, til 10 = Rekreasjon eller natur- og dyreliv', y = 'Prosent', fill = '')  +
  theme_classic() +
  scale_fill_grey(start = 0, end = 0.9)
  
plot

post_t_delib <- post_t_delib %>% 
  filter(!is.na(Q7)) %>%
  mutate(fill = case_when(Q7 %in% 1:5 ~ "Flertall",
                                          Q7 == 6 | Q7 == 12 ~ "Ikke medregnet",
                                          Q7 %in% 7:11 ~ "Mindretall")) 

plot <- ggplot(post_t_delib, aes(x = factor(Q7), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Boligpolitikk", x = 'Svarfordeling fra 0 = Sikre rimelige boliger, til 10 = Ikke blande seg inn', y = 'Prosent', fill = '')  +
  theme_classic() +
  scale_fill_grey(start = 0, end = 0.9)
  
plot

post_t_delib <- post_t_delib %>%  
filter(!is.na(Q8)) %>%
 mutate(fill = case_when(Q8 %in% 1:5 ~ "Flertall",
                                          Q8 == 6 | Q8 == 12 ~ "Ikke medregnet",
                                          Q8 %in% 7:11 ~ "Mindretall")) 

plot <- ggplot(post_t_delib, aes(x = factor(Q8), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Parker vs. idrettsanlegg", x = 'Svarfordeling fra 0 = Prioritere parker, til 10 = Prioritere idrettsanlegg', y = 'Prosent', fill = '')  +
  theme_classic() +
  scale_fill_grey(start = 0, end = 0.9)
  
plot

post_t_delib <- post_t_delib %>% 
  filter(!is.na(Q9)) %>%
  mutate(fill = case_when(Q9 %in% 1:5 ~ "Mindretall",
                                          Q9 == 6 | Q9 == 12 ~ "Ikke medregnet",
                                          Q9 %in% 7:11 ~ "Flertall")) 

plot <- ggplot(post_t_delib, aes(x = factor(Q9), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Kalle opp nye gatenavn etter kvinner", x = 'Svarfordeling fra 0 = Motsetter seg, til 10 = Støtter', y = 'Prosent', fill = '')  +
  theme_classic() +
  scale_fill_grey(start = 0, end = 0.9)
  
plot

#Gjør om "Vet ikke" til missing
demovate_all <- demovate_all %>% mutate(Q3 = na_if(Q3, 12),
Q4 = na_if(Q4, 12),
Q5 = na_if(Q5, 12),
Q6 = na_if(Q6, 12),
Q7 = na_if(Q7,12),
Q8 = na_if(Q8,12),
Q9 = na_if(Q9,12)
)
demovate_all_srv <- srvyr::as_survey_design(demovate_all, ids = id)

#Endre skalaer fra 1-11 til 0-10
demovate_all_srv <- demovate_all_srv %>% mutate(
  Q3 = case_when(Q3 == 1 ~ 0,
                                      Q3 == 2 ~ 1,
                                      Q3 == 3 ~ 2,
                                      Q3 == 4 ~ 3,
                                      Q3 == 5 ~ 4,
                                      Q3 == 6 ~ 5,
                                      Q3 == 7 ~ 6,
                                      Q3 == 8 ~ 7,
                                      Q3 == 9 ~ 8,
                                      Q3 == 10 ~ 9,
                                      Q3 == 11 ~ 10),
                       Q4 = case_when(Q4 == 1 ~ 0,
                                      Q4 == 2 ~ 1,
                                      Q4 == 3 ~ 2,
                                      Q4 == 4 ~ 3,
                                      Q4 == 5 ~ 4,
                                      Q4 == 6 ~ 5,
                                      Q4 == 7 ~ 6,
                                      Q4 == 8 ~ 7,
                                      Q4 == 9 ~ 8,
                                      Q4 == 10 ~ 9,
                                      Q4 == 11 ~ 10),
                       Q5 = case_when(Q5 == 1 ~ 0,
                                      Q5 == 2 ~ 1,
                                      Q5 == 3 ~ 2,
                                      Q5 == 4 ~ 3,
                                      Q5 == 5 ~ 4,
                                      Q5 == 6 ~ 5,
                                      Q5 == 7 ~ 6,
                                      Q5 == 8 ~ 7,
                                      Q5 == 9 ~ 8,
                                      Q5 == 10 ~ 9,
                                      Q5 == 11 ~ 10),
                       Q6 = case_when(Q6 == 1 ~ 0,
                                      Q6 == 2 ~ 1,
                                      Q6 == 3 ~ 2,
                                      Q6 == 4 ~ 3,
                                      Q6 == 5 ~ 4,
                                      Q6 == 6 ~ 5,
                                      Q6 == 7 ~ 6,
                                      Q6 == 8 ~ 7,
                                      Q6 == 9 ~ 8,
                                      Q6 == 10 ~ 9,
                                      Q6 == 11 ~ 10),
                       Q7 = case_when(Q7 == 1 ~ 0,
                                      Q7 == 2 ~ 1,
                                      Q7 == 3 ~ 2,
                                      Q7 == 4 ~ 3,
                                      Q7 == 5 ~ 4,
                                      Q7 == 6 ~ 5,
                                      Q7 == 7 ~ 6,
                                      Q7 == 8 ~ 7,
                                      Q7 == 9 ~ 8,
                                      Q7 == 10 ~ 9,
                                      Q7 == 11 ~ 10),
                       Q8 = case_when(Q8 == 1 ~ 0,
                                      Q8 == 2 ~ 1,
                                      Q8 == 3 ~ 2,
                                      Q8 == 4 ~ 3,
                                      Q8 == 5 ~ 4,
                                      Q8 == 6 ~ 5,
                                      Q8 == 7 ~ 6,
                                      Q8 == 8 ~ 7,
                                      Q8 == 9 ~ 8,
                                      Q8 == 10 ~ 9,
                                      Q8 == 11 ~ 10),
                       Q9 = case_when(Q9 == 1 ~ 0,
                                      Q9 == 2 ~ 1,
                                      Q9 == 3 ~ 2,
                                      Q9 == 4 ~ 3,
                                      Q9 == 5 ~ 4,
                                      Q9 == 6 ~ 5,
                                      Q9 == 7 ~ 6,
                                      Q9 == 8 ~ 7,
                                      Q9 == 9 ~ 8,
                                      Q9 == 10 ~ 9,
                                      Q9 == 11 ~ 10)
                       )

Q3 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q3, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q3") %>%
ungroup()

Q4 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q4, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q4") %>%
ungroup()

Q5 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q5, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q5") %>%
ungroup()

Q6 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q6, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q6") %>%
ungroup()

Q7 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q7, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q7") %>%
ungroup()

Q8 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q8, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q8") %>%
ungroup()

Q9 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q9, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q9") %>%
ungroup()

dokken <- bind_rows(list(Q3, Q4, Q5, Q6, Q7, Q8, Q9)) %>%
mutate(Undersøkelse = case_when(time == 0 ~ "Før",
time == 1 ~ "Etter"),
Gruppe = case_when(treated == 0 ~ "Kontrollgruppe",
treated == 1 ~ "Deltakergruppe"),
Sak = case_when(question == 'Q3' ~ 'Cruisetrafikk ut av sentrum',
question == 'Q4' ~ 'Utenlandsferger ut av sentrum',
question == 'Q5' ~ 'Utfylling i sjø',
question == 'Q6' ~ 'Hva sjølinjen skal brukes til',
question == 'Q7' ~ 'Boligpolitikk',
question == 'Q8' ~ 'Parker vs. idrettsanlegg',
question == 'Q9' ~ 'Nye gatenavn'))

# Gjør om Undersøkelse til faktor og endre rekkefølgen
dokken <- dokken %>%
mutate(Undersøkelse = factor(Undersøkelse,
levels = c("Før",
"Etter")))
dokken %>% arrange(Sak, Gruppe, Undersøkelse) %>%
select(Sak, Gruppe, Undersøkelse, Gj.snitt = mean) %>%
kableExtra::kable(., booktabs = T, digits = 1, caption = 'Gjennomsnitt for deltakergruppen') %>% kable_styling(c('striped', 'hover'))%>%
collapse_rows(columns = c(1, 2))
# forskjeller i gjennomsnitt for de ulike gruppene

library(broom)

# Q3
change_Q3_c <- demovate_all %>% select(time, treated, Q3) %>% 
  filter(treated == 0) 

Q3_lm_c <- lm(Q3 ~ time, data = change_Q3_c) 
Q3_lm_c <- tidy(Q3_lm_c, conf.int = TRUE) %>% 
  mutate(Question = "Q3", 
         Gruppe = "Kontrollgruppe")

change_Q3_t <- demovate_all %>% select(time, treated, Q3) %>% 
  filter(treated == 1)

Q3_lm_t <- lm(Q3 ~ time, data = change_Q3_t) 
Q3_lm_t <- tidy(Q3_lm_t, conf.int = TRUE) %>% 
mutate(Question = "Q3", 
         Gruppe = "Deltakergruppe")

# Q4

change_Q4_c <- demovate_all %>% select(time, treated, Q4) %>% 
  filter(treated == 0) 

Q4_lm_c <- lm(Q4 ~ time, data = change_Q4_c) 
Q4_lm_c <- tidy(Q4_lm_c, conf.int = TRUE) %>% 
  mutate(Question = "Q4", 
         Gruppe = "Kontrollgruppe")

change_Q4_t <- demovate_all %>% select(time, treated, Q4) %>% 
  filter(treated == 1)

Q4_lm_t <- lm(Q4 ~ time, data = change_Q4_t) 
Q4_lm_t <- tidy(Q4_lm_t, conf.int = TRUE) %>% 
  mutate(Question = "Q4", 
         Gruppe = "Deltakergruppe")

# Q5

change_Q5_c <- demovate_all %>% select(time, treated, Q5) %>% 
  filter(treated == 0) 

Q5_lm_c <- lm(Q5 ~ time, data = change_Q5_c) 
Q5_lm_c <- tidy(Q5_lm_c, conf.int = TRUE) %>% 
  mutate(Question = "Q5", 
         Gruppe = "Kontrollgruppe")

change_Q5_t <- demovate_all %>% select(time, treated, Q5) %>% 
  filter(treated == 1)

Q5_lm_t <- lm(Q5 ~ time, data = change_Q5_t) 
Q5_lm_t <- tidy(Q5_lm_t, conf.int = TRUE) %>% 
  mutate(Question = "Q5", 
         Gruppe = "Deltakergruppe")

# Q6

change_Q6_c <- demovate_all %>% select(time, treated, Q6) %>% 
  filter(treated == 0) 

Q6_lm_c <- lm(Q6 ~ time, data = change_Q6_c) 
Q6_lm_c <- tidy(Q6_lm_c, conf.int = TRUE) %>% 
  mutate(Question = "Q6", 
         Gruppe = "Kontrollgruppe")

change_Q6_t <- demovate_all %>% select(time, treated, Q6) %>% 
  filter(treated == 1)

Q6_lm_t <- lm(Q6 ~ time, data = change_Q6_t) 
Q6_lm_t <- tidy(Q6_lm_t, conf.int = TRUE) %>% 
  mutate(Question = "Q6", 
         Gruppe = "Deltakergruppe")

# Q7

change_Q7_c <- demovate_all %>% select(time, treated, Q7) %>% 
  filter(treated == 0) 

Q7_lm_c <- lm(Q7 ~ time, data = change_Q7_c) 
Q7_lm_c <- tidy(Q7_lm_c, conf.int = TRUE) %>% 
  mutate(Question = "Q7", 
         Gruppe = "Kontrollgruppe")

change_Q7_t <- demovate_all %>% select(time, treated, Q7) %>% 
  filter(treated == 1)

Q7_lm_t <- lm(Q7 ~ time, data = change_Q7_t) 
Q7_lm_t <- tidy(Q7_lm_t, conf.int = TRUE) %>% 
  mutate(Question = "Q7", 
         Gruppe = "Deltakergruppe")

# Q8

change_Q8_c <- demovate_all %>% select(time, treated, Q8) %>% 
  filter(treated == 0) 

Q8_lm_c <- lm(Q8 ~ time, data = change_Q8_c) 
Q8_lm_c <- tidy(Q8_lm_c, conf.int = TRUE) %>% 
  mutate(Question = "Q8", 
         Gruppe = "Kontrollgruppe")

change_Q8_t <- demovate_all %>% select(time, treated, Q8) %>% 
  filter(treated == 1)

Q8_lm_t <- lm(Q8 ~ time, data = change_Q8_t) 
Q8_lm_t <- tidy(Q8_lm_t, conf.int = TRUE) %>% 
  mutate(Question = "Q8", 
         Gruppe = "Deltakergruppe")

# Q9

change_Q9_c <- demovate_all %>% select(time, treated, Q9) %>% 
  filter(treated == 0) 

Q9_lm_c <- lm(Q9 ~ time, data = change_Q9_c) 
Q9_lm_c <- tidy(Q9_lm_c, conf.int = TRUE) %>% 
  mutate(Question = "Q9", 
         Gruppe = "Kontrollgruppe")

change_Q9_t <- demovate_all %>% select(time, treated, Q9) %>% 
  filter(treated == 1)

Q9_lm_t <- lm(Q9 ~ time, data = change_Q9_t) 
Q9_lm_t <- tidy(Q9_lm_t, conf.int = TRUE) %>% 
  mutate(Question = "Q9", 
         Gruppe = "Deltakergruppe")
  

endring_kontroll_treatment <- rbind(Q3_lm_c, Q3_lm_t,
                                    Q4_lm_c, Q4_lm_t,
                                    Q5_lm_c, Q5_lm_t,
                                    Q6_lm_c, Q6_lm_t,
                                    Q7_lm_c, Q7_lm_t,
                                    Q8_lm_c, Q8_lm_t,
                                    Q9_lm_c, Q9_lm_t)

endring_kontroll_treatment <- endring_kontroll_treatment %>% 
  mutate(Sak = case_when(Question == 'Q3' ~ 'Cruisetrafikk ut av sentrum',
                                                  Question == 'Q4' ~ 'Utenlandsferger ut av sentrum',
                                                  Question == 'Q5' ~ 'Utfylling i sjø',
                                                  Question == 'Q6' ~ 'Hva sjølinjen skal brukes til',
                                                  Question == 'Q7' ~ 'Boligpolitikk',
                                                  Question == 'Q8' ~ 'Parker vs. idrettsanlegg',
                                                  Question == 'Q9' ~ 'Nye gatenavn')) %>% 
  filter(term != "(Intercept)")
plot_endring_dokken <- ggplot(endring_kontroll_treatment,
mapping = aes(
x = Sak,
y = estimate,
ymin = conf.low,
ymax = conf.high,
colour = Gruppe
)) +
geom_hline(aes(yintercept=0), linetype = 'dotted') +
geom_pointrange(position = position_dodge(width = 1/2), size = 0.5, width = 0.1) +
  scale_color_grey()+
coord_flip() +
labs(title = 'Holdningsendringer før og \netter arrangement', subtitle = 'Dokken', y = 'Endring i gjennomsnitt', x = ' ', colour = ' ' ) +
theme_minimal() +
theme(
axis.text = element_text(colour = "black",
size = 12),
title = element_text(size = 14),
axis.title.x = element_text(size = 12, color = 'black'),
axis.line.x = element_line()



)

plot_endring_dokken

# hvor stor absolutt endring i treatment og kontroll?  

demovate_wide <- demovate_all %>%  pivot_wider(id_cols = c(id, treated), names_from = time, values_from = c(Q3:Q21))

demovate_wide_srv <- srvyr::as_survey_design(demovate_wide, ids = id)

Q3 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q3_1 - Q3_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q3")

Q4<- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q4_1 - Q4_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q4")

Q5<- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q5_1 - Q5_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q5")

Q6 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q6_1 - Q6_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q6")

Q7 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q7_1 - Q7_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q7")

Q8 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q8_1 - Q8_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q8")

Q9 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q9_1 - Q9_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q9")

dokken_absolutt_individuell_endring <- bind_rows(list(Q3, Q4, Q5, Q6, Q7, Q8, Q9)) %>% 
  mutate(Gruppe = case_when(treated == 0 ~ "Kontrollgruppe",
                            treated == 1 ~ "Deltakergruppe"),
         Sak = case_when(question == 'Q3' ~ 'Cruisetrafikk ut av sentrum',
                                                  question == 'Q4' ~ 'Utenlandsferger ut av sentrum',
                                                  question == 'Q5' ~ 'Utfylling i sjø',
                                                  question == 'Q6' ~ 'Hva sjølinjen skal brukes til',
                                                  question == 'Q7' ~ 'Boligpolitikk',
                                                  question == 'Q8' ~ 'Parker vs. idrettsanlegg',
                                                  question == 'Q9' ~ 'Nye gatenavn'))


plot_absolutt_individuell_endring_dokken <- ggplot(dokken_absolutt_individuell_endring,
       mapping = aes(
         x = Sak,
         y = diff,
         ymin = diff_low,
         ymax = diff_upp,
         colour = Gruppe
       )) +
  geom_hline(aes(yintercept=0), linetype = 'dotted') +
  geom_pointrange(position = position_dodge(width = 1/2), size = 0.5, width = 0.1) + 
  scale_color_grey()+
  coord_flip() + 
  labs(title = 'Absolutt holdningsendring\nfør og etter arrangement', subtitle = 'Dokken', y = 'Gjennomsnittlig absolutt holdningsendring', x = ' ', colour = ' ' ) +
  theme_minimal() +
  theme(
    axis.text = element_text(colour = "black",
                             size = 12),
    title = element_text(size = 14),
    axis.title.x = element_text(size = 12, color = 'black'),
    axis.line.x = element_line()

  )

plot_absolutt_individuell_endring_dokken

knitr::include_graphics("ordsky-bilfri.png")
post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q11 %in% 1:5 ~ "Flertall",
                                          Q11 == 6 | Q11 == 12 ~ "Ikke medregnet",
                                          Q11 %in% 7:11 ~ "Mindretall")) 

plot <- ggplot(post_t_delib, aes(x = factor(Q11), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Bergen sentrum mer bilfritt enn i dag?", x = 'Svarfordeling fra 0 = Mer bilfritt, til 10 = Ikke mer bilfritt', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)
  
plot

post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q12 %in% 1:5 ~ "Flertall",
                                          Q12 == 6 | Q12 == 12 ~ "Ikke medregnet",
                                          Q12 %in% 7:11 ~ "Mindretall")) 

plot <- ggplot(post_t_delib, aes(x = factor(Q12), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Sentrumsnære strøk vs. ytre bydeler", x = 'Svarfordeling fra 0 = Prioritere sentrum, til 10 = Prioritere ytre bydeler', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)
  
plot

post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q13 %in% 1:5 ~ "Flertall",
                                          Q13 == 6 | Q13 == 12 ~ "Ikke medregnet",
                                          Q13 %in% 7:11 ~ "Mindretall")) 

plot <- ggplot(post_t_delib, aes(x = factor(Q13), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Mange små vs. store få bilfrie områder", x = 'Svarfordeling fra 0 = Mange små, til 10 = Store få', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)
  
plot

post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q14 %in% 1:5 ~ "Flertall",
                                          Q14 == 6 | Q14 == 12 ~ "Ikke medregnet",
                                          Q14 %in% 7:11 ~ "Mindretall")) 

plot <- ggplot(post_t_delib, aes(x = factor(Q14), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Stenge parkeringsplasser i helgene", x = 'Svarfordeling fra 0 = Motsetter seg, til 10 = Støtter', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)
  
plot

post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q15 %in% 1:5 ~ "Mindretall",
                                          Q15 == 6 | Q15 == 12 ~ "Ikke medregnet",
                                          Q15 %in% 7:11 ~ "Flertall")) 

plot <- ggplot(post_t_delib, aes(x = factor(Q15), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Andel parkeringsplasser til private biler vs. bildeling", x = 'Svarfordeling fra 0 = Flere plasser til privatbiler, til 10 = Flere plasser til bildeling', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)
  
plot

plot <- ggplot(post_t_delib, aes(x = factor(Q16))) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(labels = c('Skilt med gjennomkjøring forbudt', 'Fysiske hindringer \nsom tvinger ned farten', 'Fysiske sperrer \nsom kan fjernes for nødvendig trafikk', 'Lavest mulig fartsgrense (30 km/t)'), drop = FALSE, guide = guide_axis(n.dodge=2)) +
   labs(title = "Reservere parkering til bildeling", x = 'Svarfordeling fra 0 = Flere plasser til privatbiler, til 10 = Flere plasser til bildeling', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)
  
plot

p1 <- ggplot(post_t_delib, aes(x = factor(Q17_1))) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10')) +
   labs(title = 'Biler', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)


p2 <- ggplot(post_t_delib, aes(x = factor(Q17_2))) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
   scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10')) +
   labs(title = 'Sykler', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)

p3 <- ggplot(post_t_delib, aes(x = factor(Q17_3))) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
   scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10')) +
   labs(title = 'Elsykler', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)

p4 <- ggplot(post_t_delib, aes(x = factor(Q17_4))) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
   scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10')) +
   labs(title = 'Elsparkesykler', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)

p5 <- ggplot(post_t_delib, aes(x = factor(Q17_5))) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10')) +
   labs(title = 'Sykkelparkering', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)

p6 <- ggplot(post_t_delib, aes(x = factor(Q17_6))) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
   scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10')) +
   labs(title = 'Sykkelverksted', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)
  
patchwork <- p1 + p2 + p3 +  p4 + p5 + p6
patchwork + plot_annotation(title = 'Villighet til å dele transportmidler', caption = 'Svarfordeling fra 0 = Ikke villig, til 10 = Villig')
post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q18 %in% 1:5 ~ "Flertall",
                                          Q18 == 6 | Q18 == 12 ~ "Ikke medregnet",
                                          Q18 %in% 7:11 ~ "Mindretall")) 

plot <- ggplot(post_t_delib, aes(x = factor(Q18), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Avstand til parkering", x = 'Svarfordeling fra 0 = Gåavstand til fasiliterer, til 10 = Parkering like ved bolig', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)
  
plot

post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q19 %in% 1:5 ~ "Flertall",
                                          Q19 == 6 | Q19 == 12 ~ "Ikke medregnet",
                                          Q19 %in% 7:11 ~ "Mindretall")) 

plot <- ggplot(post_t_delib, aes(x = factor(Q19), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Bilfritt nabolag", x = 'Svarfordeling fra 0 = Motsetter seg, til 10 = Støtter', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)
  
plot

post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q20 %in% 1:5 ~ "Mindretall",
                                          Q20 == 6 | Q20 == 12 ~ "Ikke medregnet",
                                          Q20 %in% 7:11 ~ "Flertall")) 

plot <- ggplot(post_t_delib, aes(x = factor(Q20), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Hvem skal bestemme om nabolag skal være bilfritt", x = 'Svarfordeling fra 0 = Nabolaget skal bestemme, til 10 = Kommunen skal bestemme', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)
  
plot

post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q21 %in% 1:5 ~ "Mindretall",
                                          Q21 == 6 | Q21 == 12 ~ "Ikke medregnet",
                                          Q21 %in% 7:11 ~ "Flertall")) 

plot <- ggplot(post_t_delib, aes(x = factor(Q21), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Redusere fart", x = 'Svarfordeling fra 0 = Motsetter seg, til 10 = Støtter', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)
  
plot

#Gjør om "Vet ikke" til missing
demovate_all <- demovate_all %>% mutate(
Q11 = na_if(Q11, 12),
Q12 = na_if(Q12, 12),
Q13 = na_if(Q13, 12),
Q14 = na_if(Q14,12),
Q15 = na_if(Q15,12),
Q17_1 = na_if(Q17_1,12),
Q17_2 = na_if(Q17_2,12),
Q17_3 = na_if(Q17_3,12),
Q17_4 = na_if(Q17_4,12),
Q17_5 = na_if(Q17_5,12),
Q17_6 = na_if(Q17_6,12),
Q18 = na_if(Q18,12),
Q19 = na_if(Q19,12),
Q20 = na_if(Q20,12),
Q21 = na_if(Q21,12)
)
demovate_all_srv <- srvyr::as_survey_design(demovate_all, ids = id)

demovate_all_srv <- demovate_all_srv %>% mutate(
  Q11 = case_when(Q11 == 1 ~ 0,
                  Q11 == 2 ~ 1,
                  Q11 == 3 ~ 2,
                  Q11 == 4 ~ 3,
                  Q11 == 5 ~ 4,
                  Q11 == 6 ~ 5,
                  Q11 == 7 ~ 6,
                  Q11 == 8 ~ 7,
                  Q11 == 9 ~ 8,
                  Q11 == 10 ~ 9,
                  Q11 == 11 ~ 10),
   Q12 = case_when(Q12 == 1 ~ 0,
                  Q12 == 2 ~ 1,
                  Q12 == 3 ~ 2,
                  Q12 == 4 ~ 3,
                  Q12 == 5 ~ 4,
                  Q12 == 6 ~ 5,
                  Q12 == 7 ~ 6,
                  Q12 == 8 ~ 7,
                  Q12 == 9 ~ 8,
                  Q12 == 10 ~ 9,
                  Q12 == 11 ~ 10),
   Q13 = case_when(Q13 == 1 ~ 0,
                  Q13 == 2 ~ 1,
                  Q13 == 3 ~ 2,
                  Q13 == 4 ~ 3,
                  Q13 == 5 ~ 4,
                  Q13 == 6 ~ 5,
                  Q13 == 7 ~ 6,
                  Q13 == 8 ~ 7,
                  Q13 == 9 ~ 8,
                  Q13 == 10 ~ 9,
                  Q13 == 11 ~ 10),
   Q14 = case_when(Q14 == 1 ~ 0,
                  Q14 == 2 ~ 1,
                  Q14 == 3 ~ 2,
                  Q14 == 4 ~ 3,
                  Q14 == 5 ~ 4,
                  Q14 == 6 ~ 5,
                  Q14 == 7 ~ 6,
                  Q14 == 8 ~ 7,
                  Q14 == 9 ~ 8,
                  Q14 == 10 ~ 9,
                  Q14 == 11 ~ 10),
   Q15 = case_when(Q15 == 1 ~ 0,
                  Q15 == 2 ~ 1,
                  Q15 == 3 ~ 2,
                  Q15 == 4 ~ 3,
                  Q15 == 5 ~ 4,
                  Q15 == 6 ~ 5,
                  Q15 == 7 ~ 6,
                  Q15 == 8 ~ 7,
                  Q15 == 9 ~ 8,
                  Q15 == 10 ~ 9,
                  Q15 == 11 ~ 10),
   Q17_1 = case_when(Q17_1 == 1 ~ 0,
                  Q17_1 == 2 ~ 1,
                  Q17_1 == 3 ~ 2,
                  Q17_1 == 4 ~ 3,
                  Q17_1 == 5 ~ 4,
                  Q17_1 == 6 ~ 5,
                  Q17_1 == 7 ~ 6,
                  Q17_1 == 8 ~ 7,
                  Q17_1 == 9 ~ 8,
                  Q17_1 == 10 ~ 9,
                  Q17_1 == 11 ~ 10),
   Q17_2 = case_when(Q17_2 == 1 ~ 0,
                  Q17_2 == 2 ~ 1,
                  Q17_2 == 3 ~ 2,
                  Q17_2 == 4 ~ 3,
                  Q17_2 == 5 ~ 4,
                  Q17_2 == 6 ~ 5,
                  Q17_2 == 7 ~ 6,
                  Q17_2 == 8 ~ 7,
                  Q17_2 == 9 ~ 8,
                  Q17_2 == 10 ~ 9,
                  Q17_2 == 11 ~ 10),
   Q17_3 = case_when(Q17_3 == 1 ~ 0,
                  Q17_3 == 2 ~ 1,
                  Q17_3 == 3 ~ 2,
                  Q17_3 == 4 ~ 3,
                  Q17_3 == 5 ~ 4,
                  Q17_3 == 6 ~ 5,
                  Q17_3 == 7 ~ 6,
                  Q17_3 == 8 ~ 7,
                  Q17_3 == 9 ~ 8,
                  Q17_3 == 10 ~ 9,
                  Q17_3 == 11 ~ 10),
   Q17_4 = case_when(Q17_4 == 1 ~ 0,
                  Q17_4 == 2 ~ 1,
                  Q17_4 == 3 ~ 2,
                  Q17_4 == 4 ~ 3,
                  Q17_4 == 5 ~ 4,
                  Q17_4 == 6 ~ 5,
                  Q17_4 == 7 ~ 6,
                  Q17_4 == 8 ~ 7,
                  Q17_4 == 9 ~ 8,
                  Q17_4 == 10 ~ 9,
                  Q17_4 == 11 ~ 10),
   Q17_5 = case_when(Q17_5 == 1 ~ 0,
                  Q17_5 == 2 ~ 1,
                  Q17_5 == 3 ~ 2,
                  Q17_5 == 4 ~ 3,
                  Q17_5 == 5 ~ 4,
                  Q17_5 == 6 ~ 5,
                  Q17_5 == 7 ~ 6,
                  Q17_5 == 8 ~ 7,
                  Q17_5 == 9 ~ 8,
                  Q17_5 == 10 ~ 9,
                  Q17_5 == 11 ~ 10),
   Q17_6 = case_when(Q17_6 == 1 ~ 0,
                  Q17_6 == 2 ~ 1,
                  Q17_6 == 3 ~ 2,
                  Q17_6 == 4 ~ 3,
                  Q17_6 == 5 ~ 4,
                  Q17_6 == 6 ~ 5,
                  Q17_6 == 7 ~ 6,
                  Q17_6 == 8 ~ 7,
                  Q17_6 == 9 ~ 8,
                  Q17_6 == 10 ~ 9,
                  Q17_6 == 11 ~ 10),
   Q18 = case_when(Q18 == 1 ~ 0,
                  Q18 == 2 ~ 1,
                  Q18 == 3 ~ 2,
                  Q18 == 4 ~ 3,
                  Q18 == 5 ~ 4,
                  Q18 == 6 ~ 5,
                  Q18 == 7 ~ 6,
                  Q18 == 8 ~ 7,
                  Q18 == 9 ~ 8,
                  Q18 == 10 ~ 9,
                  Q18 == 11 ~ 10),
   Q19 = case_when(Q19 == 1 ~ 0,
                  Q19 == 2 ~ 1,
                  Q19 == 3 ~ 2,
                  Q19 == 4 ~ 3,
                  Q19 == 5 ~ 4,
                  Q19 == 6 ~ 5,
                  Q19 == 7 ~ 6,
                  Q19 == 8 ~ 7,
                  Q19 == 9 ~ 8,
                  Q19 == 10 ~ 9,
                  Q19 == 11 ~ 10),
   Q20 = case_when(Q20 == 1 ~ 0,
                  Q20 == 2 ~ 1,
                  Q20 == 3 ~ 2,
                  Q20 == 4 ~ 3,
                  Q20 == 5 ~ 4,
                  Q20 == 6 ~ 5,
                  Q20 == 7 ~ 6,
                  Q20 == 8 ~ 7,
                  Q20 == 9 ~ 8,
                  Q20 == 10 ~ 9,
                  Q20 == 11 ~ 10),
   Q21 = case_when(Q21 == 1 ~ 0,
                  Q21 == 2 ~ 1,
                  Q21 == 3 ~ 2,
                  Q21 == 4 ~ 3,
                  Q21 == 5 ~ 4,
                  Q21 == 6 ~ 5,
                  Q21 == 7 ~ 6,
                  Q21 == 8 ~ 7,
                  Q21 == 9 ~ 8,
                  Q21 == 10 ~ 9,
                  Q21 == 11 ~ 10)
)

Q11 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q11, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q11") %>%
ungroup()

Q12 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q12, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q12") %>%
ungroup()

Q13 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q13, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q13") %>%
ungroup()

Q14 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q14, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q14") %>%
ungroup()

Q15 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q15, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q15") %>%
ungroup()

Q17_1 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q17_1, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q17_1") %>%
ungroup()

Q17_2 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q17_2, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q17_2") %>%
ungroup()

Q17_3 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q17_3, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q17_3") %>%
ungroup()

Q17_4 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q17_4, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q17_4") %>%
ungroup()

Q17_5 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q17_5, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q17_5") %>%
ungroup()

Q17_6 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q17_6, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q17_6") %>%
ungroup()

Q18 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q18, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q18") %>%
ungroup()

Q19 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q19, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q19") %>%
ungroup()

Q20 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q20, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q20") %>%
ungroup()

Q21 <- demovate_all_srv %>%
group_by(time, treated) %>%
summarise(mean = survey_mean(Q21, na.rm = T, vartype = c("ci", "se"))) %>%
mutate(question = "Q21") %>%
ungroup()

bilfri <- bind_rows(list(Q11, Q12, Q13, Q14, Q15, Q17_1, Q17_2, Q17_3, Q17_4, Q17_5, Q17_6, Q18, Q19, Q20, Q21)) %>%
mutate(Undersøkelse = case_when(
  time == 0 ~ "Før",
time == 1 ~ "Etter"),
Gruppe = case_when(
  treated == 0 ~ "Kontrollgruppe",
treated == 1 ~ "Deltakergruppe"),
Sak = case_when(
  question == 'Q11' ~ 'Bergen sentrum mer bilfritt',
                                                  question == 'Q12' ~ 'Sentrum vs. ytre bydeler',
                                                  question == 'Q13' ~ 'Mange små vs. store, få bilfrie områder',
                                                  question == 'Q14' ~ 'Stenge parkeringsplasser i helgene',
                                                  question == 'Q15' ~ 'Andel parkeringsplasser til private biler vs. bildeling',
                                                   question == 'Q17_1' ~ 'Villighet til å dele biler',
                                                   question == 'Q17_2' ~ 'Villighet til å dele sykler',
                                                   question == 'Q17_3' ~ 'Villighet til å dele elsykler',
                                                   question == 'Q17_4' ~ 'Villighet til å dele elsparkesykler',
                                                   question == 'Q17_5' ~ 'Villighet til å dele sykkelparkering',
                                                   question == 'Q17_6' ~ 'Villighet til å dele sykkelverksted',
                                                  question == 'Q18' ~ 'Gåavstand til fasiliteter vs. parkering like ved bolig',
                                                  question == 'Q19' ~ 'Bo i bilfritt nabolag',
                                                  question == 'Q20' ~ 'Hvem skal bestemme om nabolag skal være bilfritt',
                                                  question == 'Q21' ~ 'Redusere fart i tettbygde strøk'))
                 
                 
# Gjør om Undersøkelse til faktor og endre rekkefølgen
bilfri <- bilfri %>%
mutate(Undersøkelse = factor(Undersøkelse,
levels = c("Før",
"Etter")))
bilfri %>% arrange(Sak, Gruppe, Undersøkelse) %>%
  filter(Gruppe == 'Deltakergruppe') %>% 
select(Sak, Undersøkelse, Gj.snitt = mean) %>%
kableExtra::kable(., booktabs = T, digits = 1, caption = 'Gjennomsnitt for deltakergruppen') %>% kable_styling(c('striped', 'hover'), font_size = 10) %>%
collapse_rows(columns = 1)
bilfri %>% arrange(Sak, Gruppe, Undersøkelse) %>%
  filter(Gruppe == 'Kontrollgruppe') %>% 
select(Sak, Undersøkelse, Gj.snitt = mean) %>%
kableExtra::kable(., booktabs = T, digits = 1, caption = 'Gjennomsnitt for kontrollgruppen') %>% kable_styling(c('striped', 'hover'), font_size = 10) %>%
collapse_rows(columns = 1)

#Q11
change_Q11_c <- demovate_all %>% select(time, treated, Q11) %>%
filter(treated == 0)

Q11_lm_c <- lm(Q11 ~ time, data = change_Q11_c)
Q11_lm_c <- tidy(Q11_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q11",
Gruppe = "Kontrollgruppe")

change_Q11_t <- demovate_all %>% select(time, treated, Q11) %>%
filter(treated == 1)

Q11_lm_t <- lm(Q11 ~ time, data = change_Q11_t)
Q11_lm_t <- tidy(Q11_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q11",
Gruppe = "Deltakergruppe")

#Q12
change_Q12_c <- demovate_all %>% select(time, treated, Q12) %>%
filter(treated == 0)

Q12_lm_c <- lm(Q12 ~ time, data = change_Q12_c)
Q12_lm_c <- tidy(Q12_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q12",
Gruppe = "Kontrollgruppe")

change_Q12_t <- demovate_all %>% select(time, treated, Q12) %>%
filter(treated == 1)

Q12_lm_t <- lm(Q12 ~ time, data = change_Q12_t)
Q12_lm_t <- tidy(Q12_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q12",
Gruppe = "Deltakergruppe")

#Q13
change_Q13_c <- demovate_all %>% select(time, treated, Q13) %>%
filter(treated == 0)

Q13_lm_c <- lm(Q13 ~ time, data = change_Q13_c)
Q13_lm_c <- tidy(Q13_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q13",
Gruppe = "Kontrollgruppe")

change_Q13_t <- demovate_all %>% select(time, treated, Q13) %>%
filter(treated == 1)

Q13_lm_t <- lm(Q13 ~ time, data = change_Q13_t)
Q13_lm_t <- tidy(Q13_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q13",
Gruppe = "Deltakergruppe")

#Q14
change_Q14_c <- demovate_all %>% select(time, treated, Q14) %>%
filter(treated == 0)

Q14_lm_c <- lm(Q14 ~ time, data = change_Q14_c)
Q14_lm_c <- tidy(Q14_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q14",
Gruppe = "Kontrollgruppe")

change_Q14_t <- demovate_all %>% select(time, treated, Q14) %>%
filter(treated == 1)

Q14_lm_t <- lm(Q14 ~ time, data = change_Q14_t)
Q14_lm_t <- tidy(Q14_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q14",
Gruppe = "Deltakergruppe")

#Q15
change_Q15_c <- demovate_all %>% select(time, treated, Q15) %>%
filter(treated == 0)

Q15_lm_c <- lm(Q15 ~ time, data = change_Q15_c)
Q15_lm_c <- tidy(Q15_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q15",
Gruppe = "Kontrollgruppe")

change_Q15_t <- demovate_all %>% select(time, treated, Q15) %>%
filter(treated == 1)

Q15_lm_t <- lm(Q15 ~ time, data = change_Q15_t)
Q15_lm_t <- tidy(Q15_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q15",
Gruppe = "Deltakergruppe")

#Q17_1
change_Q17_1_c <- demovate_all %>% select(time, treated, Q17_1) %>%
filter(treated == 0)

Q17_1_lm_c <- lm(Q17_1 ~ time, data = change_Q17_1_c)
Q17_1_lm_c <- tidy(Q17_1_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q17_1",
Gruppe = "Kontrollgruppe")

change_Q17_1_t <- demovate_all %>% select(time, treated, Q17_1) %>%
filter(treated == 1)

Q17_1_lm_t <- lm(Q17_1 ~ time, data = change_Q17_1_t)
Q17_1_lm_t <- tidy(Q17_1_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q17_1",
Gruppe = "Deltakergruppe")

#Q17_2
change_Q17_2_c <- demovate_all %>% select(time, treated, Q17_2) %>%
filter(treated == 0)

Q17_2_lm_c <- lm(Q17_2 ~ time, data = change_Q17_2_c)
Q17_2_lm_c <- tidy(Q17_2_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q17_2",
Gruppe = "Kontrollgruppe")

change_Q17_2_t <- demovate_all %>% select(time, treated, Q17_2) %>%
filter(treated == 1)

Q17_2_lm_t <- lm(Q17_2 ~ time, data = change_Q17_2_t)
Q17_2_lm_t <- tidy(Q17_2_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q17_2",
Gruppe = "Deltakergruppe")

#Q17_3
change_Q17_3_c <- demovate_all %>% select(time, treated, Q17_3) %>%
filter(treated == 0)

Q17_3_lm_c <- lm(Q17_3 ~ time, data = change_Q17_3_c)
Q17_3_lm_c <- tidy(Q17_3_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q17_3",
Gruppe = "Kontrollgruppe")

change_Q17_3_t <- demovate_all %>% select(time, treated, Q17_3) %>%
filter(treated == 1)

Q17_3_lm_t <- lm(Q17_3 ~ time, data = change_Q17_3_t)
Q17_3_lm_t <- tidy(Q17_3_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q17_3",
Gruppe = "Deltakergruppe")

#Q17_4
change_Q17_4_c <- demovate_all %>% select(time, treated, Q17_4) %>%
filter(treated == 0)

Q17_4_lm_c <- lm(Q17_4 ~ time, data = change_Q17_4_c)
Q17_4_lm_c <- tidy(Q17_4_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q17_4",
Gruppe = "Kontrollgruppe")

change_Q17_4_t <- demovate_all %>% select(time, treated, Q17_4) %>%
filter(treated == 1)

Q17_4_lm_t <- lm(Q17_4 ~ time, data = change_Q17_4_t)
Q17_4_lm_t <- tidy(Q17_4_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q17_4",
Gruppe = "Deltakergruppe")

#Q17_5
change_Q17_5_c <- demovate_all %>% select(time, treated, Q17_5) %>%
filter(treated == 0)

Q17_5_lm_c <- lm(Q17_5 ~ time, data = change_Q17_5_c)
Q17_5_lm_c <- tidy(Q17_5_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q17_5",
Gruppe = "Kontrollgruppe")

change_Q17_5_t <- demovate_all %>% select(time, treated, Q17_5) %>%
filter(treated == 1)

Q17_5_lm_t <- lm(Q17_5 ~ time, data = change_Q17_5_t)
Q17_5_lm_t <- tidy(Q17_5_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q17_5",
Gruppe = "Deltakergruppe")

#Q17_6
change_Q17_6_c <- demovate_all %>% select(time, treated, Q17_6) %>%
filter(treated == 0)

Q17_6_lm_c <- lm(Q17_6 ~ time, data = change_Q17_6_c)
Q17_6_lm_c <- tidy(Q17_6_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q17_6",
Gruppe = "Kontrollgruppe")

change_Q17_6_t <- demovate_all %>% select(time, treated, Q17_6) %>%
filter(treated == 1)

Q17_6_lm_t <- lm(Q17_6 ~ time, data = change_Q17_6_t)
Q17_6_lm_t <- tidy(Q17_6_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q17_6",
Gruppe = "Deltakergruppe")

#Q18
change_Q18_c <- demovate_all %>% select(time, treated, Q18) %>%
filter(treated == 0)

Q18_lm_c <- lm(Q18 ~ time, data = change_Q18_c)
Q18_lm_c <- tidy(Q18_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q18",
Gruppe = "Kontrollgruppe")

change_Q18_t <- demovate_all %>% select(time, treated, Q18) %>%
filter(treated == 1)

Q18_lm_t <- lm(Q18 ~ time, data = change_Q18_t)
Q18_lm_t <- tidy(Q18_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q18",
Gruppe = "Deltakergruppe")

#Q19
change_Q19_c <- demovate_all %>% select(time, treated, Q19) %>%
filter(treated == 0)

Q19_lm_c <- lm(Q19 ~ time, data = change_Q19_c)
Q19_lm_c <- tidy(Q19_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q19",
Gruppe = "Kontrollgruppe")

change_Q19_t <- demovate_all %>% select(time, treated, Q19) %>%
filter(treated == 1)

Q19_lm_t <- lm(Q19 ~ time, data = change_Q19_t)
Q19_lm_t <- tidy(Q19_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q19",
Gruppe = "Deltakergruppe")

#Q20
change_Q20_c <- demovate_all %>% select(time, treated, Q20) %>%
filter(treated == 0)

Q20_lm_c <- lm(Q20 ~ time, data = change_Q20_c)
Q20_lm_c <- tidy(Q20_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q20",
Gruppe = "Kontrollgruppe")

change_Q20_t <- demovate_all %>% select(time, treated, Q20) %>%
filter(treated == 1)

Q20_lm_t <- lm(Q20 ~ time, data = change_Q20_t)
Q20_lm_t <- tidy(Q20_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q20",
Gruppe = "Deltakergruppe")

#Q21
change_Q21_c <- demovate_all %>% select(time, treated, Q21) %>%
filter(treated == 0)

Q21_lm_c <- lm(Q21 ~ time, data = change_Q21_c)
Q21_lm_c <- tidy(Q21_lm_c, conf.int = TRUE) %>%
mutate(Question = "Q21",
Gruppe = "Kontrollgruppe")

change_Q21_t <- demovate_all %>% select(time, treated, Q21) %>%
filter(treated == 1)

Q21_lm_t <- lm(Q21 ~ time, data = change_Q21_t)
Q21_lm_t <- tidy(Q21_lm_t, conf.int = TRUE) %>%
mutate(Question = "Q21",
Gruppe = "Deltakergruppe")

endring_kontroll_treatment <- rbind(
Q11_lm_c, Q11_lm_t,
Q12_lm_c, Q12_lm_t,
Q13_lm_c, Q13_lm_t,
Q14_lm_c, Q14_lm_t,
Q15_lm_c, Q15_lm_t,
Q17_1_lm_c, Q17_1_lm_t,
Q17_2_lm_c, Q17_2_lm_t,
Q17_3_lm_c, Q17_3_lm_t,
Q17_4_lm_c, Q17_4_lm_t,
Q17_5_lm_c, Q17_5_lm_t,
Q17_6_lm_c, Q17_6_lm_t,
Q18_lm_c, Q18_lm_t,
Q19_lm_c, Q19_lm_t,
Q20_lm_c, Q20_lm_t,
Q21_lm_c, Q21_lm_t
)

endring_kontroll_treatment <- endring_kontroll_treatment %>%
mutate(Sak = case_when(Question == 'Q11' ~ 'Bilfritt sentrum',
                                                  Question == 'Q12' ~ 'Sentrum vs. ytre bydeler',
                                                  Question == 'Q13' ~ 'Små vs. store bilfrie områder',
                                                  Question == 'Q14' ~ 'Stenge parkeringsplasser',
                                                  Question == 'Q15' ~ 'Private biler vs. bildeling',
                                                   Question == 'Q17_1' ~ 'Dele biler',
                                                   Question == 'Q17_2' ~ 'Dele sykler',
                                                   Question == 'Q17_3' ~ 'Dele elsykler',
                                                   Question == 'Q17_4' ~ 'Dele elsparkesykler',
                                                   Question == 'Q17_5' ~ 'Dele sykkelparkering',
                                                   Question == 'Q17_6' ~ 'Dele sykkelverksted',
                                                  Question == 'Q18' ~ 'Gåavstand vs. parkering',
                                                  Question == 'Q19' ~ 'Bo i bilfritt nabolag',
                                                  Question == 'Q20' ~ 'Hvem skal bestemme',
                                                  Question == 'Q21' ~ 'Redusere fart')) %>%
         filter(term != "(Intercept)")
plot_endring_bilfri <- ggplot(endring_kontroll_treatment,
mapping = aes(
x = Sak,
y = estimate,
ymin = conf.low,
ymax = conf.high,
colour = Gruppe
)) +
geom_hline(aes(yintercept=0), linetype = 'dotted') +
geom_pointrange(position = position_dodge(width = 1/2), size = 0.5, width = 0.1) +
  scale_color_grey()+
coord_flip() +
labs(title = 'Holdningsendringer før og \netter arrangement', subtitle = 'Bilfrie områder', y = 'Endring i gjennomsnitt', x = ' ', colour = ' ' ) +
theme_minimal() +
theme(
axis.text = element_text(colour = "black",
size = 12),
title = element_text(size = 14),
axis.title.x = element_text(size = 12, color = 'black'),
axis.line.x = element_line()
)

plot_endring_bilfri

#billfri absolutt  endring

Q11 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q11_1 - Q11_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q11")

Q12<- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q12_1 - Q12_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q12")

Q13<- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q13_1 - Q13_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q13")

Q14 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q14_1 - Q14_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q14")

Q15 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q15_1 - Q15_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q15")

Q17_1 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q17_1_1 - Q17_1_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q17_1")

Q17_2 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q17_2_1 - Q17_2_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q17_2")

Q17_3 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q17_3_1 - Q17_3_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q17_3")

Q17_4 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q17_4_1 - Q17_4_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q17_4")

Q17_5 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q17_5_1 - Q17_5_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q17_5")

Q17_6 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q17_6_1 - Q17_6_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q17_6")

Q18 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q18_1 - Q18_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q18")

Q19 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q19_1 - Q19_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q19")

Q20 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q20_1 - Q20_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q20")

Q21 <- demovate_wide_srv %>% group_by(treated) %>%
  summarise(diff = survey_mean(
    sqrt((Q21_1 - Q21_0)^2), vartype = c("ci"), na.rm = T)) %>% 
  mutate(question = "Q21")



bilfri_absolutt_individuell_endring <- bind_rows(list(Q11, Q12, Q13, Q14, Q15, Q17_1, Q17_2, Q17_3, Q17_4, Q17_5, Q17_6, Q18, Q19, Q20, Q21)) %>% 
  mutate(Gruppe = case_when(treated == 0 ~ "Kontrollgruppe",
                            treated == 1 ~ "Deltakergruppe"),
         Sak = (case_when(question == 'Q11' ~ 'Bilfritt sentrum',
                                                  question == 'Q12' ~ 'Sentrum vs. ytre bydeler',
                                                  question == 'Q13' ~ 'Små vs. store bilfrie områder',
                                                  question == 'Q14' ~ 'Stenge parkeringsplasser',
                                                  question == 'Q15' ~ 'Private biler vs. bildeling',
                                                   question == 'Q17_1' ~ 'Dele biler',
                                                   question == 'Q17_2' ~ 'Dele sykler',
                                                   question == 'Q17_3' ~ 'Dele elsykler',
                                                   question == 'Q17_4' ~ 'Dele elsparkesykler',
                                                   question == 'Q17_5' ~ 'Dele sykkelparkering',
                                                   question == 'Q17_6' ~ 'Dele sykkelverksted',
                                                  question == 'Q18' ~ 'Gåavstand parkering',
                                                  question == 'Q19' ~ 'Bo i bilfritt nabolag',
                                                  question == 'Q20' ~ 'Hvem skal bestemme',
                                                  question == 'Q21' ~ 'Redusere fart')))


plot_absolutt_individuell_endring_bilfri <- ggplot(bilfri_absolutt_individuell_endring,
       mapping = aes(
         x = Sak,
         y = diff,
         ymin = diff_low,
         ymax = diff_upp,
         colour = Gruppe
       )) +
  geom_hline(aes(yintercept=0), linetype = 'dotted') +
  geom_pointrange(position = position_dodge(width = 1/2), size = 0.5, width = 0.1) + 
  scale_color_grey()+
  coord_flip() + 
  labs(title = 'Absolutt holdningsendring\nfør og etter arrangement', subtitle = 'Bilfri', y = 'Gjennomsnittlig absolutt holdningsendring', x = ' ', colour = ' ' ) +
  theme_minimal() +
  theme(
    axis.text = element_text(colour = "black",
                             size = 12),
    title = element_text(size = 14),
    axis.title.x = element_text(size = 12, color = 'black'),
    axis.line.x = element_line()

  )

plot_absolutt_individuell_endring_bilfri

post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q85_1 %in% 1:5 ~ "Mindretall",
                                          Q85_1 == 6 | Q85_1 == 14 ~ "Ikke medregnet",
                                          Q85_1 %in% 7:11 ~ "Flertall")) 
p1 <- ggplot(post_t_delib, aes(x = factor(Q85_1), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '   Vet\n  ikke')) +  labs(title = 'Gruppediskusjonene', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)

post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q85_2 %in% 1:5 ~ "Mindretall",
                                          Q85_2 == 6 | Q85_2 == 14 ~ "Ikke medregnet",
                                          Q85_2 %in% 7:11 ~ "Flertall")) 
p2 <- ggplot(post_t_delib, aes(x = factor(Q85_2), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '   Vet\n  ikke')) +
  labs(title = 'Informasjonsmaterialet', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)

post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q85_3 %in% 1:5 ~ "Mindretall",
                                          Q85_3 == 6 | Q85_3 == 14 ~ "Ikke medregnet",
                                          Q85_3 %in% 7:11 ~ "Flertall")) 
p3 <- ggplot(post_t_delib, aes(x = factor(Q85_3), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '   Vet\n  ikke')) +
   labs(title = 'Plenumsdiskusjonene', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)

post_t_delib <- post_t_delib %>% mutate(fill = case_when(Q85_4 %in% 1:5 ~ "Mindretall",
                                          Q85_4 == 6 | Q85_4 == 14 ~ "Ikke medregnet",
                                          Q85_4 %in% 7:11 ~ "Flertall")) 
p4 <- ggplot(post_t_delib, aes(x = factor(Q85_4), fill = fill)) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '   Vet\n  ikke')) +
   labs(title = 'Arrangementet som helhet', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic() +
  scale_fill_grey(start = 0, end = 0.9)

patchwork <- p4 + p2 + p3 + p1
patchwork + plot_annotation(title = 'Nytte av deliberativ meningsmåling', caption = 'Svarfordeling fra 0 = Bortkastet tid, til 10 = Ekstremt verdifullt, og hvor 5 = Nøyaktig på midten')
p1 <- ggplot(post_t_delib, aes(x = factor(Q86_1))) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L), breaks = seq(0,1, by = 0.5)) +
 scale_x_discrete(limits = c('1', '2', '3', '4', '5'),
                                    labels = c('Sterkt uenig', 'Litt uenig', 'Verken eller', 'Litt enig', 'Sterkt enig'),
                                    guide = guide_axis(n.dodge = 2), drop = FALSE) +
     labs(title = 'Gruppelederne ga alle mulighet \ntil å delta', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)


p2 <- ggplot(post_t_delib, aes(x = factor(Q86_2))) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L), breaks = seq(0,1, by = 0.3)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5'),
                                    labels = c('Sterkt uenig', 'Litt uenig', 'Verken eller', 'Litt enig', 'Sterkt enig'),
                                    guide = guide_axis(n.dodge = 2), drop = FALSE) +
  
   labs(title = 'Gruppemedlemmene deltok \nlike mye', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)


p3 <- ggplot(post_t_delib, aes(x = factor(Q86_3))) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L), breaks = seq(0,1, by = 0.4)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5'),
                                    labels = c('Sterkt uenig', 'Litt uenig', 'Verken eller', 'Litt enig', 'Sterkt enig'),
                                    guide = guide_axis(n.dodge = 2), drop = FALSE) +
  labs(title = 'Motstridende argumenter \nble vurdert', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic() +
  scale_fill_grey(start = 0, end = 0.9)


p4 <- ggplot(post_t_delib, aes(x = factor(Q86_4))) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L), breaks = seq(0,1, by = 0.4)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5'),
                                    labels = c('Sterkt uenig', 'Litt uenig', 'Verken eller', 'Litt enig', 'Sterkt enig'),
                                    guide = guide_axis(n.dodge = 2), drop = FALSE) +
                     labs(title = 'De viktigste aspektene \nble dekket', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic() +
  scale_fill_grey(start = 0, end = 0.9)


p5 <- ggplot(post_t_delib, aes(x = factor(Q86_5))) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(labels = c('Sterkt uenig', 'Litt uenig', 'Verken eller', 'Litt enig', 'Sterkt enig'), guide = guide_axis(n.dodge = 2), drop = FALSE) +
   labs(title = 'Jeg lærte mye om folk \nsom er annerledes enn meg', x = ' ', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)


patchwork <- (p1 + p2) / (p3 + p4) / p5

patchwork + plot_annotation(title = 'Evaluering av gruppediskusjoner')
#Create function for lower and upper confidence intervals
lower_ci <- function(mean, se, n, conf_level = 0.95){
  lower_ci <- mean - qt(1 - ((1 - conf_level) / 2), n - 1) * se
}
upper_ci <- function(mean, se, n, conf_level = 0.95){
  upper_ci <- mean + qt(1 - ((1 - conf_level) / 2), n - 1) * se
}

# Ekstern 'political efficacy'
efficacy_pre_c <- demovate_all %>% 
  filter(time == 0 & treated == 0) %>% 
  select(Q23) %>%
  ungroup() %>% 
  summarise(smean = mean(Q23, na.rm = TRUE),
            ssd = sd(Q23, na.rm = TRUE),
            count = n()) %>%
  mutate(se = ssd / sqrt(count),
         lower_ci = lower_ci(smean, se, count),
         upper_ci = upper_ci(smean, se, count))

efficacy_pre_t <- demovate_all %>% 
  filter(time == 0 & treated == 1) %>% 
  select(Q23) %>% 
  ungroup() %>% 
  summarise(smean = mean(Q23, na.rm = TRUE),
            ssd = sd(Q23, na.rm = TRUE),
            count = n()) %>%
  mutate(se = ssd / sqrt(count),
         lower_ci = lower_ci(smean, se, count),
         upper_ci = upper_ci(smean, se, count))

efficacy_post_c <- demovate_all %>% 
  filter(time == 1 & treated == 0) %>%
    select(QID86) %>% 
  ungroup() %>% 
   summarise(smean = mean(QID86, na.rm = TRUE),
            ssd = sd(QID86, na.rm = TRUE),
            count = n()) %>%
  mutate(se = ssd / sqrt(count),
         lower_ci = lower_ci(smean, se, count),
         upper_ci = upper_ci(smean, se, count))
  
efficacy_post_t <- demovate_all %>% 
  filter(time == 1 & treated == 1) %>% 
  select(Q88) %>% 
  ungroup() %>% 
  summarise(smean = mean(Q88, na.rm = TRUE),
            ssd = sd(Q88, na.rm = TRUE),
            count = n()) %>%
  mutate(se = ssd / sqrt(count),
         lower_ci = lower_ci(smean, se, count),
         upper_ci = upper_ci(smean, se, count))


efficacy <- bind_rows(list(efficacy_pre_c, efficacy_pre_t, efficacy_post_c, efficacy_post_t), .id = "group") %>% 
 mutate(survey = case_when(group == c(1:2) ~ 'Før deliberasjon',
                           group == c(3:4) ~ 'Etter deliberasjon'),
        group = (case_when(group %in% c(1, 3) ~ 'Kontrollgruppe',
                           group %in% c(2, 4) ~ 'Deltakergruppe')))

p1 <- ggplot(efficacy, aes(x = group, y = smean, colour=survey)) +
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci), size = 1, width = 0.1) +
  geom_point(size = 3) +
  coord_flip() +
  scale_y_continuous() +
  labs(title = 'Mulig for folk som meg å påvirke lokalpolitikken', subtitle = 'Fra 0 = Ikke i det hele tatt til 10 = Passer fullt og helt', y = 'Gjennomsnitt', x = ' ', colour = ' ' ) +
  theme_classic()+
  scale_colour_grey()

#Intern 'political efficacy'
efficacy_pre_c <- demovate_all %>% 
  filter(time == 0 & treated == 0) %>%
  select(Q24) %>% 
  ungroup() %>% 
  summarise(smean = mean(Q24, na.rm = TRUE),
            ssd = sd(Q24, na.rm = TRUE),
            count = n()) %>%
  mutate(se = ssd / sqrt(count),
         lower_ci = lower_ci(smean, se, count),
         upper_ci = upper_ci(smean, se, count))

efficacy_pre_t <- demovate_all %>% 
  filter(time == 0 & treated == 1) %>%
  select(Q24) %>% 
  ungroup() %>% 
  summarise(smean = mean(Q24, na.rm = TRUE),
            ssd = sd(Q24, na.rm = TRUE),
            count = n()) %>%
  mutate(se = ssd / sqrt(count),
         lower_ci = lower_ci(smean, se, count),
         upper_ci = upper_ci(smean, se, count))

efficacy_post_c <- demovate_all %>% 
  filter(time == 1 & treated == 0) %>%
    select(Q90) %>% 
  ungroup() %>% 
   summarise(smean = mean(Q90, na.rm = TRUE),
            ssd = sd(Q90, na.rm = TRUE),
            count = n()) %>%
  mutate(se = ssd / sqrt(count),
         lower_ci = lower_ci(smean, se, count),
         upper_ci = upper_ci(smean, se, count))
  
efficacy_post_t <- demovate_all %>% 
  filter(time == 1 & treated == 1) %>%
  select(Q90) %>% 
  ungroup() %>% 
  summarise(smean = mean(Q90, na.rm = TRUE),
            ssd = sd(Q90, na.rm = TRUE),
            count = n()) %>%
  mutate(se = ssd / sqrt(count),
         lower_ci = lower_ci(smean, se, count),
         upper_ci = upper_ci(smean, se, count))


efficacy <- bind_rows(list(efficacy_pre_c, efficacy_pre_t, efficacy_post_c, efficacy_post_t), .id = "group") %>% 
 mutate(survey = case_when(group == c(1:2) ~ 'Før deliberasjon',
                           group == c(3:4) ~ 'Etter deliberasjon'),
        group = (case_when(group %in% c(1, 3) ~ 'Kontrollgruppe',
                           group %in% c(2, 4) ~ 'Deltakergruppe')))

p2 <- ggplot(efficacy, aes(x = group, y = smean, colour=survey)) +
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci), size = 1, width = 0.1) +
  geom_point(size = 3) +
  coord_flip() +
  scale_y_continuous() +
  labs(title = 'Politikk er innviklet', subtitle = 'Fra 0 = Ikke i det hele tatt til 10 = Passer fullt og helt', y = 'Gjennomsnitt', x = ' ', colour = ' ' ) + 
  theme_classic()+
  scale_colour_grey()

p1 / p2


plot <- ggplot(post_t_delib, aes(x = factor(Q91))) +
  geom_bar(aes(y = (..count..) / sum(..count..))) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  scale_x_discrete(limits = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'),
                   labels = c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Vet ikke')) +
   labs(title = "Vektlegging av resultater", x = 'Svarfordeling fra 0 = Skal ikke vektlegges i det hele tatt til 10 = Må følges uansett', y = 'Prosent', fill = '')  +
  theme_classic()+
  scale_fill_grey(start = 0, end = 0.9)
  
plot