Tuesday, July 26, 2011

R - recode() data

recode() - from package lordif

library(lordif)  # maybe you have first to install the package (install.packages("lordif"))
  • creating example data
z <- sample(0:1, 25, replace=T)
original <- c(0,1)
new <- c("boy", "girl")
z
original
new
z:  1 1 1 0 0 0 1 1 1 1 0 0 1 1 1 1 0 1 1 1 1 1 1 1 0
original: 0 1
new:  "boy"  "girl"
  • so z is a vector consisting of 1 and 0
  • original contains the distinct values of z
  • and new contains the values which should replace the original values in the following way: the first item of original is replaced by the first value of new, the second by the second etc pp
z <- recode(z, original, new)
z
 [1] "girl" "girl" "girl" "boy"  "boy"  "boy"  "girl" "girl" "girl" "girl"
[11] "boy"  "boy"  "girl" "girl" "girl" "girl" "boy"  "girl" "girl" "girl"
[21] "girl" "girl" "girl" "girl" "boy"

u

Wednesday, July 20, 2011

R - t.test for subsets of a data frame (ddply or tapply)

(two sample t-test for groups in data frame here)
First we create a data frame, on which we can work
x <- rnorm(100)                 
y <- sample(1:3, 100, replace=T)
z <- sample(1:2, 100, replace=T)

df <- data.frame(group=y,sex=z,sds=x)
head(df)
group sex       sds
1     1   2 1.3412663
2     1   1 0.8000326
3     2   2 0.0371029
4     1   2 0.2064684
5     1   2 1.6429816
6     3   1 1.3271138
In the following I use ddply() from the plyr-package, so if you did not install and/or load it you have to do it now - the command library() without any argument gives you a list of the installed packages:
install.packages("plyr") # install the package
library(plyr) # load it
Here is the function for doing the t.tests:
t.test.plyr <- function(x, var, mean=0 ){
  y <- rep(NA,10)
  y[6] <- nrow(x)[1]              # count observations
  if(nrow(x) < 2) return(y)       # exits if too less observations
  res <- t.test(x[var], mu=mean)  # doing the test

  y[1] <- res$statistic           # extract values of interest
  y[2] <- res$p.value      
  y[3] <- res$estimate     
  y[4] <- res$conf.int[1]  
  y[5] <- res$conf.int[2]  
  y[7] <- res$parameter    
  y[8] <- res$method       
  y[9] <- res$alternative  
  y[10] <- res$null.value   

  names(y) <- c("statistic","p.value","estimate","conf.int1", "conf.int2", "nobs","dof","method","alternative","null.value")
  y 
}

where t.test.plyr() is a function of:
- x - the data frame
- var - the variable which should be testet
- mean - the mean of the t-test

now we can use ddply in the following way:
result <- ddply(df, .(group, sex), t.test.plyr, "sds")
result
group sex           statistic           p.value            estimate
1     1   1   0.774917157596946 0.445379429071851   0.132214420988956
2     1   2  -0.415594591752269  0.68359099812399 -0.0987341821354776
3     2   1   0.130257003222609 0.899578855830346  0.0602600950757679
4     2   2 -0.0579045286518569 0.954965406753824 -0.0150105343694587
5     3   1    1.18044920537142 0.256202807853187   0.406567809727671
6     3   2    1.49215606704641  0.15126489039304   0.294865619363907
           conf.int1         conf.int2 nobs dof            method alternative
1 -0.218494854049804 0.482923696027717   27  26 One Sample t-test   two.sided
2 -0.605109702463049 0.407641338192093   16  15 One Sample t-test   two.sided
3  -1.00655416438508  1.12707435453662    9   8 One Sample t-test   two.sided
4 -0.592608791299465 0.562587722560548   11  10 One Sample t-test   two.sided
5 -0.327541518602652  1.14067713805799   16  15 One Sample t-test   two.sided
6 -0.117342538637965  0.70707377736578   21  20 One Sample t-test   two.sided
  null.value
