-
Notifications
You must be signed in to change notification settings - Fork 6
/
.Rhistory
512 lines (512 loc) · 15.8 KB
/
.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
plot(example3, col = "steelblue", pch = 20, xlab = "X", ylab = "Y",
main = "Correlation = 0")
plot(example4, col = "steelblue", pch = 20, xlab = "X", ylab = "Y",
main = "Correlation = 0")
library(tidyverse)
library(readxl)
read_excel(https://github.com/lisamarie0509/swirl-class/blob/main/Customer%20data.xlsx?raw=true)
read_excel(https://github.com/lisamarie0509/swirl-class/blob/main/Customer%20data.xlsx)
read_excel(https://github.com/lisamarie0509/swirl-class/raw/main/Customer%20data.xlsx)
as_tibble(df)
read_excel(https://github.com/lisamarie0509/swirl-class/raw/main/Customer%20data.xlsx)
read_excel("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.xlsx")
read_excel("https://github.com/lisamarie0509/swirl-class/raw/main/Customer%20data.xlsx")
read_excel("https://github.com/lisamarie0509/swirl-class/raw/main/Customer%20data.xlsx")
jjj <- read_excel("https://github.com/lisamarie0509/swirl-class/raw/main/Customer%20data.xlsx")
jjj <- read_excel("https://github.com/lisamarie0509/swirl-class/raw/main/Customer%20data.xlsx")
jjj <- read_excel("https://github.com/lisamarie0509/swirl-class/raw/main/Customer 20data.xlsx")
ccc <-read_excel("https://github.com/lisamarie0509/swirl-class/blob/main/Customer%20data.xlsx")
ccc <-read_excel("https://github.com/lisamarie0509/swirl-class/blob/main/Customer 20data.xlsx")
ccc <-read_excel("https://github.com/lisamarie0509/swirl-class/raw/main/Customer%20data.xlsx")
read_csv("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.csv")
?read_csv
read_csv("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.csv", delim=;)
read_csv("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.csv", delim=";")
read_csv("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.csv", delim=;)
read_csv("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.csv")
problems()
read_csv("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.csv", sep=";")
read.csv("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.csv", sep=";")
sss <- read.csv("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.csv", sep=";")
View(sss)
sss <- read_csv2("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.csv")
sss <- read_csv2("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.csv")
sss <- read_csv2("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.csv")
jjj <- read_excel("https://github.com/lisamarie0509/swirl-class/raw/main/Customer 20data.xlsx")
ccc <-read_excel("https://github.com/lisamarie0509/swirl-class/raw/main/Customer%20data.xlsx")
read_excel("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.xlsx")
problems()
serq <- read_excel("https://raw.githubusercontent.com/lisamarie0509/swirl-class/main/Customer%20data.xlsx")
```{r data generation, echo = FALSE, results = "hide"}
d <- data.frame(x = runif(100, -1, 1))
a <- 0
b <- sample(c(-1, 1), 1) * sample(c(0, 0.6, 0.9), 1)
d$y <- a + b * d$x + rnorm(100, sd = 0.25)
write.csv(d, "regression.csv", row.names = FALSE, quote = FALSE)
m <- lm(y ~ x, data = d)
bhat <- coef(m)[2]
bpvl <- summary(m)$coefficients[2, 4]
bsol <- c(bpvl >= 0.05, (bpvl < 0.05) & (bhat > 0), (bpvl < 0.05) & (bhat < 0))
plot(y ~ x, data = d)
To replicate the analysis in R:
```
## data
d <- read.csv("regression.csv")
## regression
m <- lm(y ~ x, data = d)
summary(m)
## visualization
plot(y ~ x, data = d)
abline(m)
```
?fmt
??fmt
install.packages("gt")
library("gt")
install.packages("exams")
?exams
??exams
devtools::install_github("koncina/unilur")
1:3 + 1:10
devtools::install_github("jrnold/r4ds-exercise-solutions")
Run `ggplot(data = mpg)` what do you see?
devtools::install_github("jonotuke/examMarking")
??examMarking
## ----------------------------------------------------------------------------------------
## install.packages("tidyverse")
library(tidyverse)
## list of pre loaded data
data()
# the mpg data set
data(mpg)
?mpg
head(mpg)
tail(mpg)
## list of pre loaded data
data()
seq(1,3)
seq(1,3,3)
seq(1,12,3)
seq(1,120,3)
112 115 118
tail()
tail(deaths)
tail(covid)
# install.packages("tidyverse")
# install.packages("ggpubr")
# install.packages("sjPlot")
library("tidyverse")
library("ggpubr")
library("sjPlot")
load(url("https://github.com/hubchev/courses/raw/main/dta/forest.Rdata"))
head(df)
tail(df)
dim(df)
summary(df)
df <- rename(df, country=country.x)
df <- rename(df, year=date)
df <- df %>%
mutate(gdp_pc = gdp/pop)
ggplot(df, aes(x = gdp_growth, y = unemployment_dif)) +
geom_point() +
stat_smooth(formula=y~x, method="lm", se=FALSE)
#18
ggplot(df, aes(x=gdp_growth, y=unemployment_dif)) + geom_point() +
stat_smooth(formula = y~x, method="lm", se=FALSE)
#15plot(df)
#16
plot(data=df(df$year==2015))
#15
plot(df)
#19
ggplot(df, aes(x=gdp_growth, y=unemployment_dif, label=income)) +
geom_point() + stat_smooth(formula = y~x, method="lm", se=FALSE) +
+
# geom_text(hjust=0, vjust=0) +
+
ggtitle("high income")
#18
ggplot(df, aes(x=gdp_growth, y=unemployment_dif)) + geom_point() +
stat_smooth(formula = y~x, method="lm", se=FALSE)
#19
ggplot(df, aes(x=gdp_growth, y=unemployment_dif, label=income)) +
geom_point() + stat_smooth(formula = y~x, method="lm", se=FALSE)
#20
ggarrange
ggplot(df, aes(x=gdp_growth, y=unemployment_dif, label=income)) +
geom_point() + stat_smooth(formula = y~x, method="lm", se=FALSE) +
+
# geom_text(hjust=0, vjust=0) +
+
ggtitle("high income")
ggplot(df, aes(x=gdp_growth, y=unemployment_dif, label=income)) +
geom_point() + stat_smooth(formula = y~x, method="lm", se=FALSE) +
+
# geom_text(hjust=0, vjust=0) +
+
ggtitle("low income")
#12
df <- df %>%
mutate(gdp_pc) = gdp/pop
#13
df %>%
group_by(income) %>%
summarise(meanunemploy = mean(unemployment), meangdp_pc = mean(gdp_pc))
#14
df%>%
group_by(year==2020) %>%
summarise(df, year, unemployment_diff, gdppc)
#15
pairs(~ gdp + gdp_pc + gdp_growth + unemployment + unemployment_dif,
data = data(gdp))
#15
pairs(~ gdp + gdp_pc + gdp_growth + unemployment + unemployment_dif,
data = df)
#18df %>%
ggplot(aes(gdp_growth,unemployment_diff)) +
geom_point(size=3) +
geom_smooth(method = lm, se = FALSE)
#18
df %>%
ggplot(aes(gdp_growth,unemployment_diff)) +
geom_point(size=3) +
geom_smooth(method = lm, se = FALSE)
#18
df %>%
ggplot(aes(gdp_growth,unemployment_dif)) +
geom_point(size=3) +
geom_smooth(method = lm, se = FALSE)
#21
#In the graphs you can see how the variables unemployment_dif and gdp_growth
#affect each other, if it is a High income country and once, if it is a Low
#income country. With High income you can see that the dispersion is realatively
#wide, unlike Low income. Than with Low income. Low income have an unstable
#correlation between the variable. High income, the other side, see that
#unemployment_dif is, the higher is the gdp_growth.
#22
reg_base <- lm(unemployment_dif ~ gdp_growth, data = df)
#I try this with the Panel, there is a mistake.
#23
summary(reg_base)
# 14
df %>%
filter(year > 2020) %>%
group_by(income) %>%
summarise(m_unemployment = mean(unemployment),
m_gdp_pc = mean(gdp_pc),
unemployment_dif,
gdp_growth)
# 14
df %>%
filter(year == 2020) %>%
group_by(income) %>%
summarise(m_unemployment = mean(unemployment),
m_gdp_pc = mean(gdp_pc),
unemployment_dif,
gdp_growth)
# 13
df %>%
group_by(income) %>%
summarise(m_unemployment = mean(unemployment),
m_gdp_pc = mean(gdp_pc))
# 14
df %>%
filter(year == 2020) %>%
group_by(income) %>%
summarise(m_unemployment = mean(unemployment),
m_gdp_pc = mean(gdp_pc),
unemployment_dif,
gdp_growth)
# 14
df %>%
filter(year == 2020) %>%
group_by(income) %>%
summarise(m_unemployment = mean(unemployment),
m_gdp_pc = mean(gdp_pc))
# 15
pairs(~gdp + gdp_pc + gdp_growth + unemployment + unemployment_dif, data =
df,
main = "Scatterplot Matrix")
# 16
pairs(~gdp + gdp_pc + gdp_growth + unemployment + unemployment_dif + year
== 2015,
data = df, main = "Scatterplot Matrix")
# 17cor(df$unemployment_dif, df$gdp_growth, method = c("pearson"))
# 18
ggplot(df, aes(x=gdp_growth, y=unemployment_dif))+
geom_point() +
stat_smooth(formula=y~x, method="lm", se=FALSE, colour="blue",
linetype=1)
# 19
ggplot(df, aes(x=gdp_growth, y=unemployment_dif)) +
geom_point( aes(income = "High income")) +
stat_smooth(formula = y ~ x, method = "lm",
se = FALSE, colour = "blue", linetype = 1) +
ggtitle("High income")
ggplot(df, aes(x=gdp_growth, y=unemployment_dif)) +
geom_point( aes(income = "Low income")) +
stat_smooth(formula = y ~ x, method = "lm",
se = FALSE, colour = "blue", linetype = 1)+
ggtitle("Low income")
# 20
gg1 <- ggplot(df, aes(x=gdp_growth, y=unemployment_dif)) +
geom_point( aes(income = "High income")) +
stat_smooth(formula = y ~ x, method = "lm",
se = FALSE, colour = "blue", linetype = 1) +
ggtitle("High income")
gg2 <- ggplot(df, aes(x=gdp_growth, y=unemployment_dif)) +
geom_point( aes(income = "Low income")) +
stat_smooth(formula = y ~ x, method = "lm",
se = FALSE, colour = "blue", linetype = 1)+
ggtitle("Low income")
ggarrange(gg1, gg2)
allobservation <- lm(unemployment_dif ~ gdp_growth, data = df)
summary(allobservation)
highincome <- lm(unemployment_dif ~ gdp_growth, income == "High income",
data = df)
summary(highincome)
lowincome <- lm(unemployment_dif ~ gdp_growth, income == "Low income", data
= df)
summary(lowincome)
# 23
as.table("allobservation", "highincome", "lowincome")
# variables:country, date
#8 there are 11 observations and 6 variables
#9 summary statistics
summary(df)
#13table(df$unemployment, df$gdp_pc)
#14
table(df$unemployment, df$gdppc, df$unemployment_dif, df$gdp_growth)
#15
plot(gdp, gdp_pc, gdp_growth, unemployment, unemployment_dif)
load(url("https://github.com/hubchev/courses/raw/main/dta/forest.Rdata"))
head(df)
tail(df)
dim(df)
summary(df)
df <- rename(df, country=country.x)
df <- rename(df, year=date)
df <- df %>%
mutate(gdp_pc = gdp/pop)
#15
plot(gdp, gdp_pc, gdp_growth, unemployment, unemployment_dif)
results <- lm(unemployment_dif ~ gdp_growth, data=df)
cor.test(df$unemployment_dif, df$gdp_growth)
summary(results)
#18
ggplot(df, (x=gdp_growth, y=unemployment_dif))
conf.int = TRUE
plot(unemployment_dif ~ growth
data = df
main= "scatterplot of unemployment_dif and growth"
xlab = "growth (X)",
ylab= "unemplyoment_dif (Y)")
#14
df %>%
group_by(year == 2020) %>%
summarise(df,year,unemployment_dif, gdppc)
#15
pairs(~ gdp + gdp_pc + gdp_growth + unemployment + unemployment_diff, data = df)
#15
pairs(~ gdp + gdp_pc + gdp_growth + unemployment + unemployment_dif, data = df)
df <- df %>%
filter (year == 2015) %>%
paris(~gdp + gdp_pc + gdp_growth + unemployment + unemployment_dif, data = df)
df <- df %>%
filter (year == 2015) %>%
pairs(~gdp + gdp_pc + gdp_growth + unemployment + unemployment_dif, data = df)
df <- df %>%
filter (year == 2015) %>%
pairs(~gdp + gdp_pc + gdp_growth + unemployment + unemployment_dif)
df <- df %>%
filter (year == 2015) %>%
pairs(~gdp + gdp_pc + gdp_growth + unemployment + unemployment_dif, data = .)
#17
cor(df$unemployment_dif, df$gdp_growth, method=c("person"))
#18
df %>%
ggplot(aes(gdp_growth, unemployment_dif)) +
geom_point(size=3)+
geom_smooth(method = lm, se = FALSE)
load(url("https://github.com/hubchev/courses/raw/main/dta/forest.Rdata"))
head(df)
tail(df)
dim(df)
summary(df)
df <- rename(df, country=country.x)
df <- rename(df, year=date)
df <- df %>%
mutate(gdp_pc = gdp/pop)
#18
df %>%
ggplot(aes(gdp_growth, unemployment_dif)) +
geom_point(size=3)+
geom_smooth(method = lm, se = FALSE)
library("swirl")
ls()
rm(list=ls())
install_course_github("hubchev", "swirl-it")
swirl()
# do some calculation for training purpose
x <- 5
x2 <- 6
resultofxandx2 <- x + x2
# do some calculation for training purpose
x <- 99995
x2 <- 6
resultofxandx2 <- x + x2
# do some calculation for training purpose
x <- 9.9995
x2 <- 6
resultofxandx2 <- x + x2
install.packages("swirl")
library("swirl")
rm(list=ls())
install_course_github("hubchev", "swirl-it")
swirl()
sltjd()
skip()
swirl()
x <- 678
rm(list=ls())
ls()
x <- 678
ls()
?ls
?rm
# install.packages("tidyverse")
# install.packages("ggpubr")
# install.packages("sjPlot")
library("tidyverse")
detach("package:forcats", unload = TRUE)
library(swirl)
swirl()
source("~/Dropbox/hsf/github/courses/scr/exe_subset.R")
swirl()
nxt()
skip()
skip()
skip()
skip()
skip()
skip()
setwd("/home/sthu/Dropbox/hsf/github/courses/rmd/")
rmarkdown::render("22-10_dsb-project-description.Rmd", "all")
setwd("/home/sthu/Dropbox/hsf/github/courses/rmd/")
rmarkdown::render("22-10_dsb-project-description.Rmd", "all")
setwd("/home/sthu/Dropbox/hsf/github/courses/rmd/")
rmarkdown::render("22-10_dsb-project-description.Rmd", "all")
setwd("/home/sthu/Dropbox/hsf/github/courses/rmd/")
rmarkdown::render("22-10_dsb-project-description.Rmd", "all")
setwd("/home/sthu/Dropbox/hsf/github/courses/rmd/")
rmarkdown::render("22-10_dsb-project-description.Rmd", "all")
setwd("/home/sthu/Dropbox/hsf/github/courses/rmd/")
rmarkdown::render("checklist.Rmd", "all")
setwd("/home/sthu/Dropbox/hsf/github/courses/rmd/")
rmarkdown::render("checklist.rmd", "all")
setwd("/home/sthu/Dropbox/hsf/github/courses/rmd/")
rmarkdown::render("22-10_dsb-project-description.Rmd", "all")
?diff
x1 = rnorm(100)
x2 = rnorm(100)
x3 = rnorm(100)
t = data.frame(a=x1, b=x1+x2, c=x1+x2+x3)
# Plot data frame
plot(t$a, type='l', ylim=range(t), lwd=3, col=rgb(1,0,0,0.3))
lines(t$b, type='s', lwd=2, col=rgb(0.3,0.4,0.3,0.9))
points(t$c, pch=20, cex=4, col=rgb(0,0,1,0.3))
points(t$c, pch=20, cex=3, col=rgb(0,0,1,0.3))
points(t$c, pch=20, cex=2, col=rgb(0,0,1,0.3))
points(t$c, pch=20, cex=8, col=rgb(0,0,1,0.3))
points(t$c, pch=20, cex=4, col=rgb(0,0,1,0.3))
# Plot data frame
plot(t$a, type='l', ylim=range(t), lwd=3, col=rgb(1,0,0,0.3))
lines(t$b, type='s', lwd=2, col=rgb(0.3,0.4,0.3,0.9))
points(t$c, pch=20, cex=4, col=rgb(0,0,1,0.3))
points(t$c, pch=10, cex=4, col=rgb(0,0,1,0.3))
points(t$c, pch=20, cex=4, col=rgb(0,0,1,0.3))
x1 = rnorm(100)
x2 = rnorm(100)
x3 = rnorm(100)
t = data.frame(a=x1, b=x1+x2, c=x1+x2+x3)
# Plot data frame
plot(t$a, type='l', ylim=range(t), lwd=3, col=rgb(1,0,0,0.3))
lines(t$b, type='s', lwd=2, col=rgb(0.3,0.4,0.3,0.9))
points(t$c, pch=20, cex=4, col=rgb(0,0,1,0.3))
# Plot data frame
plot(t$a, type='l', ylim=range(t), lwd=3, col=rgb(1,0,0,0.3))
lines(t$b, type='s', lwd=2, col=rgb(0.3,0.4,0.3,0.9))
points(t$c, pch=20, cex=2, col=rgb(0,0,1,0.3))
?plot
# Plot data frame
plot(t$a, type='l', ylim=range(t), lwd=1, col=rgb(1,0,0,0.3))
lines(t$b, type='s', lwd=2, col=rgb(0.3,0.4,0.3,0.9))
points(t$c, pch=20, cex=2, col=rgb(0,0,1,0.3))
# Plot data frame
plot(t$a, type='l', ylim=range(t), lwd=9, col=rgb(1,0,0,0.3))
# Plot data frame
plot(t$a, type='l', ylim=range(t), lwd=2, col=rgb(1,0,0,0.3))
load()
data()
data(mtcars)
force(mtcars)
View(mtcars)
data()
View(mtcars)
head(mtcars, 6)
?mtcars
nrow(mtcars)
ncol(mtcars)
summary(mtcars)
quantile(mtcars$wt)
var(mtcars$wt)
hist(mtcars$hp)
?vars
?var
# clear the environment
rm(list = ls())
# set working directory
setwd("/home/sthu/Dropbox/hsf/github/courses/")
# Let us do the following:
# 1. check if a package is installed
# 2. if not installed the package should be installed and loaded
# 3. if installed the package should be loaded
# I like to do it with a function that is part of pacman package:
if (!require("pacman")) {install.packages("pacman")}
pacman::p_load(haven, tidyverse, vtable, gtsummary, pastecs, Hmisc,
sjlabelled, tis, ggpubr, sjPlot)
convergence.dta
# import data
data <- read_dta("dta/convergence.dta")
View(data)
# inspect data
names(data)
str(data)
data
head(data)
tail(data)
summary(data)
view(data)
#library(vtable)
vtable(data, missing=TRUE)
# library(pastecs)
stat.desc(data)
# library(Hmisc)
describe(data)
# library(gtsummary)
tbl_summary(data)
# library(Hmisc)
describe(data)
# library(gtsummary)
tbl_summary(data)
View(data)
# check the assignements of countries to continents
data %>%
select(country, africa, asia, weurope) %>%
view()
data <- mutate(data, x_1 = africa + asia + weurope)
View(data)
data %>%
filter(x_1==0) %>%
select(africa, asia, weurope, country) %>%
view()