26 January 2013

2D PCA with SVD in R

res.svd <- svd(x)
t <- res.svd$u %*% diag(res.svd$d)
t1 <- t[,1]
p1 <- res.svd$v[,1]
gues <- t1 %*% t(p1)
plot(x)
points(gues[,1],gues[,2],col='red')

25 January 2013

Factor Analysis in R


fa.alldat <- factanal(alldat[c(1,2,3)],1,scores="regression");
biplot(fa.alldat$scores,fa.alldat$loadings,cex=0.5)


factanal
using maximum likelihood factor analysis

fa
is basically using Principal Axes

21 January 2013

Bash Script Compilation

for loop
for((i=6; i<10; i++)) ; do ... done

leading zero
valf=$(printf "%.5d" "$val")

the server my wiki is on is not feeling well currently... ;_;

Summary for MCA in R

standing <- read.table("filename.dat",sep=",",header=TRUE,row.names=1)

summary(gmo[,1:16])

specifiying supplement (sup, eg. description of individual answering questionairre) on the data:  res.mca <- MCA(tea,quali.sup=c(19:21,23:26))

names(standing)

ggplot:
mca1 = MCA(standing, graph = FALSE)
cats = apply(standing,2,function(x) nlevels(as.factor(x)))
mca1_vars_df = data.frame(mca1$var$coord, Variable = rep(names(cats), cats))
mca1_obs_df = data.frame(mca1$ind$coord)
ggplot(data = mca1_vars_df, aes(x = Dim.1, y = Dim.2, label = rownames(mca1_vars_df))) + geom_hline(yintercept = 0, colour = "gray70") + geom_vline(xintercept = 0, colour = "gray70") + geom_text(aes(colour = Variable)) + geom_density2d(colour = "gray80") + geom_text(data = mca1_vars_df, aes(x = Dim.1, y = Dim.2, label = rownames(mca1_vars_df), colour = Variable))

category comparison (one with others):
catdes(standing,num.var=2)
    $test.chi2


run a script containing printouts and plots:
source("script.r",print.eval = TRUE)

(note: printing is turned off in loops), so to plot in a loop:
print(ggplot(...))

leading zeros:
u <- formatC(i, width = 2, format = "d", flag = "0")
or
sprintf("Leading zeros:%04d", x)

Variables representation (use new.plot when the graph window is not showing anymore):
plot.MCA(res.2,choix="var",new.plot=TRUE)
add cex=0.5 for small font

Plotting data.frame:
X11();barplot(c(as.matrix(mca2$eig[3])))

Data used for "variables representation" plot in MCA:
res.mca$var$eta2

17 January 2013

R related installing on old ubuntu (9.10 karmic)

wanted to install ggplot2

install.packages("ggplot2")
but couldn't get it through cran anymore

sudo apt-get install r-cran-ggplot2
also couldn't get it through package

so the other way was to download the old tar from cran (start from a relatively new one, the command will give error if your R is too old, karmic is R 2.9.2 btw)
and apparently, this command saves me (I remembered installing manually to R folders, hmm)
R CMD INSTALL Desktop/packagename.tar.gz

PS. sigh, never edit a blogger post anymore, blogger ruined my previous R post

03 January 2013

Linking file from host in Virtual Box


mkdir /home/<user>/vboxshare
sudo mount -t vboxsf -o uid=1000,gid=1000 share /home/<hostuser>/vboxshare
sudo adduser <user> vboxsf
make dir in vboxshare, ln -s that folder in the virtual os

Virtual OS was Ubuntu 12.04, Host was Mac OSX 10.6.8

15 November 2012

transparent object (weird depth problem)



The possible causes are:
no glEnable(GL_DEPTH_TEST);
no glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); in every frame
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.0f,100.0f);
0 for near (should be 0.1f)

But the culprit of my problem is:
glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE ); 
which is called before you run openGL
If you don't put GLUT_DEPTH in here, depth won't be calculated!

To check whether you've defined depth or not in your program:
int depth;
glGetIntegerv(GL_DEPTH_BITS, &depth);
std::cout << "depth " << depth << std::endl;
if the result is 0, that means the depth is not defined.
This GL_DEPTH_BITS seems to be deprecated in openGL 3.3 / 4, though. I don't know what my version is but it worked (was 0, is now non 0).

I'm using gtkglarea, and the setting for it is:
int attrlist[] = {
    GDK_GL_RGBA,
    GDK_GL_RED_SIZE,1,
    GDK_GL_GREEN_SIZE,1,
    GDK_GL_BLUE_SIZE,1,
    GDK_GL_DOUBLEBUFFER,
    GDK_GL_DEPTH_SIZE,16,
    GDK_GL_NONE
};
which is used by  glArea = GTK_WIDGET(gtk_gl_area_new(attrlist)); to create new open widget in GTK.
Not sure why the size is 16, though, but putting on 32 results in a blank screen for mine.

Beware that some computer may work by default without this depth setting. Ugh. @_@