forked from ashokkrish/CougarStats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
403 lines (403 loc) · 11.6 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
save.image("~/MRU/rstudio cougarstats.RData")
load("~/MRU/rstudio cougarstats.RData")
shiny::runApp('MRU/CougarStats')
runApp('MRU/CougarStats')
runApp('MRU/CougarStats')
runApp('MRU/CougarStats')
runApp('MRU/CougarStats')
load("~/MRU/R Practice/firstTest.R")
#test
5 + 6
a <- 5
b <- 6
a + b
sum(a+b)
ages <- c(a,b)
ages <- c(a,b)
ages
sum(ages)
sum(ages)
a <- 10
b <- 3
sum(ages)
ages <- c(a,b)
sum(ages)
sum(ages)
b <- 3
sum(ages)
sum(ages)
sum(ages)
a <- 11
b <- 3
ages <- c(a,b)
sum(ages)
a <- 10
sum(ages)
#test
5 + 6
a <- 10
b <- 3
a + b
sum(a+b)
ages <- c(a,b)
ages
sum(ages)
names <- c("John", "James")
friends <- data.frame(names, ages)
View(friends)
view(friends)
5 + 6
a <- 10
b <- 3
a + b
sum(a+b)
ages <- c(a,b)
ages
sum(ages)
names <- c("John", "James")
friends <- data.frame(names, ages)
view(friends)
library(utils)
view(friends)
friends <- data.frame(names, ages)
names <- c("John", "James")
view(friends)
## Creating Tables ----
table(msleep$vore)
# Basics: objects, concatenations, dataframes ----
5 + 6
a <- 10 # save 10 in an object 'a'
b <- 3
a + b
sum(a+b)
ages <- c(a,b) # concatenate a and b, save in object ages
ages
sum(ages)
names <- c("John", "James")
friends <- data.frame(names, ages) # create new data frame
View(friends) # View the table thing
str(friends) # structure of friends
friends$ages # look at variables of ages
friends$names # look at variables of names
friends[1,1] # display the observation/record? at this position [row,col]
friends[1, ] # display all columns at row 1
friends[ ,1] # display all rows at column 1
# Built in data sets for practice ----
## Star Wars ----
install.packages("tidyverse")
library(tidyverse)
data()
View(starwars)
starwars %>% # pipe operator (shift+ctrl+M). Means "and then"
filter(height > 150 & mass < 200) %>% # filter observations
mutate(height_in_meters = height/100) %>%
select(height_in_meters, mass) %>% # select column, like in SQL
arrange(mass) %>%
View()
# Data structure and types of variables
## Mammalian Sleep ----
View(msleep)
glimpse(msleep) # much like str(friends). view the structure of the data
head(msleep) # displays first 6 rows in console
class(msleep$name) # what is the type class of the name variable in the msleep dataset?
length(msleep) # how many variables (columns) in msleep
length(msleep$name) # how many observations of this variable
names(msleep) # all variables in msleep
unique(msleep$vore) # all unique observations of this variable
missing <- !complete.cases(msleep) # complete gives rows where all variables are filled
# !complete can be saved in missing
msleep[missing, ] # display all columns of the missing rows
## Clean ----
starwars %>%
select(name, height, mass)
starwars %>% # select columns 1-3
select(1:3)
starwars %>%
select(ends_with("color"))
starwars %>% # switch order of columns
select(name, mass, height, everything()) %>% # everything() = everything else
View()
starwars %>% # rename a variable
rename("characters" = "name") %>%
head()
class(starwars$hair_color)
starwars$hair_color <- as.factor(starwars$hair_color) # change type to factor
class(starwars$hair_color)
starwars %>%
mutate(hair_color = as.character(hair_color)) %>% # change back to char
glimpse()
## Changing factor levels ----
df <- starwars
df$sex <- as.factor(df$sex)
levels(df$sex) # similar to using unique(). returns in alphabetical order
df <- df %>% # change the order of levels
mutate(sex = factor(sex, levels = c("male", "female", "hermaphroditic", "none"))) %>%
glimpse()
levels(df$sex) # changed. levels are categories (only in factors) within that variable
## Filter rows ----
starwars %>%
select(mass, sex) %>%
filter(mass < 55 & sex == "female")
## Recode data ----
starwars %>% # change words in records
select(sex) %>%
mutate(sex = recode(sex, "male" = "man", "female" = "woman"))
## Dealing with missing data ----
mean(starwars$height) # doesn't work bc of missing data
mean(starwars$height, na.rm = TRUE) # remove NA values
## Dealing with duplicates ----
Names <- c("Peter", "John", "Andrew", "Peter")
Age <- c(22, 33, 44, 22)
Friends <- data.frame(Names, Age)
Friends
Friends %>% # displays distinct records (doesn't alter dataframe itself)
distinct() # same as distinct(Friends) by itself
## Manipulating data----
starwars %>%
mutate(height_m = height/100) %>%
select(name, height, height_m)
## Conditional change (if_else) ----
starwars %>%
mutate(height_m = height/100) %>%
select(name, height, height_m) %>%
mutate(tallness = if_else(height_m < 1, "short", "tall"))
# Pivot ----
## Reshape data with Pivot wider----
library(gapminder)
View(gapminder)
data <- select(gapminder, country, year, lifeExp) # select from gapminder as first arg
View(data)
wide_data <- data %>%
pivot_wider(names_from = year, values_from = lifeExp) # first arg is variables, second arg is values
View(wide_data)
## Reshape data with Pivot longer ----
long_data <- wide_data %>%
pivot_longer(2:13, names_to = "year", values_to = "lifeExp") # first arg is columns to be changed, second and third are variables to be made
View(long_data)
# Describing data ----
## Range / spread ----
min(msleep$awake)
max(msleep$awake)
range(msleep$awake)
IQR(msleep$awake)
## Centrality ----
mean(msleep$awake)
median(msleep$awake)
## Variance ----
var(msleep$awake)
## Summary ----
summary(msleep$awake) # gives min, max, mean, median, IQR
msleep %>% # summarize more variables
select(awake, sleep_total) %>%
summary()
# Longer Summarize Practice ----
## Mammalian Sleep ----
msleep %>%
drop_na(vore) %>% # drop missing values
group_by(vore) %>% # group by vore categories
summarise(Lower = min(sleep_total),
Average = mean(sleep_total),
Upper = max(sleep_total),
Difference = Upper - Lower) %>%
arrange(Average) %>%
View()
## Creating Tables ----
table(msleep$vore)
## Creating Tables ----
table(msleep$vore)
# Data structure and types of variables
## Mammalian Sleep ----
library(msleep)
library(tidyverse)
data()
## Creating Tables ----
table(msleep$vore)
msleep %>%
select(vore, order) %>%
filter(order %in% c("Rodentia", "Primates")) %>%
table()
plot(pressure)
## The Grammar of Graphics (data, mapping, geometry) ----
### Bar plots ----
ggplot(data = starwars, mapping = aes(x = gender)) + geom_bar()
## The Grammar of Graphics (data, mapping, geometry) ----
### Bar plots ----
ggplot(data = starwars,
mapping = aes(x = gender)) + # isntead of %>% , add geometry (bar plot)
geom_bar()
### Histograms ----
starwars %>%
drop_na(height) %>%
ggplot(mapping = aes(x = height)) +
geom_histogram()
### Box Plots ----
starwars %>%
drop_na(height) %>%
ggplot(aes(height)) + # both "mapping =" and "x =" are assumed by default
geom_boxplot(fill = "steelblue") +
theme_bw() + # built-in themes
labs(title = "Boxplot of height", # labels
x = "Height of characters")
filter(sex %in% c("male, "female"))
source("~/MRU/R Practice/firstTest.R", echo=TRUE)
### Density Plots ----
starwars %>%
drop_na(height) %>%
filter(sex %in% c("male", "female")) %>%
ggplot(aes(x = height,
color = sex, # color of lines
fill = sex)) +
geom_density(alpha = 0.2) +
theme_bw()
### Scatter Plots ----
starwars %>%
filter(mass < 200) %>%
ggplot(aes(height, # first arg assumed to be x
mass, # second arg assumed to be y
color = sex)) + # color mapped against sex
geom_point(size = 5, alpha = 0.5) + # size = size of points
theme_bw() +
labs(title = "Height and mass by sex")
### Scatter Plots ----
starwars %>%
filter(mass < 200) %>%
ggplot(aes(height, # first arg assumed to be x
mass, # second arg assumed to be y
color = sex)) + # color mapped against sex
geom_point(size = 5, alpha = 0.5) + # size = size of points
theme_min() +
labs(title = "Height and mass by sex")
### Scatter Plots ----
starwars %>%
filter(mass < 200) %>%
ggplot(aes(height, # first arg assumed to be x
mass, # second arg assumed to be y
color = sex)) + # color mapped against sex
geom_point(size = 5, alpha = 0.5) + # size = size of points
theme_minimal() +
labs(title = "Height and mass by sex")
### Smoothed Model ----
starwars %>%
filter(mass < 200) %>%
ggplot(aes(height, mass, color = sex)) +
geom_point(size = 3, alpha = 0.8) +
geom_smooth() +
facet_wrap(~sex) +
theme_minimal() +
labs(title = "Height and mass by sex")
### Smoothed Model ----
starwars %>%
filter(mass < 200) %>%
ggplot(aes(height, mass, color = sex)) +
geom_point(size = 3, alpha = 0.8) +
geom_smooth() +
facet_wrap(~sex) +
theme_bw() +
labs(title = "Height and mass by sex")
warning()
warnings()
### Smoothed Model ----
starwars %>%
filter(mass < 200) %>%
ggplot(aes(height, mass, color = sex)) +
geom_point(size = 3, alpha = 0.8) +
geom_smooth() + # draws a smooth linear model over points
facet_wrap(~sex) + # facet wrap by sex (organize into little boxes)
theme_bw() +
labs(title = "Height and mass by sex")
warnings()
# Analyze ----
## Hypothesis Testing ----
### T-test ----
library(gapminder)
View(gapminder)
t_test_plot
gapminder %>%
filter(continent %in% c("Africa", "Europe")) %>%
t.test(lifeExp ~ continent, # lifeExp as a function of continent
data = ., # . = put data explicitly at this spot??
alternative = "two.sided", # assumed
paired = FALSE) # assumed
### ANOVA ----
gapminder %>%
filter(year == 2007) %>%
filter(continent %in% c("Americas", "Europe", "Asia")) %>%
aov(lifeExp ~ continent, data = .) %>%
summary()
gapminder %>%
filter(year == 2007) %>%
filter(continent %in% c("Americas", "Europe", "Asia")) %>%
aov(lifeExp ~ continent, data = .) %>%
TukeyHSD()
gapminder %>%
filter(year == 2007) %>%
filter(continent %in% c("Americas", "Europe", "Asia")) %>%
aov(lifeExp ~ continent, data = .) %>%
TukeyHSD() %>%
plot()
View(flowers)
flowers <- iris %>%
mutate(Size = cut(Sepal.Length, # new variable called Size
breaks = 3, # 3 breaks, 3 intervals named:
labels = c("Small", "Medium", "Large"))) %>%
select(Species, Size)
View(flowers)
#### Goodness of Fit Test ----
flowers %>%
select(Size) %>%
table() %>%
chisq.test()
#### Test of Independence ----
flowers %>%
table() %>%
chisq.test()
#### Test of Independence ----
flowers %>%
table() %>%
chisq.test()
flowers %>%
table() %>%
flowers %>%
table()
flowers %>%
table()
#### Test of Independence ----
flowers %>%
table() %>%
chisq.test()
# Linear Model (Regression) ----
head(cars, 10)
cars %>%
lm(dist ~ speed, data = .) %>%
summary()
### Box Plots ----
starwars %>%
drop_na(height) %>%
ggplot(aes(height)) + # both "mapping =" and "x =" are assumed by default
geom_boxplot(fill = "steelblue") +
theme_bw() + # built-in themes
labs(title = "Boxplot of height", # labels
x = "Height of characters")
### Smoothed Model ----
starwars %>%
filter(mass < 200) %>%
ggplot(aes(height, mass, color = sex)) +
geom_point(size = 3, alpha = 0.8) +
geom_smooth() + # draws a smooth linear model over points
facet_wrap(~sex) + # facet wrap by sex (organize into little boxes)
theme_bw() +
labs(title = "Height and mass by sex")
source("~/.active-rstudio-document", echo=TRUE)
shiny::runApp('MRU/R Practice')
runApp('MRU/CougarStats')
runApp('MRU/R Practice')
runApp('MRU/CougarStats')
runApp('MRU/R Practice')
runApp('MRU/CougarStats')
runApp('MRU/CougarStats - Sam's Fork/CougarStats')
runApp('MRU/CougarStats - Sam's Fork/CougarStats')
shiny::runApp('MRU/CougarStats - SB/CougarStats')
runApp('MRU/CougarStats - SB/CougarStats')
shiny::runApp('MRU/CougarStats - SB/CougarStats')
runApp('MRU/CougarStats')
runApp('MRU/CougarStats - SB/CougarStats')