Skip to content

Instantly share code, notes, and snippets.

@vanatteveldt
Last active February 27, 2023 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanatteveldt/22f39b50a3b942f5b968a3074e14bc48 to your computer and use it in GitHub Desktop.
Save vanatteveldt/22f39b50a3b942f5b968a3074e14bc48 to your computer and use it in GitHub Desktop.
library(psych)
library(tidyverse)
library(haven)
d = read_sav("~/Downloads/Project 2 GGD_February 21, 2023_03.46.sav")
cleaned = d |> filter(status != 1) |>
rename_with(~str_replace(., "Q37", "Vertrouwen"), starts_with("Q37")) |>
rename_with(~str_replace(., "Q38", "Privacygevoeligheid"), starts_with("Q38")) |>
rename_with(~str_replace(., "Q40", "Perceptie_medewerkers"), starts_with("Q40")) |>
select(ResponseId, starts_with("Vertrouwen_"), starts_with("Privacy"), starts_with("Perceptie"))
cleaned |> select(starts_with("Vertrouwen")) |> alpha()
cleaned |> select(starts_with("Privacygevoeligheid")) |> alpha()
cleaned |> select(starts_with("Perceptie_medewerkers")) |>alpha()
scaled = cleaned |>
rowwise() |>
mutate(Vertrouwen=mean(c_across(Vertrouwen_1:Vertrouwen_8)),
Privacygevoeligheid=mean(c_across(Privacygevoeligheid_1:Privacygevoeligheid_9)),
Perceptie_medewerkers=mean(c_across(Perceptie_medewerkers_1:Perceptie_medewerkers_8))
) |>
select(ResponseId, Vertrouwen, Privacygevoeligheid, Perceptie_medewerkers) |>
arrange(ResponseId)
scaled |> select(-ResponseId) |> cor(use = "pairwise")
cor.test(scaled$Vertrouwen, scaled$Privacygevoeligheid)
library(corrplot)
cleaned |>
select(-ResponseId) |>
cor(use = "pairwise") |>
corrplot(diag = F)
m = lm(Vertrouwen ~ Privacygevoeligheid + Perceptie_medewerkers, data=scaled)
summary(m)
library(sjPlot)
sjPlot::tab_model(m, show.std=T, show.ci=F)
library(tidyverse)
url = "https://raw.githubusercontent.com/ccs-amsterdam/r-course-material/master/data/income_topdecile.csv"
income_raw = read_csv(url) |> na.omit()
income = income_raw |> pivot_longer(-Year, names_to = 'country', values_to = 'income_topdecile')
url = "https://raw.githubusercontent.com/ccs-amsterdam/r-course-material/master/data/wealth_inequality.csv"
wealth_raw = read_csv(url)
wealth = pivot_longer(wealth_raw, -Year, names_to="key", values_to="value")
wealth = separate(wealth, key, into = c("country","measurement"), sep=":")
wealth |> mutate(measurement = trimws(measurement))
wealth = wealth %>% mutate(measurement = str_replace(measurement, " top ", "capital_top_"))
wealth = pivot_wider(wealth, names_from=measurement, values_from=value)
wealth = mutate(wealth, country = recode(country, "United Kingdom"="UK", "United States"="US"))
income = mutate(income, country = recode(country, "U.K."="UK", "U.S."="US"))
inequality = inner_join(income, wealth)
ggplot(inequality) + geom_line(aes(x=Year, y=income_topdecile, colour=country))
inequality2 = pivot_longer(inequality, -Year:-country, names_to="measure")
inequality2 |>
filter(country=="France") |>
ggplot() + geom_line(aes(x=Year, y=value, linetype=measure))
inequality2 |>
filter(measure %in% c("income_topdecile", "capital_top_decile"),
country != "Europe") |>
ggplot() + geom_line(aes(x=Year, y=value, linetype=measure)) + facet_wrap(vars(country))+
theme_minimal() +
theme(legend.position="bottom", legend.title = element_blank(), plot.title = element_text(hjust = 0.5)) +
ylab("Inequality") +
scale_linetype_discrete(name="Variable:", labels=c("Capital (top 10%)", "Income (top 10%)")) +
ggtitle("Capital and income inequality over time")
library(academictwitteR)
library(tidyverse)
library(tidytext)
library(ggwordcloud)
set_bearer()
tweets_raw = get_all_tweets(query = "#andrewtate", start_tweets = "2023-01-01T00:00:00Z", end_tweets = "2023-02-27T00:00:00Z", n = 1000)
tweets = tweets_raw |> select(id, created_at, text) |> as_tibble()
library(tidytext)
words = tweets |>
mutate(text = str_remove_all(text, "https://.*?\\b")) |>
unnest_tokens(input="text", output="word") |>
filter(!word %in% stop_words$word)
freqs = words |>
group_by(word) |>
summarize(n=n()) |>
arrange(-n)
freqs |>
head(150) |>
ggplot(aes(label=word, size=n, color=n)) +
geom_text_wordcloud() +
theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment