
#packages
library(Hmisc)#for correlations
library(corrplot)#for correlation plots
library(lme4) #linear regression
library(plyr)#counting
library(reshape2)#melt


#importing data
data<-read.csv("/Users/user/Documents/Surveys/rollsafe_magic.csv",header = TRUE, sep=",") #change the file location

#change strings to numeric equivalent
data[]<-lapply(data,as.character)
data[data=="Not at all"]=0;
data[data=="Slightly"]=1;
data[data=="Somewhat"]=2;
data[data=="Very"]=3;
data[data=="Extremely"]=4;

data$physical_health<-as.character(data$physical_health)
data$physical_health[data$physical_health=="Extremely unhealthy lifestyle"]=-3;
data$physical_health[data$physical_health=="Moderately unhealthy lifestyle"]=-2;
data$physical_health[data$physical_health=="Slightly unhealthy lifestyle"]=-1;
data$physical_health[data$physical_health=="Neutral lifestyle"]=0;
data$physical_health[data$physical_health=="Slightly healthy lifestyle"]=1;
data$physical_health[data$physical_health=="Moderately healthy lifestyle"]=2;
data$physical_health[data$physical_health=="Extremely healthy lifestyle"]=3;
data$physical_health<-as.numeric(data$physical_health)

data$mental_health<-as.character(data$mental_health)
data$mental_health[data$mental_health=="Extremely poor mental health"]=-3;
data$mental_health[data$mental_health=="Moderately poor mental health"]=-2;
data$mental_health[data$mental_health=="Slightly poor mental health"]=-1;
data$mental_health[data$mental_health=="Neutral mental health"]=0;
data$mental_health[data$mental_health=="Slightly good mental health"]=1;
data$mental_health[data$mental_health=="Moderately good mental health"]=2;
data$mental_health[data$mental_health=="Extremely good mental health"]=3;
data$mental_health<-as.numeric(data$mental_health)


#include combined with other 
data$combined_with_other<-as.character(data$combined_with_other)
data$combined_with_other[data$combined_with_other=="0-19"]=-2;
data$combined_with_other[data$combined_with_other=="20-39"]=-1;
data$combined_with_other[data$combined_with_other=="40-59"]=0;
data$combined_with_other[data$combined_with_other=="60-79"]=1;
data$combined_with_other[data$combined_with_other=="80-100"]=2;

#making a table with connection, energy, empathy, love, paranoia, boredom, euphoria, hours
first_experience_no<-apply(data[,3:10], 2, as.numeric)
first_few_experience_no<-apply(data[,12:19], 2, as.numeric)
recent_experience_no<-apply(data[,22:29], 2, as.numeric)#start from 21 if you want the years

#I have defined magic loss as difference between first and last sessions
magic_loss<-as.matrix(first_experience_no) - as.matrix(recent_experience_no);

magic_loss_2<-cbind(magic_loss, data$years_between_first_last, data$no_of_mdma, data$no_of_mdma_per_year, data$need_for_increase, data$mental_health, data$physical_health)
class(magic_loss_2)<- "numeric"
colnames(magic_loss_2) <- c("connection_diff", "energy_diff", "empathy_diff", "love_diff", "paranoia_diff", "boredom_diff", "euphoria_diff", "hours_diff", "years_diff", "no_mdma_trips","mdma_per_year", "need_for_increase", "mental_health", "physical_health")

data<-cbind(data, magic_loss_2)

#Linear regressions
#=======================================================================
#scale and correcting regressors
data$need_for_increase_correct<-as.numeric(data$need_for_increase)-3;
data$no_of_mdma_correct<-scale(as.numeric(data$no_of_mdma), center = TRUE, scale = TRUE)
data$no_of_mdma_per_year_correct<-scale(as.numeric(data$no_of_mdma_per_year), center = TRUE, scale = TRUE)
data$years_between_first_last<-scale(as.numeric(data$years_between_first_last), center = TRUE, scale = TRUE)

connection_model_1<-lm(connection_diff~no_of_mdma_per_year_correct+no_of_mdma_correct+years_between_first_last+combined_with_other, data=data)
summary(connection_model_1)
anova(connection_model_1)

euphoria_model_1<-lm(euphoria_diff~no_of_mdma_per_year_correct+no_of_mdma_correct+years_between_first_last+combined_with_other, data=data)
summary(euphoria_model_1)
anova(euphoria_model_1)

empathy_model_1<-lm(empathy_diff~no_of_mdma_per_year_correct+no_of_mdma_correct+years_between_first_last, data=data)
summary(empathy_model_1)
anova(empathy_model_1)

love_model_1<-lm(love_diff~no_of_mdma_per_year_correct+no_of_mdma_correct+years_between_first_last, data=data)
summary(love_model_1)
anova(love_model_1)

paranoia_model_1<-lm(paranoia_diff~no_of_mdma_per_year_correct+no_of_mdma_correct+years_between_first_last, data=data)
summary(paranoia_model_1)
anova(paranoia_model_1)

boredom_model_1<-lm(boredom_diff~no_of_mdma_per_year_correct+no_of_mdma_correct+years_between_first_last, data=data)
summary(boredom_model_1)
anova(boredom_model_1)

connection_model_2<-lm(connection_diff~no_of_mdma_per_year_correct+no_of_mdma_correct+mental_health+physical_health, data=data)
summary(connection_model_2)
anova(connection_model_2)

euphoria_model_2<-lm(euphoria_diff~no_of_mdma_per_year_correct+no_of_mdma_correct+mental_health+physical_health, data=data)
summary(euphoria_model_2)
anova(euphoria_model_2)

empathy_model_2<-lm(empathy_diff~no_of_mdma_per_year_correct+no_of_mdma_correct+mental_health+physical_health, data=data)
summary(empathy_model_2)
anova(empathy_model_2)

paranoia_model_2<-lm(paranoia_diff~no_of_mdma_per_year_correct+no_of_mdma_correct+mental_health+physical_health, data=data)
summary(paranoia_model_2)
anova(paranoia_model_2)

boredom_model_2<-lm(boredom_diff~no_of_mdma_per_year_correct+no_of_mdma_correct+mental_health+physical_health, data=data)
summary(boredom_model_2)
anova(boredom_model_2)

love_model_2<-lm(love_diff~no_of_mdma_per_year_correct+no_of_mdma_correct+mental_health+physical_health, data=data)
summary(love_model_2)
anova(love_model_2)

loss_model_3<-lm(connection_diff~no_of_mdma_per_year_correct+need_for_increase_correct, data=data)
summary(loss_model_3)

loss_model_4<-lm(connection_diff~no_of_mdma_per_year_correct+physical_health+physically_active, data=data)
summary(loss_model_4)