Posted by & filed under R.

At my previous internship, SAS was the software of choice for about 80% of the statistical work done; this rate was pulled down by me using/pushing for R and another co-worker using/pushing for Python.

One of the positive features of SAS is its PROC FREQ command. It gives useful summaries for contingency tables, such as row and column summary statistics and tests of different types. R‘s base function table is not full of bells and whistles. To get similar statistics as SAS’s PROC FREQ, check out CrossTable from the gmodels package or the base function margin.table to get row/column totals. See this for examples.

Tags:

Posted by & filed under ESS, R.

I really like David Whiting’s Sweave methods in ESS: M-n s M-n P and I get a compiled pdf file. However, i wanted to change it so Sweave uses the cacheSweaveDriver() when sweaving. I knew the the functions were defined in the ess-swv.el file (in my carbon emacs in Mac OS X, it is at /Applications/Emacs.app/Contents/Resources/site-lisp/ess/ess-swv.el). Now, I edited the file to create a new way to Sweave:

;;; changed by vinh
(defun ess-swv-run-in-R2 (cmd &optional choose-process)
 "Run \\[cmd] on the current .Rnw file. Utility function not called by user."
 (let* ((rnw-buf (current-buffer)))
 (if choose-process ;; previous behavior
 (ess-force-buffer-current "R process to load into: ")
 ;; else
 (update-ess-process-name-list)
 (cond ((= 0 (length ess-process-name-list))
 (message "no ESS processes running; starting R")
 (sit-for 1); so the user notices before the next msgs/prompt
 (R)
 (set-buffer rnw-buf)
 )
 ((not (string= "R" (ess-make-buffer-current))); e.g. Splus, need R
 (ess-force-buffer-current "R process to load into: "))
 ))

 (save-excursion
 (ess-execute (format "require(tools)")) ;; Make sure tools is loaded.
 (basic-save-buffer); do not Sweave/Stangle old version of file !
 (let* ((sprocess (get-ess-process ess-current-process-name))
 (sbuffer (process-buffer sprocess))
 (rnw-file (buffer-file-name))
 (Rnw-dir (file-name-directory rnw-file))
 (Sw-cmd
 (format
 "local({..od <- getwd(); setwd(%S); %s(%S, cacheSweaveDriver()); setwd(..od) })"
 Rnw-dir cmd rnw-file))
 )
 (message "%s()ing %S" cmd rnw-file)
 (ess-execute Sw-cmd 'buffer nil nil)
 (switch-to-buffer rnw-buf)
 (ess-show-buffer (buffer-name sbuffer) nil)))))


(defun ess-swv-weave2 ()
 "Run Sweave on the current .Rnw file."
 (interactive)
 (ess-swv-run-in-R2 "Sweave"))

(define-key noweb-minor-mode-map "\M-nw" 'ess-swv-weave2)

Now, I can do M-n w to Sweave with cacheSweaveDriver(). Tried it, didn’t work. After many trials, i decided to put this code in my .emacs file. Works. Hmm, wierd. Is ESS using a different ess-swv.el file? Tried deleting it from where i think it is. M-n s still works. Weird!!!

After some more searching, found out that the stuff is going on in the ess-swv.elc file. Deleted both files, M-n s doesn’t work anymore. Googled what *.elc is. It is a byte-compiled version of .el (to make things run faster in emacs, sometimes). So I did it to mine.

Byte compile for emacs: http://www.cs.utah.edu/dept/old/texinfo/emacs18/emacs\_26.html

So I open my ess-swv.el file in emacs, typed M-x byte-compile-file

Voila! .elc file created. Close emacs and run it again!

Posted by & filed under Uncategorized.

Cooliris, an add-on for most browsers (firefox, safari, even IE), let’s you browse images on websites interactively and in a very “nice” layout.

I had it before, but never got it to work because they said it works for photobucket but I couldn’t get it to work (they say press on the icon). Recently, I was looking at pictures through Picasa, and I hit the icon that appears on the picture and it took me to it. Really cool! You can do searches (google, youtube, etc) and play images and videos.

After some time googling, I found out that with photobucket (and any supported site), I can click the cooliris icon right next to the url bar / search bar. It will be un-grayed if the site is supported.

Works with facebook as well.

Posted by & filed under Uncategorized.

So google didn’t add a feature where we can attach files to blogs (other than image files). I wanted to do it with google docs, but couldn’t because I’d have to share it with people by inviting them. Since Google’s G drive isn’t out yet, I’m going to put it in my pubic folder in dropbox. This is actually a good, long-term solution.

NOTE to self, always put ” Here is the attached file. ” and link the file this way. This way I can easily search for posts that have attachments.

Posted by & filed under LaTeX, R.

I wanted to draw arrows in LaTeX to make that predictor-outcome-confounder diagram I saw in class. I found an answer here using the xy package in LaTeX.

I used to want to draw in LaTeX. pgf and tiks looks cool, but the learning curve looks too steep for me. I opted to R’s plotting and openoffice’s drawing capabilities. However, I just discovered the picture environment in LaTeX, which is not hard at all! It is also described here.

I will post up some examples.

Here is the attached file. It is also at ScribD.

Posted by & filed under LaTeX.

These days when search for LaTeX questions, I often end up on up at this wikibook.

I don’t like pulling up the “not so short guide.” as I can can get the answer through Google much faster. Google is like my second memory bank. All I need is to be able to link my thoughts to keywords. For example, I wanted to make a heat map in a previous post. The heatmap function in R did not produce what I wanted. I then remembered a heatmap is actually a colored contour plot. I googled “R colored contour” and it took me to filled.contour, exactly what I needed.

Back to LaTeX. I just wanted to remind myself that when others ask me what they should do to learn LaTeX, I should send them to the “not so short guide” and this wikibook (Leslie Lamport’s book is good too, but come on, who really needs to buy a computer book these days? It’s a good reference, but to learn and get started, there are plenty of good, free alternatives online).

Posted by & filed under R, Statistics.

http://blog.nguyenvq.com/wp-content/uploads/2009/05/LinearRegressionNormalErrors.jpeg

Linear Regression: Normal Errors

I remember my first encounter to the classical linear regression model with normally distributed errors by Dan Gillen in Stat 120c in spring 2005 at UCI. I mean, I’ve done linear regression in high school science classes: you get a best fit line out of your data. In the stat class, I remember seeing data drawn on the board and a line going through the data. To illustrate the normally distributed errors, he drew sideway normal curves.

I liked this presentation of the classical model. How do convey this in R? I couldn’t find a package for it (searched for an hour I think). I always search for packages because I always think others could do a better job at it than me and because I don’t like to re-invent things, even though it requires less time for me to do it myself in many cases. Well, here it is, written by me in R:

sideways.dnorm <- function(where.x, where.y, values=seq(-4,4,.1), magnify=4, ...){
 ###... consists of mean, sd, and log passed to dnorm function
 dens <- dnorm(x=values, ...)
 x <- where.x + dens * magnify
 y <- where.y + values
 return(cbind(x,y))
 }

temp <- sideways.dnorm(where.x=3, where.y=3, values=seq(-2,2,.1))
plot(temp)

##jpeg("LinearRegressionNormalErrors.jpeg")
pdf("LinearRegressionNormalErros.pdf")
set.seed(123)
n <- 200
x <- runif(n, -8,8)
y <- 1 + .5*x + rnorm(n, sd=1)
fit <- lm(y~x)
plot(x,y, xlim=c(-10,10), ylim=c(-8, 10)
, main="Classical Linear Regression\nwith Gaussian errors"
##, main=expression(paste("Classical Linear Regression\nwith N(0,", sigma\^2, ") errors", sep=''))
, col="red"
)
abline(fit, lwd=3, col="blue")
where.normal.x <- c(-4,0,4)
for(i in 1:length(where.normal.x)){
 where.x <- where.normal.x[i]
 where.y <- predict(fit, newdata=data.frame(x=where.x))
 xy <- sideways.dnorm(where.x=where.x, where.y=where.y, magnify=4)
 lines(xy)
 abline(h=where.y, lty=2)
 abline(v=where.x, lty=2)
 }

dev.off()