Monday, April 16, 2012

Convert ERb into HAML (via rake)

install these gems:

haml-rails
ruby_parser
hpricot


#./lib/tasks/erb2haml.rake
desc "Creates haml files for each of the erb files found under views (skips existing)"
task :erb2haml do
  from_path = File.join(File.dirname(__FILE__), '..', '..', 'app', 'views')
  Dir["#{from_path}/**/*.erb"].each do |file|
    puts file
  # for each .erb file in the path, convert it & output to a .haml file
  output_file = file.gsub(/\.erb$/, '.haml')
  `bundle exec html2haml -ex #{file} #{output_file}` unless File.exist?(output_file)
 end
end

now run rake
and don't forget that its not rake erb2html, its rake erb2haml!

From:
here

PS:
gem 'ruby_parser'
gem 'hpricot'

Sunday, April 15, 2012

set up git for the first use (with rails)

After installing the packages git-core and gitosis:

configure
git config --global user.name "username"
git config --global user.email "user@mail.com"
git config --global core.editor "emacs -w"

run
git init

customize the .gitignore file
examples here

add your project to git
git add . ## from your project folder


to see which files are in the staging area:
git status

to tell Git you want to keep the changes
git commit -m "initial commit"


list of commit messages:
git log


undo changes
git checkout -f ## -f flag to force overwriting the current changes

Wednesday, March 21, 2012

Saturday, March 17, 2012

R - dates with lubridate

datetime

1 package lubridate

1.1 reading dates with lubridate

  • lubridate is a relatively new package which allows to handle date and date time formats in a more convenient way than it was possible with format()
  • first load the package
library(lubridate)
  • if you want to read date or convert some data into date/date time format you can use ymd()
  • the first argument to ymd() have to be numeric oder string vector of suspected dates
  • it should be used if there is a year, month and time component in a arbitry order, seperated by one of the following seperators: "-", "/", ".", and ""
  • analogous ymd should appear in one of the following order: ymd, ydm, mdy, myd, dmy, dym
  • there is a optional argument tz to specify which time zone to parse the date with (string, known by the OS)
ydm(12051103) # ->  "1205-03-11 UTC"
ymd(12051103) # ->  "1205-11-03 UTC"
dym(12051103) # ->  "511-03-12 UTC"
dmy(12051103) # ->  "1103-05-12 UTC"
mdy(12051103) # ->  "1103-12-05 UTC"
myd(12051103) # ->  "511-12-03 UTC"
[1] "1205-03-11 UTC"
[1] "1205-11-03 UTC"
[1] "511-03-12 UTC"
[1] "1103-05-12 UTC"
[1] "1103-12-05 UTC"
[1] "511-12-03 UTC"
  • it can also deal with the year consisting of two digits
dym(120503) # -> "2005-03-12 UTC"
dym(127503) # -> "1975-03-12 UTC"
[1] "2005-03-12 UTC"
[1] "1975-03-12 UTC"
  • extract information:
    • day
my.date <- dym(120503)
day(my.date)
[1] 12
  • week day (number)
wday(my.date)
[1] 7
  • week day (string)
wday(my.date,label=T)
[1] Sat
Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat
  • year
year(my.date)
[1] 2005
  • month (number)
month(my.date)
[1] 3
  • month (name of month)
month(my.date,label=T)
[1] Mar
12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
  • week
week(my.date)
[1] 11
  • day of year
yday(my.date)
[1] 71

1.2 working with dates

  • get the origin of the current time scale
origin
[1] "1970-01-01 GMT"
  • get the current date
x <- today()
x
[1] "2012-03-17"
  • week of current day
week(x)
  • two weeks later
week(x) <- week(x) + 2
x
  • is also a Saturday
wday(x,label=T)
 [1] Sat
Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat
  • rounding: floor_date(), ceiling_date()
    • rounding down and up to the nearest integer day value
