forked from hcp4715/R4Psy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.Rmd
More file actions
183 lines (164 loc) · 5.72 KB
/
practice.Rmd
File metadata and controls
183 lines (164 loc) · 5.72 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
---
title: "Practice"
author: "Fangru"
date: "2025-04-16"
output:xaringan::moon_reader:
css: [default, css/Custumed_Style.css,css/zh-CN.css]
lib_dir: libs
nature:
highlightLines: true
highlightStyle: github
countIncrementalSlides: false
seal: true
ratio: 16:9
params:
output_dir: "../output"
---
# <h1 lang="en">7.2 函数</h1>
<span style="font-size: 30px;">综合练习</span></center><br>
- 定义一个函数用于计算 $d'$
- 计算不同Shape情况下(immoralself,moralself,immoralother,moralother)<br> 基于信号检测论(match为信号,mismatch噪音)的 $d'$ 值
<br>
- 1 导入所需要镜像和R包
```{r 7.1 R package}
# 可以将清华的镜像设置为下载的镜像
# options(repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
# 导入所需R包
library(tidyverse)
```
---
- 2 读取所需要录入的数据
```{r 7.2 load data}
# 此前已经将合并后的实验数据存入"match_raw.csv",此次可直接读取
df7 <- bruceR::import(here::here("data", "match","match_raw.csv"))
# 也可以用原始方式导入
# penguin_data <- read.csv('./data/match/match_raw.csv') # 仅用基础包的做法
```
- 3 选择我们需要的变量
```{r 7.3 select variables,message=FALSE}
# 选择我们需要的变量
df7 <- dplyr::select(df7,
Sub, Age, Sex, Hand, #人口统计学
Block, Bin, Trial, # 试次
Shape, Label, Match, # 刺激
Resp, ACC, RT) # 反应结果
```
---
- 4 分组并计算 $d'$ 值
```{r 7.4 count Dprime}
#按照
df7 <- dplyr::group_by(df7, Sub, Shape,)
df7
df7 <- dplyr::summarise(df7,
hit = length(ACC[Match == "match" & ACC == 1]),
fa = length(ACC[Match == "mismatch" & ACC == 0]),
miss = length(ACC[Match == "match" & ACC == 0]),
cr = length(ACC[Match == "mismatch" & ACC == 1]),
Dprime = qnorm(
ifelse(hit / (hit + miss) < 1,
hit / (hit + miss),
1 - 1 / (2 * (hit + miss))
)
)
- qnorm(
ifelse(fa / (fa + cr) > 0,
fa / (fa + cr),
1 / (2 * (fa + cr))
)
)
)
df7 <- dplyr::ungroup(df7)
print(df7) #可以逐步打印结果以便检查
```
---
- 5 删除额外变量
```{r 7.5 delete variables}
df7 <- df7[, -which(names(df7) == "hit"|names(df7) == "fa"|names(df7) == "miss"|names(df7) == "cr")]
print(df7)
```
---
- 6 长转宽,将形4个形状为列标题,以显示不同被试在不同形状条件下的$d'$ 值
```{r 7.6}
df7 <- tidyr::pivot_wider(df7, names_from = "Shape", values_from = "Dprime")
print(df7)
```
---
- 7 合并Tidyverse操作
```{r example of total rawdata_matchtask, message=FALSE}
# 用管道操作符合并以上代码
df7_1 <- bruceR::import(here::here("data", "match","match_raw.csv")) %>%
dplyr::select(Sub, Age, Sex, Hand, #人口统计学
Block, Bin, Trial, # 试次
Shape, Label, Match, # 刺激
Resp, ACC, RT) %>% # 反应结果
tidyr::drop_na() %>% #删除缺失值
dplyr::group_by(Sub, Shape,) %>%
dplyr::summarise(
hit = length(ACC[Match == "match" & ACC == 1]),
fa = length(ACC[Match == "mismatch" & ACC == 0]),
miss = length(ACC[Match == "match" & ACC == 0]),
cr = length(ACC[Match == "mismatch" & ACC == 1]),
Dprime = qnorm(
ifelse(hit / (hit + miss) < 1,
hit / (hit + miss),
1 - 1 / (2 * (hit + miss))
)
)
- qnorm(
ifelse(fa / (fa + cr) > 0,
fa / (fa + cr),
1 / (2 * (fa + cr))
)
)
) %>%
dplyr::ungroup() %>%
dplyr::mutate(hit = NULL, fa = NULL, miss = NULL, cr = NULL) %>%
tidyr::pivot_wider(names_from = "Shape", values_from = "Dprime")
```
---
layout: false
#练习
1. 读取match数据,对不同shape的击中率进行分组绘图,可使用boxplot观察差异。
- (1) 按照7的过程,处理好数据,使shape为一列,hit为一列
```{r 8.1}
# 用管道操作符合并以上代码
df8_1 <- bruceR::import(here::here("data", "match","match_raw.csv")) %>%
dplyr::select(Sub, Age, Sex, Hand, #人口统计学
Block, Bin, Trial, # 试次
Shape, Label, Match, # 刺激
Resp, ACC, RT) %>% # 反应结果
tidyr::drop_na() %>% #删除缺失值
dplyr::group_by(Sub, Shape,) %>%
dplyr::summarise(hit = length(ACC[Match == "match" & ACC == 1])) %>%
dplyr::ungroup() %>%
print(df8_1)
```
---
- (2) 作为x和y绘制箱型图
```{r 8.2 pic_box}
pic_box_1 <- df8_1 %>%
ggplot(aes(x = Shape, y = hit)) +
geom_boxplot(staplewidth = 1) +
# 绘制箱线图并添加上下边缘线
theme_classic()
```
---
2. 在上一题中,如何按照特定的顺序来展现 boxplot,比如按照moralSelf - immoralSelf - moralOther - immoralOther(提示:设置因子)
```{r 8.2 pic_box}
df8_2 <- bruceR::import(here::here("data", "match", "match_raw.csv")) %>%
dplyr::select(Sub, Age, Sex, Hand,
Block, Bin, Trial,
Shape, Label, Match,
Resp, ACC, RT) %>%
tidyr::drop_na() %>%
dplyr::group_by(Sub, Shape) %>%
dplyr::summarise(hit = length(ACC[Match == "match" & ACC == 1])) %>%
dplyr::ungroup() %>%
dplyr::mutate(Shape = factor(Shape,
levels = c("moralSelf", "immoralSelf", "moralOther", "immoralOther")))
#利用dplyr中的mutate函数,将Shape转化为因子,并利用levels来进行自定义排序
pic_box_2 <- df8_2 %>%
ggplot(aes(x = Shape, y = hit)) +
geom_boxplot(staplewidth = 1) +
theme_classic()
```