Thursday, December 23, 2010

gnuplot: 3D graphs with 3 diamensional data

# This is a comment. gnuplot will ignore all comments, including this one.
# Stuff in red you gotta change.
# To make the path to the file less messy, you should probably just stick it in the binary folder of your gnuplot directory.
# tested on gnuplot 4.4 for windows

set dgrid3d

set xtics ("label I" 1,"label II" 2, "label III" 3, "label IV" 4, "label V" 5, "label VI" 6, "label VII" 7, "label VIII" 8) # In the same way, you can set ytics and set ztics. Optional.

set xlabel "x label" # Optional.
set ylabel "y label" # Optional.
set zlabel "z label" # Optional.

set xrange [X1:X2] # where X1 < X2. Optional.
set yrange [Y1:Y2] # where Y1 < Y2. Optional.
set zrange [Z1:Z2] # where Z1 < Z2. Optional.

# X is the column where x-coords are
# Y is the column where the y-coords are
# Z1,Z2,Z3 and Z4 are the columns where your variables (those that change with x and y) are.
# the line below is the most important part of this whole script

splot 'file.txt' using X:Y:Z1 with points pt 12 ti "Var A1", \
'file.txt' using X:Y:Z2 with points pt 12 ti "Var A2", \
'file.txt' using X:Y:Z3 with points pt 12 ti "Var A3", \
'file.txt' using X:Y:Z4 with lines ti "Var B" # when it's a 3-d plot, you gotta use splot instead of plot.

Photobucket

Wednesday, November 10, 2010

R: Redundancy Analysis

# lib(s): vegan

# data input is in the form of a MATRIX.

agt.smith <- as.matrix(infile)

# at minimum:

rda(agt.smith)

# how about some result visualization?

# /* END OF REDUNDANCY ANALYSIS */

R: INDSCAL

# lib(s): SensoMineR

# this particular function was written for the Napping data types i.e. products (stimuli) are positioned on a tableclothe by panelists, then their coordinates are used as input for the Indscal model. I haven't the slightest idea in hell what that means, so if you're equally clueless and gonna go forward with this, well... good luck to you. Fortune favours the brave.

library(SensoMineR)

# data input is in the form of a MATRIX.

agt.smith <- as.matrix(infile)

indscal(agt.smith) # this actually generates some output.

# /* END OF INDSCAL */

Wednesday, October 27, 2010

R: Biplot (generic)

# I can't get the variables in to a matrix directly. Why? If I knew why, i would fix it. So bear with this.

# put variables of interest into a dataframe:
neo <- data.frame(infile$dep.var1,infile$dep.var2)
agt.smith <- data.frame(infile$indep.var1,infile$indep.var2)

# now put it in a matrix:
x <- as.matrix(neo)
y <- as.matrix(agt.smith)

# and here's the plot:
biplot(x,y)

# for more details: ?biplot

# /* END OF BIPLOTS */

Tuesday, October 26, 2010

R: Logistic Regression

# practically reposted from Quick R

# i haven't had the chance to test this, but I really don't see why it shouldn't work... if you try using it and it refuses to cooperate, send me an email.

# Data input: generic

fit <- glm(binary.outcome.var~indep.var1+indep.var2+indep.var3+...+indep.var,data=infile,family=binomial()) # where all indep.var(s) are continous variables. And assuming your the file your data resides in is called infile.
summary(fit) # display results
confint(fit) # 95% CI for the coefficients
exp(coef(fit)) # exponentiated coefficients
exp(confint(fit)) # 95% CI for exponentiated coefficients
predict(fit, type="response") # predicted values
residuals(fit, type="deviance") # residuals

# /* END OF LOGISTIC REGRESSION*/

Monday, October 25, 2010

R: Principal coordinates metric and non-metric scaling

# lib(s): labdsv

# Data input: generic

# - Principal coordinates metric scaling - (aka Classical (Metric) Multidimensional Scaling )

# This produces some shit:

the.dist <- dist(infile)
the.cmd <- cmdscale(the.dist)

# How about a plot?
x<- the.cmd[,1]
y<- the.cmd[,2]
plot(x,y) # what is the plot supposed to show anyway?!?

# - End of Principal coordinates metric scaling - (aka Classical (Metric) Multidimensional Scaling )

# - Principal coordinates non-metric scaling - (aka Nonmetric Multidimensional Scaling)

library(labdsv)
non-met <- nmds(the.dist)

# how about another plot?
plot(non-met)

# - End of Principal coordinates non-metric scaling - (aka Nonmetric Multidimensional Scaling)

# what actually happens in either of the above, however, remains a mystery to me.

# /* END OF PRINCIPAL COORDINATES METRIC/NON-METRIC SCALING */

R: SMA (standard major axis) Regression

# !!! One should first test the significance of the correlation coefficient (r) to determine if the hypothesis of a relationship is supported. No SMA regression equation should be computed when this condition is not met. This remains a less-than-ideal solution since SMA slope estimates cannot be tested for significance. Confidence intervals should also be used with caution: simulations have shown that, as the slope departs from +/-1, the SMA slope estimate is increasingly biased and the confidence interval includes the true value less and less often. Even when the slope is near +/-1, the confidence interval is too narrow if n is very small or if the correlation is weak !!!

# Data input: generic

# To test the significance of the correlation coefficient (r):
cor.test(infile$var1,infile$var2,method="spearman")

# If the result of the above is significant(ie: your variables are significantly correlated), then the following function may be applied.
# ref for regression method: http://www.palass.org/modules.php?name=palaeo_math&page=7

sma.reg <- function(X,Y) # where X is the independent variable and Y is the dependent variable.
{
# calculate the regression slope:
std.devX <- sd(X)
std.devY <- sd(Y)
reg.slope <- std.devX/std.devY

# calculate the y-intercept
av.X <- mean(X)
av.Y <- mean(Y)
y.intercept <- av.Y-(reg.slope*av.X)

# calculate the sign of the slope:
Xmc <- X-av.X
Ymc <- Y-av.Y
XmcYmc <- Xmc*Ymc
slope.dir <- sum(XmcYmc)
if(slope.dir<0)
{
min.Ycoords <- -1*reg.slope*(min(X))+y.intercept
max.Ycoords <- -1*reg.slope*(max(X))+y.intercept
Xcoords <- c(min(X),max(X))
Ycoords <- c(min.Ycoords,max.Ycoords)
plot(X,Y)
lines(Xcoords,Ycoords)
paste("Y","=","-",reg.slope,"X","+",y.intercept)
} else
{
min.Ycoords <- reg.slope*(min(X))+y.intercept
max.Ycoords <- reg.slope*(max(X))+y.intercept
Xcoords <- c(min(X),max(X))
Ycoords <- c(min.Ycoords,max.Ycoords)
plot(X,Y)
lines(Xcoords,Ycoords)
paste("Y","=",reg.slope,"X","+",y.intercept)
}
}
# /* END OF SMA REGRESSION */