31 January 2013

r plot


Saving the plot beforehand
jpeg('rplot.jpg')
plot(x,y)
dev.off()

If error for above, save current active plot
dev.copy(png,'myplot.png')
dev.off()

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