1          0
2          0
3          0
4          0
5          0
6          0
If you want to change the mu in you t.test, add the argument mean:
result <- ddply(df, .(group, sex), t.test.plyr, "sds", mean=1)
result
If there is no need or you do not want a data frame as result you can just use tapply(). tapply results in a list with as many elements as subsets, each element contains the results of each t.test (as list).
res <- tapply(df$sds, list(df$sex,df$group), t.test)
res
1      2      3     
1 List,9 List,9 List,9
2 List,9 List,9 List,9
So the first element of res contains the t.test results for sex=1 and group=1 etc pp
res[[1]]
One Sample t-test

data:  X[[1L]] 
t = 0.7749, df = 26, p-value = 0.4454
alternative hypothesis: true mean is not equal to 0 
95 percent confidence interval:
 -0.2184949  0.4829237 
sample estimates:
mean of x 
0.1322144

Friday, July 15, 2011

Ubuntu - convert pdf to images (resolution selectable)

nice command line tool:
pdftoppm

creates one image (png, jpeg pbm or pgm) per page.

e.g. if source.pdf has two pages
pdftoppm -png -r 600 source.pdf target
creates 2 images, solution: 600 DPI, file names: target-01.png, target-02.png

Thursday, July 14, 2011

Emacs - change style of word wrapping

use word wrap
M-x visual-line-mode
Emacs displays long lines by truncation
M-x toggle-truncate-lines

VBA excel - sheets exist ?

The function:
Public Function SheetExists(SheetName As String) As Boolean
    Dim ws As Worksheet
    SheetExists = False
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name = SheetName Then SheetExists = True
    Next ws
End Function
create sheet if it does not exist:
For i = 1 to n
    If SheetExists("Sheet" & CStr(i)) Then
        MsgBox ("Sheet" & CStr(i) & " exists")
    Else
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Sheet" & CStr(i)
    End If
Next i

Saturday, July 9, 2011

R ggplot - barplot

barplot

  • load the package ggplot2 and the diamond data
  • first we create a object p of class ggplot, where we map clarity to x (horizontal position)
p <- ggplot(diamonds, aes(x=clarity))
summary(p)
data: carat, cut, color, clarity, depth, table, price, x, y, z
  [53940x10]
mapping:  x = clarity
faceting: facet_grid(. ~ ., FALSE)
  • p contains all information ggplot needs to build a barplot, all we have to do is adding a layer with geom="bar";
p + layer(geom="bar")

  • to this simple barplot we can add the following (optional) aesthetics:
    • colour: color of the borders of the bins
    • fill: color of the filling
    • size:
    • linetype: line type of the borders
    • weight:
    • alpha: transparency of fill [0,1]
p + layer(geom="bar", geom_params=list(fill="steelblue", colour="black", linetype=4, alpha=0.3))

  • but a barplot can visualize more information about the data - so we add a new mapping to p
p <- ggplot(diamonds, aes(x=clarity, fill=color)) # where color is the variable of the dataframe diamonds
  • now the default output looks like:
p + layer(geom="bar")

  • the plot can be customized by the same aesthetics, fill would override the aesthetic mapping of p
  • additional we can add a position adjustment
p + layer(geom="bar", position="dodge") 

  • or
p + layer(geom="bar", position="fill") 

  • if we flip (coordflip()) the underlying coordinate system, we can rotate the plot
p + layer(geom="bar", position="stack") + coord_flip()

  • or we even can change the coordinate system to polar coordinates:
p + layer(geom="bar", position="stack") + coord_polar()

R - filled.contour plot

filled.contour

  • if you have a matrix with nxm dimensions and every entry represents a value of interest and the indices of the entry describe the position, then you can use filled.contour() to visualize the matrix
  • first we create a simple matrix to demonstrate how it works:
