Showing posts with label R-code. Show all posts
Showing posts with label R-code. Show all posts

Monday, February 15, 2010

Thursday, December 3, 2009

likelihood ratio test

> logLik.test
function(fit0,fit)
{
  l0 = logLik(fit0)
  l1 = logLik(fit)
  lratio = abs(as.numeric(l0)-as.numeric(l1))
  df = abs( attr(l1,'df') - attr(l0,'df') )
  pval = 1-pchisq( 2*lratio, df)
  return(pval)
}

log likelihood from lm, glm, polr


> fit = lm(y ~ x)
> logLik(fit)
'log Lik.' -36.4 (df=4)
> attr(logLik(fit),'df')
[1] 4

Monday, October 26, 2009

plots boxplot, stripchart and mean

## CAN BE REPLACED BY qplot in ggplot2 package

qplot(x,y,geom=c("boxplot","jitter")) ## plots boxplot, stripchart and mean boxjitter = function(x,y=NULL,border='gray',jitter=.1,vertical=T,...) { if(!is.null(y)) { boxplot(y~x,border=border,ylim = range(y,na.rm=T),outline=F,...) stripchart(y~x,vertical=vertical,method="jitter",jitter=jitter,add=T) meanvec = tapply(y,x,mean.na) points(1:length(meanvec),meanvec,pch="M",col='blue') } else { x = x[!is.na(x)] boxplot(x,border=border,ylim = range(x),outline=F,...) stripchart(x,vertical=vertical,method="jitter",jitter=jitter,add=T) points(1,mean.na(x),pch="M",col='blue') } }

Thursday, October 22, 2009

calculate number of minor alleles from A G format

num.minal = function(a1,a2)
{
  aall = c(a1,a2)
  tab = table(aall)
  miname = names(tab)[tab==min(tab)]
  apply(cbind(a1==miname,a2==miname),1,sum)  
}

R table, percentages and chi square test like in stata



table.all = function(...)
{
  args = list(...)
  dnn = names(args)
  x = args[[1]]
  y = args[[2]]
  print(table(x,y,dnn=dnn))
  print("percentage by row")
  print( round( prop.table(table(x,y,dnn=dnn),1) * 100 ) )
  print("percentage by col")
  print( round( prop.table(table(x,y,dnn=dnn),2) * 100 ) )
  print(chisq.test(table(x,y,dnn=dnn)) )
}


Friday, October 2, 2009

order data frames in R

say data has read and prog as colums

data[order(read, prog), ]

this will order first by read then by prog

data[order(-read), ]

- reverses the order

** there can be conflicts if read is a variable outside of the data frame. Use data[order(data[,1]),] assuming read is column 1

from: http://www.ats.ucla.edu/stat/R/faq/sort.htm

Thursday, October 1, 2009

list names from data that contain charname

## list names from data frames that contain charname

grepnames = function(charname,data)
  {
    names(data)[grep(charname,names(data))]
  }

Friday, September 25, 2009

how to get rid of emac's automatic switching of _ into ->

Solution by Paul R. (thanks!)

M-x
set-variable 
ess-S-assign 
"-"  

That's it.


Wednesday, July 11, 2007

reducing margins in R plots

reduce margins
par(mar=c(2,2,1,1)) # default in R is c(5, 4, 4, 2) + 0.1
c(bottom, left, top, right)

Monday, June 18, 2007

theoretical ACF of ARMA

ARMAacf(ar = numeric(0), ma = numeric(0), lag.max = r, pacf = FALSE)

Friday, June 8, 2007

plot in log scale with axis

plot(1:100, log = "y", yaxt = "n") # do not show y axis
axis(2, c(1,10,100)) # draw y axis with required labels 

Tuesday, May 15, 2007

calculating angle from xy coordinates

## from north (-pi,pi) positive toward east
angle.north = function(xx,yy){sign(xx)*(yy<0)*pi + atan(xx/yy)}

## from east (0,2.pi) positive toward north
angle = function(xx,yy){atan(yy/xx) - sign(xx)*sign(yy)*pi*(xx<0)}

Wednesday, February 28, 2007

image with gray scale

imagebw = function(x,y,z,...) image(x,y,z,col=gray((33:64)/64),...)

Friday, November 17, 2006

R equivalent of repmat (matlab)

repmat = function(X,m,n){
##R equivalent of repmat (matlab)
mx = dim(X)[1]
nx = dim(X)[2]
matrix(t(matrix(X,mx,nx*n)),mx*m,nx*n,byrow=T)

Wednesday, December 8, 2004

R legacy help galton

## calling C functions from R
gcc -c kendall.c
R CMD SHLIB -o libkendall.so kendall.o
R
dyn.load('libkendall.so')
xd <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
yd <- c( 2.6,  3.1,  2.5,  5.0,  3.6,  4.0,  5.2,  2.8,  3.8)
.C("kendall",as.double(xd),as.double(yd),as.integer(9),
result=as.integer(1))$result

## header
rm(list=ls())
options(contrasts=c("contr.treatment","contr.poly"))
setwd("/mnt/win_c/my documents/stat/ammonia")

## plot function
plot(fun,-5, 5, ylim = c(-.2, 1))

eval.parent(expr) 
## example eval.parent(media <- 1), used inside
## functions to change global variables
media <<- 1 ## does the same

rep(x,n) replicates x n-times

mtext("texto",side=3,line=.5)

install.packages("fields","/aa/haky/R/Library")
library("fields",lib="/aa/haky/R/Library")
detach(package:fields)

setenv R_LIBS /aa/haky/R/Library (then no need to specify lib=...)

xmat <- matrix(NA,n,n)

#print
dev.print()
dev.copy2eps(file="filename.ps")
#
postscript(file="plot.ps",horizontal=F,heigth=9,width=7)
dev.off()

as.numeric(x<1)

# stop watch
tic<-Sys.time()
....do something here
toc<-Sys.time()
print(toc-tic)

paste(s1,s2,sep="")

system("mail -s something haky@galton.uchicago.edu < output")<

## strip an object's attributes:
attributes(x) <- NULL
x # now just a vector of length 6

kk[names(kk)=="site"] is same as kk$site

unlist(x)

debug(myfunction)
undebug(myfunction)

call R from emacs (ESS): alt-x R

par(mfrow=c(3,2))

Contributors

google