floor_date(my.date,"day")     # -> "2005-03-12 UTC" 
ceiling_date(my.date,"day")   # -> "2005-03-13 UTC"
[1] "2005-03-12 UTC"
[1] "2005-03-13 UTC"
  • rounding down and up to the nearest integer month value
floor_date(my.date,"month")     # ->  "2005-03-01 UTC"
ceiling_date(my.date,"month")   # ->  "2005-04-01 UTC"
[1] "2005-03-01 UTC"
[1] "2005-04-01 UTC"
  • rounding down and up to the nearest integer year value
floor_date(my.date,"year")     # ->  "2005-01-01 UTC"
ceiling_date(my.date,"year")   # ->  "2006-01-01 UTC"
[1] "2005-01-01 UTC"
[1] "2006-01-01 UTC"
  • is a year a leap-year (argument has to be a date)
leap_year(ymd("20000101"))
  • create a time interval given start and end point
date1 <- ymd("2000-01-01")
date2 <- ymd("2000-10-01")
my.int <- new_interval(date1,date2)
my.int
[1] 2000-01-01 UTC--2000-10-01 UTC
  • check whether or not a date falls within an interval
date3 <- ymd("2001-01-02")
date3 %within% my.int
[1] FALSE

or

date4 <- ymd("2000-06-02")
date4 %within% my.int
[1] TRUE
  • check whether or not an interval falls within an interval
my.int2 <- new_interval(date1,date4)
my.int2 %within% my.int
[1] TRUE

Date: 2012-03-17 12:42:50 CET

Author: Mandy

Org version 7.6 with Emacs version 23

Validate XHTML 1.0

Tuesday, November 29, 2011

R Confidence Intervals and Regions in a linear model

  • for a linear model: \( mm = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + ... + \beta_n x_n \) you can get the confidence intervals of the parameters \( \beta_0 ... \beta_n \)
data(trees)                  ## load the data
mm <- with(trees, lm(Volume ~ Girth + Height)) ## linear model
confint(mm)                  ## get the confidence intervals
2.5 %      97.5 %
(Intercept) -75.68226247 -40.2930554
Girth         4.16683899   5.2494820
Height        0.07264863   0.6058538
[1] "org_babel_R_eoe"
  • the package ellipse provides a command to construct a 2-dimensional confidence region, here we will compute the ellipse for Girth and Height
library(ellipse)
plot(ellipse(mm,c(2,3)),type="l",xlim=c(0,5.5))
points(0,0)
points(coef(mm)[ 2],coef(mm)[ 3],pch=18)
abline(v=confint(mm)[2,],lty=2)
abline(h=confint(mm)[3,],lty=2)


  • we see: (0,0) lies outside the ellipse so we can reject \( H_0 \)
  • the two abline commands produce the lines indicating the one-way confidence intervals, if they were tangential to the ellipse, the CIs would be jointly correct

Monday, November 21, 2011

EBImage

installing

  • EBImage is a part of BioConductor and it is not available on CRAN, so you have to download and install it from the Bioconductor website
  • please type the following as super user (i.e. with admin rights), you will be asked whether you want to update a bunch of packages, answer a - this could take a few minutes
source("http://www.bioconductor.org/biocLite.R")
biocLite("EBImage")

Information about packages

show installed packages

  • show just the names of the installed packages (ordered)
sort(row.names(installed.packages()))
[1] "abind"           "acepack"         "AER"             "akima"          
  [5] "anchors"         "ape"             "base"            "bdsmatrix"      
  [9] "biglm"           "Biobase"         "BiocInstaller"   "bitops"         
 [13] "boot"            "car"             "CarbonEL"        "caTools"        
 [17] "chron"           "class"           "cluster"         "coda"           
 [21] "coda"            "codetools"       "coin"            "colorspace"     
 [25] "compiler"        "CompQuadForm"    "cubature"        "DAAG"           
 [29] "datasets"        "DBI"             "Deducer"         "DeducerExtras"  
 [33] "degreenet"       "Design"          "digest"          "diptest"        
 [37] "doMC"            "doSNOW"          "dynlm"           "e1071"          
 [41] "Ecdat"           "effects"         "ellipse"         "ergm"           
 [45] "fBasics"         "fCalendar"       "fEcofin"         "flexmix"        
