# http://cran.r-project.org/ AND DOWNLOAD THE R LATEST RELEASE: R-2.x.y.
# http://www.staff.ul.ie/mackenzieg/Assess/R_Integration_Tools/r_integration_tools.html (Crash Course in R)

library(foreign)
library(car)
library(lattice)
#
# READ IN SPSS SPREADSHEET AND SCATTERPLOT USING A 'o' SHAPE WORKS WITH R 2.7 (HAVEN'T TESTED IT YET WITH R 2.5?)
#
x <- read.spss("suicides.sav")
x1 <- data.frame(x)
x4 <- na.omit(x1)

plot(x1$income ~ x1$prestige, data=x1, xlab='Prestige', ylab='Income', main='Years in education', xlim=c(0,100), ylim=c(1000,10000), pch="o")

# MULTIPLE SCATTERPLOTS (CAN'T DO IN SPSS)

pairs(x1)
pairs(cbind(x1$prestige,x1$income,x1$educatio))

# SELECTING A SUBSET OF VARIABLES FOR MULTIPLE SCATTERPLOTS

subdat <- subset(x1,select=c(prestige,income, educatio))
pairs(subdat)

# BUBBLE PLOTS

plot(x1$income ~ x1$prestige, data=x1, xlab='Prestige', ylab='Income', main='Years in education', xlim=c(0,100), ylim=c(1000,10000), pch=".")
symbols(x1$prestige, x1$income,  circles=x1$educatio, inches=0.2, add=TRUE)

gen <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)

plot(x1$income ~ x1$prestige, data=x1, xlab='Prestige', ylab='Income', main='Years in education', xlim=c(0,100), ylim=c(1000,10000), pch=gen+1)
symbols(x1$prestige, x1$income,  circles=x1$educatio, inches=0.2, add=TRUE)

plot(x1$income ~ x1$prestige, data=x1, xlab='Prestige', ylab='Income', main='Years in education', xlim=c(0,100), ylim=c(1000,10000),pch=".")
symbols(x1$prestige[gen==1], x1$income[gen==1],  circles=x1$educatio[gen==1], inches=0.2, fg=1, add=TRUE)
symbols(x1$prestige[gen==2], x1$income[gen==2],  squares=x1$educatio[gen==2], inches=0.2, fg=2, add=TRUE)

plot(x1$income ~ x1$prestige, data=x1, xlab='Prestige', ylab='Income', main='Years in education', xlim=c(0,100), ylim=c(1000,10000),pch=".")
symbols(x1$prestige[gen==1], x1$income[gen==1],  circles=x1$educatio[gen==1], inches=0.2, fg=1, bg=1, add=TRUE)
symbols(x1$prestige[gen==2], x1$income[gen==2],  circles=x1$educatio[gen==2], inches=0.2, fg=2, bg=2, add=TRUE)

# TO SAVE AS A BITMAP FILE FOR E.G. PUTTING INTO POWERPOINT; 
# RIGHT CLICK ON PLOT AND CHOOSE 'COPY BITMAP' THEN GO INTO WORD OR POWERPOINT, RIGHT CLICK ON AN ORDINARY POWERPOINT SLIDE OR WORD PAGE AND CHOOSE 'PASTE'

# THREE D PLOT WITH COLOURS AND ROTATION

source("http://bioconductor.org/biocLite.R")
biocLite("made4")

library(made4)

gen <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
do3d(x1,x=1,y=2,z=4,pch=19,classvec=gen,cex.symbols=2,cex.lab=0.2)

# RUN EACH TEXT COMMAND BELOW SEPARATELY THEN CLICK MOUSE>CONTINUE ON THE GRAPH WHERE YOU WANT THE
# TEXT TO GO

text(locator(1),"Education", font=2)

mtext("Prestige",1,line=1,font=2)
mtext("Suicide",2,line=1,font=2)
title("suicide by gender")
legend(locator(1),legend=c("Males","Females"),col=c(2,4),pch=c(19,19))

text(locator(1),"Prestige",font=2)
text(locator(1),"Suicide",font=2)
mtext("Education",4,adj=0)


do3d(x1,x=1,y=2,z=4,classvec=gen,angle=120,cex.symbols=2)
do3d(x1,x=1,y=2,z=4,classcol=c(2,3),classvec=gen,cex.symbols=2)
rotate3d(x1,x=1,y=2,z=4,classvec=gen,beg=40,end=320,step=40,cex.symbols=2)

# FOR BECK DATA BOXPLOTS FOR MALES AND FEMALES

x <- read.spss("beck.sav")
x1 <- data.frame(x)
boxplot(x1$beck ~ x1$sex)
boxplot(x1$log_beck ~ x1$sex)

# HISTOGRAMS

hist(x1$beck)
hist(x1$log_beck)

# PLOTS TO CHECK NORMALITY ASSUMPTIONS

qqplot(x1$beck,qnorm(ppoints(x1$beck)))
qqline(x1$beck,qnorm(ppoints(x1$beck)))
qqplot(x1$log_beck,qnorm(ppoints(x1$log_beck)))
qqline(x1$log_beck,qnorm(ppoints(x1$log_beck)))

# TESTS OF NORMALITY (TOO SENSITIVE FOR LARGE N)

mn <- mean(x1$log_beck, na.rm=TRUE)
ve <- var(x1$log_beck, na.rm=TRUE)

ks.test(x1$log_beck,"pnorm", mn, ve)
shapiro.test(x1$log_beck)

# REFERENCING INPUTTED VARIABLES BY NAME ONLY USING THE ATTACH COMMAND

attach(x1,2)

hist(beck)
hist(log_beck)
