forked from CrumpLab/LabJournalWebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotes.Rmd
More file actions
629 lines (462 loc) · 12.5 KB
/
Notes.Rmd
File metadata and controls
629 lines (462 loc) · 12.5 KB
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
---
title: "Notes"
output:
html_document:
toc: true
toc_float: true
collapsed: false
number_sections: false
toc_depth: 1
#code_folding: hide
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(message=FALSE,warning=FALSE, cache=TRUE)
```
# going over a lesson on 2/4/2019
```{r}
a<- 1
b<-"Hello World"
d<-"3"
print(a)
class(d)
vector<- c(1,2,3,4,5,6,7,8)
vector[2:4]
vector[c(2,2,2,2,2)]
vector[vector <6] # show the numbers less than 7
vector[vector>=6] # greater or equal to 6
vector[vector!=4] # show numbers that are not 5
vector[8]<-3000 # put 3000 in the place of 8th number
vector
vector[1:2] <- 10
class(vector)
vector[7]<- "j" # put a number in the 7th place in the vector
vector
a<-c("1","2","3")
class(a)
b<- a
class(b)
as.numeric(b) # store charecters 123 as a number
a<- "ljjasjdflsjasf;lasdjflk;asdjfl;asdjfl"
strsplit(a,split="") # if you had a paragraph and you wanted to split the paragraph or put a period in R
a[1]
b[(1)][5]
example_list <- list(a=11, b=4, d=c(1,2,3,4))
example_list
data.frame() # particular way of structuring data
f<- matrix(0,nrow = 5, ncol = 3) # creating 5 rows and 3 columns
first_names <- c("asdf", "Asdfasd","asdfasdf")
ages<-c(3343,23122,67456)
grades<-c(89,56,99)
everybody <- data.frame(first_names,ages,grades)
everybody$ages[2] <- 12
```
```{r}
data1 <- c(1,2,3)
data1[1]
data1[-1]
```
# 2/11/2019 3rd lecture
Downloading R repository
on February 25th we will talk about data analysis
assignment 3 (R Basics) - R package assignment - how R code works, how it is shared
I will be submiting a link to a new R repository
R functions are prepackaged - in Packages - collect functions you have worked on
click instal tab in packages - repository cran allows y ou to save on cloud - choose your package and install
next step load the package - click the package button
# creating a simple function of adding 1 to whatever number you indicated
'''
my_function<- function(input){
#body of code
return(output)
}
'''
```{r}
add_one <- function(x){
return(x+1)
}
add_one(9)
```
'''
{r, eval=FALSE}
add_one <- function(x){
# add one tox, and save the output
save_result <- x+1
another way to do it
'''
{r}
add_one <- function(x) return(x+1)
'''
```{r}
add_one <- function(x) return(x+1)
add_one(7)
```
# how can we roll a die and get a number
'''
{r}
sample(1:6,1)
roll_dice <- function(){
return(sample(1:6,1))
}
roll_dice() # make sure you put something in the parenthesis
'''
```{r}
roll_dice <- function(x){
return(sample(1:6,1))
}
roll_dice()
```
creating a sum function
```{r}
x <- c(1,2,3)
my_sum <- function(x){ # this is declaring a funtion
get_sum<-sum(x)
return(get_sum)
}
# to declare a variable
a <- c(1,2,3)
```
# find a mean of restricted range of numbers
```{r}
ranged_mean <- function(x, min_val, max_val){
restricted_values <- x[x>min_val & x < max_val]
return(mean(restricted_values))
}
some_numbers <-c(3,4,3,2,3,4,5,6,7,8,8,8,9,8)
ranged_mean(some_numbers,3,9)
```
```{r,eval=F}
ranged_mean <- function(x, min_val, max_val){
restricted_values <- x[x>min_val & x < max_val]
outputs <- list(original_values = x,
restricted_values=restricted_values
restricted_mean = mean(restricted_values)
}
some_numbers <-c(3,4,3,2,3,4,5,6,7,8,8,8,9,8)
ranged_mean(some_numbers,2,4)
```
# how to make new R project
steps
1.
2.
3.
# 2/25/19February 25th data visualization today
APA paper with papaja on March 25th
making graphs
a <- data.fram()
simple reaction time - how fast can you respond to the light
first column DV - simple reaction time
SRT IV - how bright the light is AGE Subject Address
150 Dim
200 bright
210 bright
```{r}
Names <- c("Peter", "Paul", "Mary",NA)
Ages <- c(100,120,90, 80)
Sex <- c("M","M","F",NA)
my_dataframe <- data.frame(Names,Ages,Sex) # start adding things in the perenthesis
```
when you see a graph you will see lines corresponding to the numbers
you will have to create a table with all those numbers
DV Gender XY
1 B Y
2 A Y
3 A x
can have 100s of numbers
other things I should know
my_dataframe$Names # illustrates a default
my_dataframe$Ages
li
if u didn't want it to be a factor you have to convert it to a diffrent thing
my_dataframe$Names <- as.character(my_dataframe$Names)
```{r,}
library(ggplot2)
a <- c(1,2,3,2,3,4,5,4, 4, 2)
b <- c(4,3,4,3,2,1,2,3, 7, 1)
plot_df <- data.frame(a,b)
ggplot(plot_df, aes(x=a,y=b))+
geom_point() # connect the data to diff parts of the graph
# basic scatterplot
#ggplot(plot_df, aes(x=a,y=b))+
# geom_point()
```
here we are plotting the same graph but addit custom settings
```{r}
ggplot(NULL, aes(x=a,y=b))+
geom_point(size=2)+
geom_smooth(method=lm)+ # this is linear regression line
coord_cartesian(xlim=c(0,7),ylim=c(0,10))+ # if you want to increase a range
xlab("x-axis label")+ # change a name of x axis
ylab("y-axis label")+
ggtitle("I made a scatterplot")+ # add a title
theme_classic(base_size=12)+ # classic background; base size is the size of the font from 12 to whatever
theme(plot.title = element_text(hjust = 0.5))
```
what if u wanted to make a graph but you want it to take only a prortion of the page, like in the corner of the page
base size will make sure your smaller graph has the same font
3 bars graph
```{r}
factor_one <- as.factor(c("A","B","C"))
dv_means <- c(20,30,40)
dv_SEs <- c(4,3.4,4)
plot_df <- data.frame(factor_one,
dv_means,
dv_SEs)
ggplot(plot_df, aes(x=factor_one,y=dv_means))+ # what do you want to go into x and y axis
geom_bar(stat="identity")
```
bar graph with 2 factors
```{r}
factor_one <- rep(as.factor(c("A","B","C")),2) # this 2 means repeat ABC twice
factor_two <- rep(as.factor(c("IIA","IIB")),3)
dv_means <- c(20,30,40,20,40,40)
dv_SEs <- c(4,3.4,4,3,2,4)
plot_df <- data.frame(factor_one,
factor_two,
dv_means,
dv_SEs)
# basic bar graph
ggplot(plot_df, aes(x=factor_one,y=dv_means, # aestetics definition has gotten
group=factor_two,
# color=factor_two))+ # color here means the border collor
fill=factor_two))+
geom_bar(stat="identity",
position="dodge",
color="black")
```
```{r}
ggplot(plot_df, aes(x=factor_one,y=dv_means,
group=factor_two,
color=factor_two,
fill=factor_two))+
geom_bar(stat="identity", position="dodge")+
geom_errorbar(aes(ymin=dv_means-dv_SEs,
ymax=dv_means+dv_SEs),
position=position_dodge(width=0.9),
width=.2,
color="black")+
coord_cartesian(ylim=c(0,100))+
xlab("x-axis label")+
ylab("y-axis label")+
ggtitle("Bar graph 2 factors")+
theme_classic(base_size=12)+
theme(plot.title = element_text(hjust = 0.5))
```
recreating the first figure
we have 8 names (Dora, Rita, Lisa)
# 1st promblem in the 4th assignment - we will have names column - each name will appear twice in each condition
measurement - Mean freaquncy
Names MF Condition
Dara 60 social cond
Dara 60 none social
```{r}
Names <-rep(c("Dara", "Azalea", "Barbi", "Rowena", "Fiona"),each=2)
MF <- rnorm(10,45,25) # 10 is the number of numbers you want, and 45 to 25 is the range
Condition <- rep(c("Social","NonSocial"),5)
Aversity <- rep(c("A","N_A" ),times=c(4,6))
plot_df <- data.frame(Names, MF, Condition, Aversity)
ggplot(plot_df, aes(x=Condition, y=MF, group=Names, linetype=Aversity))+
geom_line()+
geom_text(label=Names)+
theme_classic()
```
# 3/11/2019
```{r}
library(dplyr)
df <- starwars
```
# some of the moves you can manipulate the data without a dplyr package
df$name # dolloar sign gives yoiu the names in the file
df[1,] # row 1
df[1:4,] # rows 1 to 4 and all of the columns
```{r}
df[df$name=="Luke Skywalker",] # find luke's row and give me all of the columns of the skywalker
df[df$height >180,]
```
```{r}
dim(df) #c(number of rows, number of columns)
df <-cbind(df, random_number=runif(dim(df)[1],0,1))
```
df$species <- as.factor(df$species)
levels(df$species)
how many characters have tatooine
```{r, eval=FALSE}
df[df$homeworld=="Tatooine",]
dim(df[df$homeworld=="Tatppome",)[1]
```
# group by certain things
Dataframe %>%
```{r}
library(dplyr)
new_df <- df %>% # adding things on
filter(height > 100) %>%
group_by(homeworld) %>%
summarise(mean_birth_year = mean (birth_year, na.rm = TRUE))
```
```{r}
View (new_df)
new_df <- df %>%
filter(height >120,
height <180,
birth_year >20)
```
```{r}
new_df <- df %>%
filter(gender =="male")
```
```{r}
new_df <- df %>%
group_by(hair_color) %>%
summarise(counts=length(name))
```
```{r,eval=FALSE}
new_df <- df %>%
group_by(hair_color, eye_color) %>%
summarise(mean_years = mean(birth_year,na.rm = TRUE)),
sd_years = sd(birth_year,na.rm = TRUE),
counts = length(names)
```
how many characterers in each movie
how to load data
```{r, eval=FALSE}
hsq <- read.csv(data.table("data/hsq.csv"))
```
# getwd() function will tell you where the working directory is
3/18
# Statistics
## t test
```{r}
x <- c(1,2,3,4,5,2,4,7,4,9)
t.test(x, mu=2)
```
SE of the mean is sd/sqrt of N
T = x-mu / SEM
T dist = find t value of 10 numbers 10000 times - the t scores make up T distribution
```{r}
x <- c(1,2,3,4,5,2,4,7,4,9)
t.test(x, mu=2, alternative = "two.sided")
```
```{r}
x <- c(1,2,3,4,5,2,4,7,4,9)
t.test(x, mu=2, alternative = "greater")
```
Paired T-test
```{r}
x <- c(1,2,3,4,5,2,4,7,4,9)
y <- c(3,2,5,4,3,2,5,6,7,8)
t.test(x, y, paired=TRUE)
```
welch two sample t-test
# simulation of 10000 t-tests
```{r}
a <- replicate(10000,t.test(rnorm(10,0,1),rnorm(10,0,1))$p.value)
hist(a)
```
# getting t values instead of p values
```{r}
a <- replicate(10000,t.test(rnorm(10,0,1),rnorm(10,0,1))$statistic)
hist(a)
```
report_t(t_results)'
in a t-test = testing difference between the means
in an ANOVA = one omnibus tes tells us whether there is a difference
```{r, eval=F}
Factor1 <- rep(c("A","B","C"),each=5)
DV <- rnorm(n=15,mean=0,sd=1)
all_data1 <- data.frame(DV,Factor1)
View(all_data1)
```
# load a function into package without loading a package - knitr:: kable()
# Randomization Test
```{r}
A <- c(1,2,3,4,5,6,7,8,9,10)
B <- c(2,4,6,8,10,12,14,16,18,20)
all <- c(A,B)
mean_difference <- c()
for(i in 1:10000){
shuffle <- sample(all)
newA <- shuffle[1:10] # first ten num put in a new A
newB <- shuffle[11:20] # the next ten nums
mean_difference[i] <- mean(newB)-mean(newA) # sizes of mean diff we could have done if we assigned people randomly
}
observed <- mean(B)-mean(A)
length(mean_difference[mean_difference >= observed])/10000 # how many means fall within
```
```{r}
RandomNumbers <- runif(100, min = 0, max = 100)
hist(RandomNumbers)
```
```{r}
RandomNumbers <- runif(100, min = 0, max = 100)
hist(RandomNumbers)
```
## 3/25/19
```{r}
a<- rnorm(10,0,1)
b<- rnorm(10,0,1)
mean_diff <- mean(a) - mean(b)
mean_diff <- replicate(10000,
mean(rnorm(10,0,1))-mean(rnorm(10,0,1)))
sort(mean_diff)[9500]
```
```{r}
hist(mean_diff)
```
show that the properties of the t-dist are the same as the properties of the analytic dist
```{r}
pt(q=c(.5,1,1.5,2,2.5),df=9)
```
```{r}
pt(q=c(.5,1,1.5,2,2.5),df=9) # looking the sampling distribution of t
t_s <- replicate(10000,t.test(rnorm(10,0,1),mu=0)$statistic)
length(t_s[t_s > .5])
```
```{r}
hist(t_s)
```
```{r}
t_s <- replicate(10000,t.test(rnorm(10,0,1),mu=0)$statistic)
length(t_s[t_s < .5])/10000
```
```{r}
t_s <- replicate(10000,t.test(rnorm(10,0,1),mu=0)$statistic)
length(t_s[t_s < 1])/10000
```
```{r,eval=F}
t_s <- replicate(10000,t.test(rnorm(10,0,1),mu=0)$statistic)
length(t_s[t_s < .5])/10000
sim_ps <- c(length(t_s[t_s <.5])/1000
ral_ps
sim_ps
sum(abs)
```
sampling distribution of correlations
```{r}
cor(rnorm(10,0,1), rnorm(10,0,1))
```
replicate 10000 times
```{r}
hist(replicate(10000,cor(rnorm(10,0,1), rnorm(10,0,1))))
```
converge all r values to absolute values and sort them from smallest 0 to largest
```{r}
sim_rs<-replicate(10000,cor(rnorm(100,0,1),rnorm(100,0,1)))
sort(abs(sim_rs))[9500]
```
F distribution
calculate f value for the main effect of group
```{r}
run_anova <- function(){
A <- rnorm(4,0,1)
B <- rnorm(4,0,1)
C <- rnorm(4,0,1)
conds<- rep(c("A","B","C"),each=4)
DV<-c(A,B,C)
df<-data.frame(conds,DV)
sum_out <-summary(aov(DV~conds,df))
return(sum_out[[1]]$'F value'[1])
}
save_fs <-replicate(10000,run_anova())
hist(save_fs)
sort(save_fs)[9500]
qf(.95,2,9)
```