...
  • the command installed.packages() provides much more information:
colnames(installed.packages())
[1] "Package"   "LibPath"   "Version"   "Priority"  "Depends"   "Imports"  
 [7] "LinkingTo" "Suggests"  "Enhances"  "OS_type"   "License"   "Built"
  • so if you want to know the package and its version
installed.packages()[,c("Package","Version")] # you can also use the col numbers c(1,3)
Package           Version      
Biobase         "Biobase"         "2.14.0"     
BiocInstaller   "BiocInstaller"   "1.2.1"      
GenABEL         "GenABEL"         "1.6-9"      
multtest        "multtest"        "2.10.0"     
abind           "abind"           "1.3-0"      
acepack         "acepack"         "1.3-3.0"    
AER             "AER"             "1.1-7"      
akima           "akima"           "0.5-4"      
anchors         "anchors"         "3.0-7"      
ape             "ape"             "2.7-1"      
bdsmatrix       "bdsmatrix"       "1.0"        
...
  • show information about a package
packageDescription("multtest")
Package: multtest
Title: Resampling-based multiple hypothesis testing
Version: 2.10.0
Author: Katherine S. Pollard, Houston N. Gilbert, Yongchao Ge, Sandra
        Taylor, Sandrine Dudoit
Description: Non-parametric bootstrap and permutation resampling-based
        multiple testing procedures (including empirical Bayes methods)
        for controlling the family-wise error rate (FWER), generalized
        family-wise error rate (gFWER), tail probability of the
        proportion of false positives (TPPFP), and false discovery rate
        (FDR).  Several choices of bootstrap-based null distribution
        are implemented (centered, centered and scaled,
        quantile-transformed). Single-step and step-wise methods are
        available. Tests based on a variety of t- and F-statistics
        (including t-statistics based on regression parameters from
        linear and survival models as well as those based on
        correlation parameters) are included.  When probing hypotheses
        with t-statistics, users may also select a potentially faster
        null distribution which is multivariate normal with mean zero
        and variance covariance matrix derived from the vector
        influence function.  Results are reported in terms of adjusted
        p-values, confidence regions and test statistic cutoffs. The
        procedures are directly applicable to identifying
        differentially expressed genes in DNA microarray experiments.
Maintainer: Katherine S. Pollard <kpollard@gladstone.ucsf.edu>
Depends: R (>= 2.9.0), methods, Biobase
Imports: survival, MASS
Suggests: snow
License: LGPL
biocViews: Microarray, DifferentialExpression, MultipleComparisons
LazyLoad: yes
Packaged: 2011-11-01 04:28:05 UTC; biocbuild
Built: R 2.14.0; i686-pc-linux-gnu; 2011-11-11 10:32:24 UTC; unix

-- File: /home/mandy/R/i686-pc-linux-gnu-library/2.14/multtest/Meta/package.rds



Thursday, September 15, 2011

R - Rattle rattle gtkin asCairoDevice(da)

Error:
Error in asCairoDevice(da) : Grafik-API Version passt nicht 
or:  
Error in asCairoDevice(da) : Graphics API version mismatch


may be the installation of the package cairoDevice failed.
Try to install the libgtk2.0-dev via sudo apt-get install libgtk2.0-dev


R - install rattle (ubuntu/debian)

First add the line:
        deb http://debian.cran.r-project.org/cran2deb/debian-i386 testing/
or this (for 64 bit vers.)
         deb http://debian.cran.r-project.org/cran2deb/debian-amd64 testing/
to /etc/apt/sources.list

than type: sudo apt-get update

than you can finally install the package via:
sudo apt-get install r-cran-rattle