**Double tap sul link e clicca su “open in a new tab”
library(ggplot2)
library(tidyverse)
library(gridExtra)
library(ggplot2)
library(tidyverse)
library(gridExtra)
#upload files
files <- list.files(path = "./Touch/", full.names = TRUE, pattern = paste(".*", ".txt", sep = ""))
dataT <- do.call(rbind, lapply(files, function(x) read.table(x,header = TRUE)))
raw <-dim(dataT)[1]
# Distance contact points
euclideanDist <- function(x1, y1, x2, y2){sqrt((x2 - x1)^2 + (y2 - y1)^2)}
dataT$fga <-sqrt((dataT$ThumbXinMM-dataT$IndexXinMM)^2 + (dataT$ThumbYinMM -dataT$IndexYinMM)^2)
#Data Cleaning
dat1 <- dataT %>%
filter(ThumbX != -1, RTs <= 2, RTs > .2)
filtered<- dim(dat1)[1]
#Removed trial
1 - (filtered/raw)
## [1] 0.06875
#Variables of interest
W = dat1%>% group_by(Distance, ExpCondition) %>%
summarise(
fga = sd(fga))
#Fga ~ Distance
ggplot(W, aes(Distance, fga, colour = ExpCondition )) +
geom_point() +
theme_minimal() +
labs(title = "Fga ~ Distance",
caption = "Pare che la WL sia presente in entrambe le condizioni. ",
x = "Distance",
y = "SD Final Grip Aperture") +
geom_smooth(method = "lm", se = FALSE)+
theme_minimal()
W2 = dat1%>% group_by(Distance, ExpCondition,TargetIndexYInMM ) %>%
summarise(
fga = sd(fga))
W2$TargetIndexYInMM <- round(W2$TargetIndexYInMM)
#check1
# What is the max hand velocity as a function of size?
ggplot(W2, aes(Distance, fga, group = ExpCondition)) +
geom_line() +
geom_point() +
facet_wrap(~TargetIndexYInMM, ncol = 5) +
theme_minimal()
#CINEMATICA
# Import data -------------------------------------------------------------
files <- list.files(path = "./DataCleaned/", full.names = TRUE, pattern = paste(".*", ".csv", sep = "")) # get only files with .csv extension
dataAll <- do.call(rbind, lapply(files, function(x) read_csv(x)))
dataAll1 <-dataAll
dataAll1$PointXpos <- rowMeans(cbind(dataAll1$ThumbXpos, dataAll1$IndexXpos))
dataAll1$PointYpos <- rowMeans(cbind(dataAll1$ThumbYpos, dataAll1$IndexYpos))
dataAll1$PointZpos <- rowMeans(cbind(dataAll1$ThumbZpos, dataAll1$IndexZpos))
dataAll1$Distance <- round(dataAll$Distance)
####### Define functions####### -------------------------------------------------------
# Euclidean distance
euclideanDist <- function(x1, y1, z1, x2, y2, z2){sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)}
# Function to calculate traveled distance between frames
getDist <- function(x, y, z){
dist <- rep(as.numeric(NA), length(x)-1)
for (i in seq(length(x)-1)){
dist[i] <- euclideanDist(x[i], y[i], z[i], x[i+1], y[i+1], z[i+1])
}
dist <- c(0, dist)
dist
}
dd <- dataAll1 %>%
group_by(SubjectID, NTrial,ExpCondition) %>%
mutate(
HandPath = getDist(PointXpos, PointYpos, PointZpos)) %>%
summarise(
Distance = head(Distance, 1),
PGA = max(IndexThumbSpan), # Max grip aperture
PGAvel = max(IndexThumbSpanVel), # Grip aperture velocit
FGA = tail(IndexThumbSpan, 1), # Final grip aperture
tPGA = (TimeFromMovOnset[IndexThumbSpan == max(IndexThumbSpan)]) , # Time of peak grip aperture
)
# Summaries of data for visual exploration -------------------------------------
ddSummary <- dd %>%
group_by(SubjectID, Distance, ExpCondition) %>%
summarise(
sdPGA = sd(PGA),
sdFGA = sd(FGA))
# What is the standard deviation of the peak grip aperture as a function of size?
ggplot(ddSummary, aes(Distance, sdPGA, group = SubjectID, colour = SubjectID)) +
geom_smooth(method = "lm", se = FALSE)+
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# What is the standard deviation of the final grip aperture as a function of size?
ggplot(ddSummary, aes(Distance, sdFGA, group = SubjectID,colour = SubjectID)) +
geom_smooth(method = "lm", se = FALSE)+
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
#------------------------CINEMATICA-------------------------------------------------------
# Import data -------------------------------------------------------------
files <- list.files(path = "./DataCleaned/", full.names = TRUE, pattern = paste(".*", ".csv", sep = "")) # get only files with .csv extension
dataAll <- do.call(rbind, lapply(files, function(x) read_csv(x)))
# Rename columns ----------------------------------------------------
dataAll1 <- dataAll
# Calculate position of midpoint between thumb and index ------------------
dataAll1$PointXpos <- rowMeans(cbind(dataAll1$ThumbXpos, dataAll1$IndexXpos))
dataAll1$PointYpos <- rowMeans(cbind(dataAll1$ThumbYpos, dataAll1$IndexYpos))
dataAll1$PointZpos <- rowMeans(cbind(dataAll1$ThumbZpos, dataAll1$IndexZpos))
dataAll1$Distance <- round(dataAll$Distance)
# First glance at data ----------------------------------------------------
######## Define functions####### --------------------------------------------------------
# Euclidean distance
euclideanDist <- function(x1, y1, z1, x2, y2, z2){sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)}
# Function to calculate traveled distance between frames
getDist <- function(x, y, z){
dist <- rep(as.numeric(NA), length(x)-1)
for (i in seq(length(x)-1)){
dist[i] <- euclideanDist(x[i], y[i], z[i], x[i+1], y[i+1], z[i+1])
}
dist <- c(0, dist)
dist
}
# Variables of interest ---------------------------------------------------
dd <- dataAll1 %>%
group_by(SubjectID, NTrial,ExpCondition) %>%
mutate(
HandPath = getDist(PointXpos, PointYpos, PointZpos)) %>% # here we calculate the distance traveled by the fingers at each frame
summarise(
Distance = head(Distance, 1),
PGA = max(IndexThumbSpan), # Max grip aperture
PGAvel = max(IndexThumbSpanVel), # Grip aperture velocity
MaxWristVel = max(WristVel), # Max hand velocity
FGA = tail(IndexThumbSpan, 1), # Final grip aperture
MovementTime = tail(TimeFromMovOnset, 1), # Movement time
MovementLength = sum(HandPath), # Length of the movement along the trajectory
ThumbXEnd = tail(ThumbXpos, 1), # Position of thumb at contact
ThumbYEnd = tail(ThumbYpos, 1),
ThumbZEnd = tail(ThumbZpos, 1),
IndexXEnd = tail(IndexXpos, 1), # Position of index at contact
IndexYEnd = tail(IndexYpos, 1),
IndexZEnd = tail(IndexZpos, 1),
WristXEnd = tail(WristXpos, 1), # Position of hand marker at contact
WristYEnd = tail(WristYpos, 1),
WristZEnd = tail(WristZpos, 1),
tPGA = (TimeFromMovOnset[IndexThumbSpan == max(IndexThumbSpan)]) , # Time of peak grip aperture
tMaxThumbVel = (TimeFromMovOnset[ThumbVel == max(ThumbVel)]), # Time of peak thumb velocity
tMaxIndexVel = (TimeFromMovOnset[IndexVel== max(IndexVel)]), # Time of peak Index velocity
tMaxWristVel = (TimeFromMovOnset[WristVel== max(WristVel)]), # Time of peak hand velocity
prop_tPGA = tPGA/MovementTime, # Proportion Time of peak grip aperture
prop_tMaxThumbVel = tMaxThumbVel/MovementTime, # Proportion Time of peak thumb velocity
prop_tMaxIndexVel = tMaxIndexVel/MovementTime, # Proportion Time of peak Index velocity
prop_tMaxWristVel = tMaxWristVel/MovementTime, # Proportion Time of peak hand velocity
prop_PGA = (sum(HandPath[1:which.max(IndexThumbSpan)])/MovementLength), # Proportion of trajectory at which MGA occurred
prop_MaxWristVel = (sum(HandPath[1:which.max(WristVel)])/MovementLength) # Proportion of trajectory at which maximum hand velocity occurred
)
# Summaries of Kinematics data for visual exploration --------------------------------
ddSummary <- dd %>%
group_by(SubjectID, Distance, ExpCondition) %>%
summarise(
mPGA = mean(PGA),
mPGAvel = mean(PGAvel),
mMaxWristVel = mean(MaxWristVel),
mFGA = mean(FGA),
mMovementTime = mean(MovementTime),
mMovementLength = mean(MovementLength),
mThumbXEnd = mean(ThumbXEnd),
mThumbYEnd = mean(ThumbYEnd),
mThumbZEnd = mean(ThumbZEnd),
mIndexXEnd = mean(IndexXEnd),
mIndexYEnd = mean(IndexYEnd),
mIndexZEnd = mean(IndexZEnd),
mtPGA = mean(tPGA),
mtMaxThumbVel = mean(tMaxThumbVel),
mtMaxIndexVel = mean(tMaxIndexVel),
mtMaxWristVel = mean(tMaxWristVel),
mprop_tPGA = mean(prop_tPGA),
mprop_tMaxThumbVel = mean(prop_tMaxThumbVel),
mprop_tMaxIndexVel = mean(prop_tMaxIndexVel),
mprop_tMaxWristVel = mean(prop_tMaxWristVel),
mprop_PGA = mean(prop_PGA),
mprop_MaxWristVel = mean(prop_MaxWristVel),
sdPGA = sd(PGA),
sdFGA = sd(FGA),
sdThumbXEnd = sd(ThumbXEnd),
sdThumbYEnd = sd(ThumbYEnd),
sdThumbZEnd = sd(ThumbZEnd),
sdIndexXEnd = sd(IndexXEnd),
sdIndexYEnd = sd(IndexYEnd),
sdIndexZEnd = sd(IndexZEnd),
sdThumbCentroid = sqrt(sum((ThumbXEnd - mean(ThumbXEnd))^2 + (ThumbYEnd - mean(ThumbYEnd))^2)/(n())), # standard deviation from centroid
sdIndexCentroid = sqrt(sum((IndexXEnd - mean(IndexXEnd))^2 + (IndexYEnd - mean(IndexYEnd))^2)/(n())), # standard deviation from centroid
corThumbIndexXEnd = cor(ThumbXEnd, IndexXEnd),
corThumbIndexYEnd = cor(ThumbYEnd, IndexYEnd),
pcorThumbIndexWristYEnd = ppcor::pcor(data.frame(ThumbYEnd, IndexYEnd, WristYEnd), method = "pearson")$estimate[2, 1], # partial correlation
pcorThumbIndexWristXEnd = ppcor::pcor(data.frame(ThumbXEnd, IndexXEnd, WristXEnd), method = "pearson")$estimate[2, 1] # partial correlatio
)
# What is the peak grip aperture as a function of size?
ggplot(ddSummary, aes(Distance, mPGA, group = SubjectID)) +
geom_jitter(data = dd, aes(Distance, PGA), width = 1, colour = "gray70") +
geom_abline(aes(intercept = 0, slope = 1), linetype = "dotted") +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
ggplot(ddSummary, aes(Distance, mPGAvel, group = SubjectID)) +
geom_jitter(data = dd, aes(Distance, PGAvel), width = 1, colour = "gray70") +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# What is the final grip aperture as a function of size?
ggplot(ddSummary, aes(Distance, mFGA, group = SubjectID)) +
geom_jitter(data = dd, aes(Distance, FGA), width = 1, colour = "gray70") +
geom_abline(aes(intercept = 0, slope = 1), linetype = "dotted") +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# Relation between peak and final grip apertures
ggplot(ddSummary, aes(mPGA, mFGA, group = SubjectID)) +
geom_abline(aes(intercept = 0, slope = 1), linetype = "dotted") +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# What is the max hand velocity as a function of size?
ggplot(ddSummary, aes(Distance, mMaxWristVel, group = SubjectID)) +
geom_jitter(data = dd, aes(Distance, MaxWristVel), width = 1, colour = "gray70") +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# # What is the movement time as a function of size?
ggplot(ddSummary, aes(Distance, mMovementTime, group = SubjectID)) +
geom_jitter(data = dd, aes(Distance, MovementTime), width = 1, colour = "gray70") +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# What is the movement length as a function of size?
ggplot(ddSummary, aes(Distance, mMovementLength, group = SubjectID)) +
geom_jitter(data = dd, aes(Distance, MovementLength), width = 1, colour = "gray70") +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# What is the time of peak grip aperture as a function of size?
ggplot(ddSummary, aes(Distance, mtPGA, group = SubjectID)) +
geom_jitter(data = dd, aes(Distance, tPGA), width = 1, colour = "gray70") +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# At what proportion of the movement did the peak grip aperture occur as a function of size?
ggplot(ddSummary, aes(Distance, mprop_PGA, group = SubjectID)) +
geom_jitter(data = dd, aes(Distance, prop_PGA), width = 1, colour = "gray70") +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# What is the standard deviation of the peak grip aperture as a function of size?
ggplot(ddSummary, aes(Distance, sdPGA, group = SubjectID)) +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# What is the standard deviation of the final grip aperture as a function of size?
ggplot(ddSummary, aes(Distance, sdFGA, group = SubjectID)) +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# What is the standard deviation of the thumb endpoint in y as a function of size?
ggplot(ddSummary, aes(Distance, sdThumbYEnd, group = SubjectID)) +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# What is the standard deviation of the index endpoint in y as a function of size?
ggplot(ddSummary, aes(Distance, sdIndexYEnd, group = SubjectID)) +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# Are standard deviations in x and y similar for thumb?
ggplot(ddSummary) +
geom_line(aes(Distance, sdThumbXEnd, group = SubjectID), colour = "red") +
geom_line(aes(Distance, sdThumbYEnd, group = SubjectID), colour = "orange") +
scale_y_continuous(name = "SD fingers") +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# Are standard deviations in x and y similar for index?
ggplot(ddSummary) +
geom_line(aes(Distance, sdIndexXEnd, group = SubjectID), colour = "red") +
geom_line(aes(Distance, sdIndexYEnd, group = SubjectID), colour = "orange") +
scale_y_continuous(name = "SD fingers") +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# Are standard deviations in x similar for thumb and index?
ggplot(ddSummary) +
geom_line(aes(Distance, sdThumbXEnd, group = SubjectID), colour = "red") +
geom_line(aes(Distance, sdIndexXEnd, group = SubjectID), colour = "orange") +
scale_y_continuous(name = "SD fingers") +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# Are standard deviations in y similar for thumb and index?
ggplot(ddSummary) +
geom_line(aes(Distance, sdThumbYEnd, group = SubjectID), colour = "red") +
geom_line(aes(Distance, sdIndexYEnd, group = SubjectID), colour = "orange") +
scale_y_continuous(name = "SD fingers") +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# What is the standard deviation of the thumb endpoint from centroid as a function of size?
ggplot(ddSummary, aes(Distance, sdThumbCentroid, group = SubjectID)) +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# What is the standard deviation of the index endpoint from centroid as a function of size?
ggplot(ddSummary, aes(Distance, sdIndexCentroid, group = SubjectID)) +
geom_line() +
geom_point() +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# What is the correlation between final endpoints of thumb and index in x direction?
ggplot(ddSummary, aes(Distance, corThumbIndexXEnd, group = SubjectID)) +
geom_hline(yintercept = 0, linetype = "dotted") +
geom_line() +
geom_point() +
scale_y_continuous(limits = c(.90, 1)) +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
# What is the correlation between final endpoints of thumb and index in y direction?
ggplot(ddSummary, aes(Distance, corThumbIndexYEnd, group = SubjectID)) +
geom_hline(yintercept = 0, linetype = "dotted") +
geom_line() +
geom_point() +
scale_y_continuous(limits = c(-1, 1)) +
facet_wrap(~ExpCondition, ncol = 5) +
theme_minimal()
#TOUCHSCREEN
files <- list.files(path = "./Touch/", full.names = TRUE, pattern = paste(".*", ".txt", sep = ""))
dataT <- do.call(rbind, lapply(files, function(x) read.table(x,header = TRUE)))
raw <-dim(dataT)[1]
euclideanDist <- function(x1, y1, x2, y2){sqrt((x2 - x1)^2 + (y2 - y1)^2)}
dataT$fga <-sqrt((dataT$ThumbXinMM-dataT$IndexXinMM)^2 + (dataT$ThumbYinMM -dataT$IndexYinMM)^2)
dataAll4 <- dataT %>%
filter(ThumbX != -1, RTs <= 2, RTs > .2)
filtered<- dim(dat1)[1]
#Trials eliminated:
length(dataT$Distance) - length(dataAll4$Distance) # number
## [1] 528
(length(dataT$Distance) - length(dataAll4$Distance))/length(dataT$Distance)*100 # percentage
## [1] 6.875
# Calculate position of midpoint between thumb and index ------------------
dataAll4$MidpointX <- rowMeans(cbind(dataAll4$ThumbXinMM, dataAll4$IndexXinMM))
dataAll4$MidpointY <- rowMeans(cbind(dataAll4$ThumbYinMM, dataAll4$IndexYinMM))
#First touch
dataAll4$ThumbIndexTimeDiff<-dataAll4$IndexTime - dataAll4$ThumbTime
# FGA
ggplot(dataAll4, aes(Distance, fga, group = ExpCondition)) +
geom_abline(intercept = 0, slope = 1, linetype = "dotted") +
geom_jitter(width = 2) +
geom_smooth(method = "lm", se = FALSE) +
facet_grid(~ExpCondition)
# Thumb with respect to target
ggplot(dataAll4, aes(TargetThumbXInMM - ThumbXinMM, TargetThumbYInMM - ThumbYinMM)) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) +
geom_point() +
coord_fixed() +
facet_grid(SubjectID~ExpCondition)
# Midpoint between thumb and index
ggplot(dataAll4, aes(MidpointX, MidpointY)) +
geom_point() +
facet_grid(SubjectID~ExpCondition)
# Time
ggplot(dataAll4, aes(Distance, RTs)) +
geom_jitter(width = 2) +
geom_smooth(method = "lm", se = FALSE) +
facet_grid(~ExpCondition)
# Which finger first?
ggplot(dataAll4, aes(ThumbIndexTimeDiff)) +
geom_vline(xintercept = 0, linetype = "dotted") +
geom_histogram() +
facet_grid(SubjectID~ExpCondition)