haotu : an open lab notebook

2013/11/28

R Delete Multiple Rows that Contain a Particular Value

Filed under: Manipulate Data in R — Tags: — S @ 01:45
subset(data,!country%in%drops)

data is a dataframe with multiple columns one of which is named country. While drops is the list of values in country whose rows in data will be dropped.

Advertisements

2013/11/27

R replace multiple items in an object :: find and replace

Filed under: Manipulate Data in R, R — Tags: — S @ 02:22

Replace multiple items in an object


recoderFunc <- function(data, oldvalue, newvalue) {

    # convert any factors to characters

    if (is.factor(data))     data     <- as.character(data)
    if (is.factor(oldvalue)) oldvalue <- as.character(oldvalue)
    if (is.factor(newvalue)) newvalue <- as.character(newvalue)

    # create the return vector

    newvec <- data

    # put recoded values into the correct position in the return vector

    for (i in unique(oldvalue)) newvec[data == i] <- newvalue[oldvalue == i]

    newvec

}

http://susanejohnston.wordpress.com/2012/10/01/find-and-replace-in-r-part-2-how-to-recode-many-values-simultaneously/

Load RData from Dropbox in Windows

Filed under: R, Windows 7 — Tags: , , — S @ 00:28

load("C:/Dropbox/SESYNC.Macroevolution.ES.Trees/Taxonomy/FAO/lookup.RData")

Note
1. commonslash (/) instead of backslash (\)
2. Dropbox is at the root.

2013/11/15

Model Averaging Sum of Squares :: Weighted Explained Variance from AICc Model Weights

Filed under: Math and Stats, Model Averaging, R, R Stats — S @ 02:51

I have been using the MuMIn package for model averaging. Below is a function that gives the average sum of squares for each predictor variable across the candidate models (i.e., the sum of squares for the model average).

Download this code as a pdf:

model_averaging_SS

 
 #This code calculates model averaged Sum of Squares as a weighted mean of the SS for each predictor across the candidate set of models.
 #The protocol was that used to average coefficients across the candidate set.
 #See Johnson J.B. & Omland K.S. (2004). Model selection in ecology and evolution. Trends in Ecology & Evolution, 19, 101-108.
 
 require(MuMIn)
 require(car)
 
 getSS<-function(x) { #x is a standard glm model object
 if(length(coef(x))>1){
 An<-Anova(x,test.statistic="F")
 SS<-as.matrix(An$S)
 rownames(SS)<-rownames(An)
 return(t(SS))
 }
 }
 
 getSSs<-function(m.s,data=NULL){
 if(is.null(data)){stop("must include the matching data object for the model selection object, m.s")}
 m.fit<-lapply(lapply(get.models(m.s),formula),glm,data=data)
 
 hold<-matrix(data=0,length(m.fit),length(attr(m.s,"global")$coefficients))
 colnames(hold)<-c("Residuals",names(attr(m.s,"global")$coefficients)[-1])
 for(i in 1:(length(m.fit)))
 {
 SS<-getSS(m.fit[[i]])
 hold[i,colnames(SS)]<-SS
 }
 return(hold)
 }
 
 avgSS<-function(m.s,data=NULL,w.min=0){
 w<-m.s$weight
 hold<-as.matrix(getSSs(m.s,data))
 hold<-hold[w>w.min,]
 w<-w[w>w.min]
 if(is.null(dim(hold))){
 warning(paste("There was only one model with a weight > ",w.min," and returned the SS for this one model.",sep=""))
 return(hold)
 } else {
 return(apply(hold,2,weighted.mean,w=w,na.rm=TRUE))
 }
 }
 
 #EXAMPLE
 CorrNorm <- function(rho = 0.4, X1 = rnorm(100)) { #produces correlated Gaussian variables
 X2<-rnorm(length(X1))
 Z = data.frame(Y=rho*X1+sqrt(1-rho^2)*X2,X1)
 return(Z)
 } #http://r.789695.n4.nabble.com/generate-two-sets-of-random-numbers-that-are-correlated-td3736161.html
 y<-CorrNorm(.8)
 y<-data.frame(y,X2=CorrNorm(.2,X1 = y[,1])[,1])
 lm.D <- lm(Y ~ X1 + X2,data=y) #Global Model
 (m.s<-dredge(lm.D)) #All submodels of the global model (a model.selection object)
 avgSS(m.s,y) #Average Sum of Squares for all the models
 avgSS(m.s,y,w.min=0.5) #Average Sum of Squares for all models with weights greater than w.min
 

2013/11/07

Where to paste google analytics script when using a wordpress theme

Filed under: Analytics, Google, HTML — S @ 05:43

I used the insert headers and footers plugin and pasted my unique script in the header box once this plugin was installed on my site.

The WordPress theme I am using is tiny forge

Google Chart Tools in R

Filed under: Google, Google Docs, Manipulate Data in R, R, R, R graphics — S @ 03:21

There is a nice R package googleVis that integrates google motion charts and chart tools with R

A tutorial here

Create a free website or blog at WordPress.com.