-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkInR_forPrinter.Rnw
290 lines (212 loc) · 13.9 KB
/
WorkInR_forPrinter.Rnw
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
\documentclass[a4paper, 12pt]{article}
\title{This is my work in R}
\author{ MyFirstName MyLastName\\OrganizationName\\
City, ZipCope, \underline{Country}\\
\url{[email protected]}}
\date{\today} %% manually: \date{January 30, 2024}
\usepackage[natbibapa]{apacite} %% for bibliography
\usepackage{rotating, graphicx} %% for rotating tables
\usepackage{adjustbox} % size of plots and tables
\usepackage{chngcntr}% section numbering
\counterwithin{table}{section}\counterwithin{figure}{section}
\begin{document} % every "begin: needs and "end"
\SweaveOpts{concordance=TRUE}
\maketitle
\begin{abstract}
This is an example of an abstract in a paper. This is an example of an abstract in a paper. This is an example of an abstract in a paper. This is an example of an abstract in a paper.This is an example of an abstract in a paper.This is an example of an abstract in a paper.
\end{abstract}
\section*{Introduction} % * to unnumber
This is just my intro to my nice paper. This is just my intro to my nice paper.This is just my intro to my nice paper. This is just my intro to my nice paper.This is just my intro to my nice paper. This is just my intro to my nice paper.This is just my intro to my nice paper. This is just my intro to my nice paper.This is just my intro to my nice paper. This is just my intro to my nice paper.This is just my intro to my nice paper. This is just my intro to my nice paper.
This is just my intro to my nice paper. This is just my intro to my nice paper.This is just my intro to my nice paper. This is just my intro to my nice paper.This is just my intro to my nice paper. This is just my intro to my nice paper.This is just my intro to my nice paper. This is just my intro to my nice paper.
\section{Exploring Tables}\label{explo-tables} % label for crossref
%footnote coming
Another section. I will use a footnot now \footnote{This is a footnote.}. I will soon use cross-ref.I will soon use cross-ref.I will soon use cross-ref.I will soon use cross-ref. I will soon use cross-ref. I will soon use cross-ref.I will soon use cross-ref.I will soon use cross-ref.I will soon use cross-ref.I will soon use cross-ref. I will soon use cross-ref. I will soon use cross-ref.
%cross-ref coming
%cross-ref coming
I will soon use cross-ref.I will soon use cross-ref: as we see in Section \ref{catexplor}.
% a chunk hidden (echo=FALSE) with some setup
% chunk has its own name
<<collecting-DataTable, echo=FALSE>>=
# clean memory
rm(list = ls())
# paste the github link here (in two parts):
linkOrg=""
linkRepo=""
file_Name1="hdidemiso_plus.RDS"
link_file_Name1=paste0(linkOrg,linkRepo,file_Name1)
#link_file_Name1=url(link_file_Name1)
#opening data
mergedData=readRDS(link_file_Name1)
@
\subsection{Exploring Categorical Data}\label{catexplor}
Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.
I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.
You can see the statistics of a categorical variable in Table \ref{catexploreTable}.
%the chunk name is NOT used in cross-ref for table.
%Table is created and displayed.
<<catexploreTable,results=tex,echo=FALSE>>=
## display frequency table of Freedom Index
library(summarytools)
library(tidyr)
library(knitr)
library(kableExtra)
namesHeaders=c("Democracy Level","count","pct","pct_cumm")
freq(mergedData$Regimetype,
plain.ascii = FALSE,
report.nas=FALSE) %>%
## changes, we need data frame
tb() %>%
kable(format = "latex",
digits = 2, #label cross-ref!!
caption = "This is a table\\label{catexploreTable}",
col.names = namesHeaders)%>%
kable_styling(full_width = F,latex_options = "h")
@
%%%%%%
You can see this variable plotted in Figure \ref{catBarplot} on page \pageref{catBarplot}.
% NO need for figure caption in chunk header.
% Object will be created BUT NOT plotted
% the chunk name IS NOT used in cross-ref for figure
\begin{figure}[h]
\centering
\begin{adjustbox}{width=7cm,height=5cm} %resize
<<catBarplot, echo=FALSE, fig=TRUE>>=
library(ggplot2)
base=ggplot(data = mergedData)
bar=base + geom_bar(aes(x=Regimetype),
fill='orange',
color='red')
bar=bar + labs(title = "Democracy in the world (2021)",
x= "Levels",
y= "Count of countries",
caption = 'Source: The Economist')
bar
@
\end{adjustbox}
\caption{Press Freedom Index in the World} %title
\label{catBarplot} % for cross-ref
\end{figure}
\subsection{Exploring Numerical Data}\label{numexplo}
Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.
Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.
Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.
<<summaryNumeric, results=tex, echo=FALSE>>=
## table of summary statistics for numerical columns
AllNumericNames=names(Filter(is.numeric, mergedData))
originalNumeric=AllNumericNames[!grepl('mM',AllNumericNames)]
## table of summary statistics
library(stargazer)
summaryHeaders=c("n","mean",'sd',"min",'p25','p75','max')
stargazer(mergedData[,originalNumeric],
title = "Stat summary for numeric vars",
label = "summaryNumeric",# cross-ref
font.size = 'scriptsize',
digits = 2,
summary.stat = summaryHeaders)
@
% Use of R object inline
In the Table \ref{summaryNumeric}, you realize that the mean of HDI is {\bf\Sexpr{round(mean(mergedData$HumanDevelopmentIndex),2)}}. It would be good to see a boxplot, check Figure \ref{numBoxplot} below.
\begin{figure}[h]
\centering
\begin{adjustbox}{width=12cm,height=8.5cm,clip,trim=0cm 0.5cm 0cm 0cm}
<<numBoxplot, echo=FALSE, fig=TRUE>>=
scaledNumeric=AllNumericNames[grepl('mM',AllNumericNames)]
DataForBox=reshape2::melt(mergedData[,scaledNumeric])
base=ggplot(data=DataForBox)
box=base + geom_boxplot(aes(x=variable,y=value),
fill='lightblue',
color='red') + coord_flip()
## plotting
box
@
\end{adjustbox}
\caption{Money spent per country on Military stuff}
\label{numBoxplot}
\end{figure}
%%%%% A citation: author(year)
Boxplots were introduced by \citet{tukey_exploratory_1977}. You can also flip the Table \ref{summaryNumeric}, as shown in Table \ref{summaryNumeric-flip}.
<<summaryNumeric-flip, results=tex, echo=FALSE>>=
stargazer(mergedData[,originalNumeric],
title = "Stat summary for numeric vars (flipped)",
label = "summaryNumeric-flip",# cross-ref
font.size = 'scriptsize',
digits = 2,
summary.stat = summaryHeaders,
float.env = "sidewaystable")
@
\pagebreak
%%%%
\section{My Regression}\label{regre}
Several times we need regression. This is a nice summary for two regressions, as shown in Table \ref{regsPrint}:
<<regresPrep, echo=FALSE>>=
## prepare regressions
h1=formula(HumanDevelopmentIndex~Functioningofgovernment)
regre1=glm(h1,data=mergedData,family = "gaussian")
h2=formula(HumanDevelopmentIndex~Functioningofgovernment + Politicalparticipation)
regre2=glm(h2,data=mergedData,family = "gaussian")
@
<<regsPrint, echo=FALSE,results=tex>>=
## show result of both
stargazer(regre1,regre2,
title = "Models for HDI",
dep.var.labels = "Human Development",
covariate.labels = c("Bureaucracy", "Participation"),
label = 'regsPrint')
@
I hope you like what you see in the Table \ref{regsPrint}. You can learn more on regression in
other book \citep[150-160]{petrie_introduction_2016}
%%%%
\section{Other plots}\label{otherPlots}
\subsection{A map}\label{mapPlot}
Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.
Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.
Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map.Let me show a nice map in Figure \ref{plot-cityMap}.
<<collecting-DataMap, message=FALSE, echo=FALSE>>=
# github link to data already saved!
file_Name2="MapSeattle.geojson"
file_Name3="calls911_geo.geojson"
link_file_Name2=paste0(linkOrg,linkRepo,file_Name2)
link_file_Name3=paste0(linkOrg,linkRepo,file_Name3)
#opening data
library(sf)
cityMap=read_sf(link_file_Name2)
eventsLocations=read_sf(link_file_Name3)
@
\begin{figure}[h]
\centering
\begin{adjustbox}{width=10cm,height=10cm}
<<plot-cityMap, echo=FALSE, fig=TRUE>>=
library(ggplot2)
ggplot(cityMap) +geom_sf(aes(fill=ACRES_TOTAL))
@
\end{adjustbox}
\caption{Plot numeric colums.}
\label{plot-cityMap}
\end{figure}
Figure \ref{plot-cityMap} uses only one layer. Let's add another layer in the next map. Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it.
\begin{figure}[h]
\centering
\begin{adjustbox}{width=10cm,height=10cm}
<<plot-911Map, echo=FALSE, fig=TRUE>>=
base=ggplot(cityMap) + geom_sf() + theme_light()
eventsByTime=base + geom_sf(data = eventsLocations,
size=0.5,
aes(color=nightTime))
eventsByTime
@
\end{adjustbox}
\caption{Calls to 911 by time of day.}
\label{plot-911Map}
\end{figure}
You can see that Figure \ref{plot-911Map} actually uses one map on top of the other.
Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it. It has been a very hard work.Here, I continue doing this nice work, I hope you like it and read it.
%% more than one author in a citation
Review other authors
(\citealp[120-160]{brunsdon_introduction_2015};
\citealp[also, see][]{camara_spatial_nodate}) to know more.
\newpage
%%%%% adding bibliography
\bibliographystyle{apacite} %%style
%\renewcommand{\refname}{Bibliography}
\bibliography{Bib_winterschool} %% filename
\end{document} %% nothing after here