# Simulation Paramter Settings
N <- 5000 # Sample Size
MX <- 10 # Mean of X
SDX <- 2 # SD of indepdenent part of X
MZ <- 10 # Mean of Z
SDZ <- 2 # SD of independent part of Z
Yerror <- 6 # SD of error in Y
a <- 10 # See model above
b <- 1
c <- .5
d <- .5

# Simulate X and Z
tempvar <- rnorm(N,0,1) # shared X, Z variance
X <- rnorm(N,MX, SDX) + tempvar
Z <- rnorm(N,MZ, SDZ) + tempvar
rm(tempvar)

# Simulate Y
Y <- a + (b * X) + (c * Z) + (d * X * Z) + rnorm(N,0,Yerror)
XYZ <- data.frame(X,Z,Y)
summary(XYZ)
cov(XYZ)
cor(XYZ)

# General Plot Parameters
proportion <- .05 # Proportion of N to be plotted
plotN <- round(proportion * N)

# Bubble Plot
symbols(x = X[1:plotN], y = Z[1:plotN], circles = Y[1:plotN], xlab = 'X', ylab = 'Z', main = "Y by X and Z", sub = 'Point Size = Y')

# Bubble Plot 2
symbols(x = X[1:plotN], y = Z[1:plotN], circles = Y[1:plotN], xlab = 'X', ylab = 'Z', fg='black', bg='grey', main = "Y by X and Z", sub = 'Point Size = Y')


# Bubble Plot 3
plot(seq(min(X)-2,max(X)+2,len=10),seq(min(Z)-2,max(Z)+2,len=10),type='n',xlab='X',ylab='Z', main='Y by X and Z',sub='Point Size = Y')
rad.ex <- .5
symbols(x = X[1:plotN], y = Z[1:plotN], circles = ((rad.ex*Y[1:plotN])/max(Y[1:plotN])), fg='black', bg='grey', add=T, inches=F)
symbols(x = X[1:plotN], y = Z[1:plotN], circles = ((rad.ex*Y[1:plotN])/max(Y[1:plotN])), add=T, inches=F)
# rad.ex serves as a scaling parameter for the expansion of the radii of the circles, in place of character expansion (cex).
# The add parameter in the symbols() function adds to the existing plot.
# The second symbols() function redraws the circles over the fill.
# The inches parameter set to FALSE scales the radii to the axis scale.

# In the study, it might be important to consider instructions on 
# reading the bubble plot. For example, everybody is taught that 
# if the lines cross in an ANOVA line plot, then there is an interaction.
# Similarly, it may be important to instruct the participants that if
# the comparative sizes of circles changes from one vertical (or horizontal)
# line to another, then there is an interaction.
# Instruction could either be a variable or a constant in the study.

# At some point the code for the bubble plot will become sufficiently complex
# that it will be easier to code it into a function that can be called with
# various parameters to alter the appearance of the plot.


