-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructure_vs_scent_choice.R
122 lines (85 loc) · 3.4 KB
/
structure_vs_scent_choice.R
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
###
# Project: Seagrass as habitat for juvenile lobsters
# Data: Structure vs scent trials
# Task: Data wrangling and modelling for first/final choice data
# author: Kaitlin McCloghry, Kingsley Griffin
# date: April-July 2022
library(tidyverse)
library(reshape2)
library(ggplot2)
library(lme4)
library(dplyr)
library(tidyr)
# read in data
structure_vs_scent <- read.csv("data/Structure_vs_scent_20.07.2022.csv")
#view data
head(structure_vs_scent)
str(structure_vs_scent)
summary(structure_vs_scent)
# tidy up the data
struct_vs_scent_clean <- filter(structure_vs_scent, Useable.data == 1)
head(struct_vs_scent_clean)
#FIRST CHOICE
#count first choice by level
structure_scent_first <- sum(struct_vs_scent_clean$first.choice == 'st/sc')
structure_first <- sum(struct_vs_scent_clean$first.choice == 'structure')
scent_first <- sum(struct_vs_scent_clean$first.choice == 'scent')
blank_first <- sum(struct_vs_scent_clean$first.choice == 'blank')
#create matrix with table info
matrix_first <- matrix(c(structure_scent_first, structure_first, scent_first, blank_first), ncol=2, byrow=TRUE)
#define column names and row names of matrix
colnames(matrix_first) <- c('scent', 'no scent')
rownames(matrix_first) <- c('structure', 'no structure')
#convert matrix to table
table_first <- as.table(matrix_first)
#view table
table_first
#table as proportions
prop_table_first <- prop.table(table_first)
prop_table_first
#Chi-squared test
chisq_first <- chisq.test(table_first)
chisq_first
#open file path to save plot
png(filename="C:/Users/kaitl/Documents/Kaitlin's Stuff/Dissertation/analysis-kaitlin-lobster/plots/structure_vs_scent_firstchoice.png")
#create barplot
barplot(prop_table_first, beside = TRUE,
xlab = 'presence of scent', ylab = 'proportion of final choice',
legend.text = TRUE, args.legend =
list(x = "topright",inset = c(0, 0), cex = 0.75))
dev.off()
#FINAL CHOICE
#replace NAs with 'neither' to not mess with next bit
struct_vs_scent_replaced <- struct_vs_scent_clean %>% replace_na(list(final.choice = 'neither'))
#count final choice by level
structure_scent_final <- sum(struct_vs_scent_replaced$final.choice == 'st/sc')
structure_final <- sum(struct_vs_scent_replaced$final.choice == 'structure')
scent_final <- sum(struct_vs_scent_replaced$final.choice == 'scent')
blank_final <- sum(struct_vs_scent_replaced$final.choice == 'blank')
structure_scent_final
structure_final
scent_final
blank_final
#create matrix with table info
matrix_final <- matrix(c(structure_scent_final, structure_final, scent_final, blank_final), ncol=2, byrow=TRUE)
#define column names and row names of matrix
colnames(matrix_final) <- c('scent', 'no scent')
rownames(matrix_final) <- c('structure', 'no structure')
#convert matrix to table
table_final <- as.table(matrix_final)
#view table
table_final
#table as proportions
prop_table_final <- prop.table(table_final)
prop_table_final
#Chi-squared test
chisq_final <- chisq.test(table_final)
chisq_final
#open file path to save plot
png(filename="C:/Users/kaitl/Documents/Kaitlin's Stuff/Dissertation/analysis-kaitlin-lobster/plots/structure_vs_scent_finalchoice.png")
#create barplot
barplot(prop_table_final, beside = TRUE,
xlab = 'presence of scent', ylab = 'proportion of final choice',
legend.text = TRUE, args.legend =
list(x = "topright",inset = c(0, 0), cex = 0.75))
dev.off()