x <- y <- 1:10 # create two vectors with the integers from 1 to 10
z <- outer(x,y) # create a matrix as the outer product of the two vectors
z # show the matrix
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    2    3    4    5    6    7    8    9    10
 [2,]    2    4    6    8   10   12   14   16   18    20
 [3,]    3    6    9   12   15   18   21   24   27    30
 [4,]    4    8   12   16   20   24   28   32   36    40
 [5,]    5   10   15   20   25   30   35   40   45    50
 [6,]    6   12   18   24   30   36   42   48   54    60
 [7,]    7   14   21   28   35   42   49   56   63    70
 [8,]    8   16   24   32   40   48   56   64   72    80
 [9,]    9   18   27   36   45   54   63   72   81    90
[10,]   10   20   30   40   50   60   70   80   90   100
  • now let us plot the matrix:
filled.contour(z)

  • the matrix is mapped to a 1x1 square: the entry with the indices (1,1) and the value 1 to the point with the coordinates (0,0) and the entry with the indices (10,10) and the value (100) to the points with the coordinates (1,1); if you want to change the axes you can add a x and a y argument to the plot;so let's use the vectors x and y from above (with the entries 1 to 10) and also change to colors
  • filled.contour() uses the cm palette as default (cyan for low, magenta for heigh values);
filled.contour(x=x,y=y,z, col=heat.colors(20))

  • more palettes you find here

And a last example

my.seq <- seq(-pi, pi, length=50) # creating a vector as a sequence from 0 to 2*pi with 50 entries
my.seq2 <- seq(-0.5*pi, 1.5*pi, length=50) # creating a vector as a sequence from 0 to 2*pi with 50 entries
my.matrix <- outer(sin(my.seq),cos(my.seq2)) # creating the matrix using sin, cos and outer
filled.contour(x=my.seq, y=my.seq2,my.matrix, plot.title=title(main="Products Sin(x)*Cos(y) on [-Pi,Pi]x[-0.5Pi,1.5Pi]", xlab="x", ylab="y"), key.title=title(main="products"), col=terrain.colors(20))

Latex Beamer - package beamercolor

Manual.
You can download the .sty file: here BeamerColor.sty
create a folder in your latex file directory
/usr/share/texmf-texlive/tex/latex/
the path can differ from the path above (e.g. if you use TeTeX).
copy the example.sty file in this directory
cp /home/me/pathtopackage/example.sty /usr/share/texmf-texlive/tex/latex/

run
sudo mktexlsr
add the line \usepackage{example} to you document

Friday, July 8, 2011

RExcel - do not work after installing new R version

When a new version of R is installed, a few things have to be done.

rcom and rscprooxy have to be installed in the new version.
 install.packages(c("rcom", "rscproxy")) 

the line
library(rcom) 
has to be added to the etc\RProfile.site (standard path Win: C:\Programme\R\R-Version\etc\)

comRegisterRegistry() has to be run (in the new version)

bin\RSetReg.exe has to be run  (path Win: C:\Programme\R\R-2.12.1\bin\i386)

Thursday, July 7, 2011

R ggplot2 - simple heatplot

simple heatplot

  • load the library ggplot2, if you have not installed it yet - install it via install.packages("ggplot2")
  • create the example data frame
  • map it to the plot
x <- rep(1:20,20)
y <- rep(1:20, rep(20,20))
z <- rnorm(400, mean=0, sd=15)
df <- data.frame(x=x,y=y,z=z)
ggplot(df, aes(x=x,y=y, fill=z)) + geom_tile()

  • now we can change the colors (colorchart) by add a scale_fill_gradient(low="green", high="red")
ggplot(df, aes(x=x,y=y, fill=z)) + geom_tile()+scale_fill_gradient(low="green", high="red")

  • an a second one (with more soothing colors)
ggplot(df, aes(x=x,y=y, fill=z)) + geom_tile()+scale_fill_gradient(low="aliceblue", high="midnightblue")