##Load data

dat1 <- read.csv("/Users/fc2284/Desktop/bip/dati.csv")

raw <-dim(dat1)[1]


# Distance contact points
euclideanDist <- function(x1, y1, x2, y2){sqrt((x2 - x1)^2 + (y2 - y1)^2)}
dat1$fga <-sqrt((dat1$ThumbXinMM-dat1$IndexXinMM)^2 + (dat1$ThumbYinMM -dat1$IndexYinMM)^2) 

#Blocks
dat1$blocks <- rep(1:4,each= 200, times = 2)

#Data Cleaning
dat1 <- dat1 %>% 
  filter(ThumbX != -1, Time <= 1.5, Time > .2,fga > 15, fga < 120)

filtered<- dim(dat1)[1]

#Removed trial
1 - (filtered/raw)
## [1] 0.051875
#------------------------------------------------------------------------------
## Weber Law
#-----------------------------------------------------------------------------

W = dat1%>% group_by(Distance, Condition, blocks,  subj) %>%
  summarise(  fga = sd(fga),
              ThumbXinMM = sd(ThumbXinMM, na.rm = TRUE),
              ThumbYinMM = sd(ThumbYinMM),
              IndexXinMM  = sd(IndexXinMM),
              IndexYinMM =  sd(IndexYinMM),
              
              Time = sd (Time),
              errT = sum(at)/n(),
              errI = sum(ai)/n()
  )


#Fga ~ Distance
ggplot(W, aes(Distance, fga, colour = Condition )) +
  geom_point() +
  theme_minimal() +
  geom_smooth(method = "lm", se = FALSE) +
  facet_grid(subj~blocks)

#ThumbXinMM ~ Distance
ggplot(W, aes(Distance, ThumbXinMM, colour = Condition )) +
  geom_point() +
  theme_minimal() +
  geom_smooth(method = "lm", se = FALSE) +
  facet_grid( subj~blocks)

#IndexXinMM ~ Distance
ggplot(W, aes(Distance, IndexXinMM, colour = Condition )) +
  geom_point() +
  theme_minimal() +
  geom_smooth(method = "lm",se = FALSE) +
  facet_grid( subj~blocks)

 #IndexYinMM ~ Distance
ggplot(W, aes(Distance, IndexYinMM, colour = Condition )) +
  geom_point() +
  theme_minimal() +
  geom_smooth(method = "lm",se = FALSE) +
  facet_grid( subj~blocks)

#Time ~ Distance
ggplot(W, aes(Distance, Time, colour = Condition )) +
  geom_point() +
  theme_minimal() +
  geom_smooth(method = "lm",se = FALSE) +
  facet_grid( subj~blocks)

#errI ~ Distance
ggplot(W, aes(Distance, errI, colour = Condition )) +
  geom_point() +
  theme_minimal() +
  facet_grid( subj~blocks)

#errT ~ Distance
ggplot(W, aes(Distance, errT, colour = Condition)) +
  geom_point() +
  theme_minimal() +
  facet_grid( subj~blocks)

#------------------------------------------------------------------------------
##Detrending Time~Distance
#-----------------------------------------------------------------------------

bb <- dat1%>%
  group_by(Distance, Condition, blocks, subj) %>%
  do(
    residFGA = residuals(lm(fga~Time+Distance, data = .))
  ) %>%
  unnest(cols = c(residFGA)) %>%
  group_by(Distance, Condition, blocks, subj) %>%
  summarise(
    
    sdFGAdetrend = sd(residFGA)
    
  )

ggplot(bb, aes(Distance, sdFGAdetrend, colour = Condition)) +
  geom_point() +
    theme_minimal() +
  geom_smooth(method = "lm", se = FALSE) +
  facet_grid(subj~blocks)

#------------------------------------------------------------------------------
## Thumb-Index Correlation
#-----------------------------------------------------------------------------

W2 = dat1%>% group_by(Distance, Condition, blocks, subj) %>%
  summarise(CorTIx = cor(IndexXinMM, ThumbXinMM),
            CorTIy = cor(IndexYinMM, ThumbYinMM)
  )

ggplot(data=W2, aes(y=CorTIx, x=Distance)) +
  geom_line(linetype = "dashed")+
  geom_point()+
  theme_minimal() +
  facet_grid( subj~blocks)

ggplot(data=W2, aes(y=CorTIy, x=Distance)) +
  geom_line(linetype = "dashed")+
  geom_point()+
  theme_minimal() +
  facet_grid( subj~blocks)

#------------------------------------------------------------------------------
## Qualche analisi sugli errori
#-----------------------------------------------------------------------------

#1: Il soggetto sbaglia sia con pollice che con indice (327 trial)
dat2<- subset(dat1, at == 0 | ai == 0)
dim(dat2)
## [1] 327  31
W = dat2%>% group_by(Distance,subj) %>%
  summarise(  fgaSD = sd(fga)
  )


ggplot(W, aes(Distance, fgaSD)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  facet_grid(~subj)

#2: Il soggetto sbaglia solo con indice (268)
dat2<- subset(dat1, at == 0 & ai == 1)
dim(dat2)
## [1] 268  31
W = dat2%>% group_by(Distance,subj) %>%
  summarise(  fgaSD = sd(fga)
  )


ggplot(W, aes(Distance, fgaSD)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  facet_grid(~subj)

#3: Il soggetto sbaglia solo con pollice (268)
dat2<- subset(dat1, at == 1 & ai == 0)
dim(dat2)
## [1] 26 31
W = dat2%>% group_by(Distance,subj) %>%
  summarise(  fgaSD = sd(fga)
  )


ggplot(W, aes(Distance, fgaSD)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  facet_grid(~subj)
## Warning: Removed 1 rows containing non-finite values (stat_smooth).
## Warning: Removed 1 rows containing missing values (geom_point).