Examples of using chron() and as.Data()

Introduction to String Matching and Manipulation in R using Regular Expressions (regExprTalk.pdf)

About efficiency of R loops

Very simple (and with no explanation) examples of using sapply(), tapply(), apply()

Programming Principals


Bootstrap by ID (fast method that works also for data with multiple records per ID)

bootstrapByIDFastest = function(data, idVar, bootTimes=200){ ## the function bootstraps IDs from the 'data' and returns ## a data.frame with the same number of IDs as in 'data'. ## each bootstraped ID keeps the same records as in 'data' data = data[!is.na(dataidVar),] indPerID = tapply(1:nrow(data), dataidVar, function(x){x}) lengthPerID = sapply(indPerID, length) for (bi in 1:bootTimes){ bootstrappedIDIndices = sample(x=1:length(indPerID), size=length(indPerID), replace=TRUE) newDataLength = sum(lengthPerID[bootstrappedIDIndices], na.rm=TRUE) allBootstrappedIndices = rep(NA, newDataLength) endInd = 0 for (i in 1:length(bootstrappedIDIndices)){ startInd = endInd + 1 endInd = startInd + lengthPerID[bootstrappedIDIndices[i]] - 1 allBootstrappedIndices[startInd:endInd] = indPerID[[bootstrappedIDIndices[i]]] } res = data[allBootstrappedIndices, ] } }

Spearman Correlation and Its Confidence Intervals

spearmanAndCIs = function(x1, x2, confLevel=0.95){ ### calculates Spearman Correlation and its confidence intervals based on Fisher_transformation ### see for references: ### http://www.stat.psu.edu/online/courses/stat509/18_agree/18_agree_print.htm ### http://en.wikipedia.org/wiki/Fisher_transformation ### !!! Note the first website has an error in the formula for CIs: ### instead of t distribution with n-3 degrees of freedom, there should be normal distribution ### Example 1 is from the website: http://www.stat.psu.edu/online/courses/stat509/18_agree/18_agree_print.htm ### x1 = c(23,23,27,27,39,41,45,49,50,53,53,54,56,57,58,58,60,61) ### x2 = c(9.5, 27.9, 7.8, 17.8, 31.4, 25.9, 27.4, 25.2, 31.1, 34.7, 42.0, 29.1, 32.5, 30.3, 33.0, 33.8, 41.1, 34.5) ### Example 2 is from the website: http://www.statsdirect.com/help/nonparametric_methods/spear.htm ### x1 = c(4,10,3,1,9,2,6,7,8,5) ### x2 = c(5,8,6,2,10,3,9,4,7,1) if (length(x1)!=length(x2))stop("Lengths of x1 and x2 must be the same") rankx1 = rank(x1) rankx2 = rank(x2) n = length(x1) nThing = n*((n+1)/2)^2 rho = (sum(rankx1*rankx2) - nThing)/(sqrt(sum(rankx1*rankx1) - nThing) * sqrt(sum(rankx2*rankx2) - nThing)) Zp = .5 * log((1+rho)/(1-rho)) standardError = 1/sqrt(n-3) CIsTrans = Zp + c(qnorm((1-confLevel)/2), qnorm((1+confLevel)/2))*standardError CIs = (exp(2*CIsTrans)-1)/(exp(2*CIsTrans)+1) c(rho, CIs) }

### example 1 (see: http://www.stat.psu.edu/online/courses/stat509/18_agree/18_agree_print.htm) x1 = c(23,23,27,27,39,41,45,49,50,53,53,54,56,57,58,58,60,61) x2 = c(9.5, 27.9, 7.8, 17.8, 31.4, 25.9, 27.4, 25.2, 31.1, 34.7, 42.0, 29.1, 32.5, 30.3, 33.0, 33.8, 41.1, 34.5) spearmanAndCIs(x1, x2)

### example 2 (see: http://www.statsdirect.com/help/nonparametric_methods/spear.htm) x1 = c(4,10,3,1,9,2,6,7,8,5) x2 = c(5,8,6,2,10,3,9,4,7,1) spearmanAndCIs(x1, x2)

Random examples of efficient implementation of certain problems
  1. How time performance differs depending on implementation

Topic attachments
I Attachment Action Size Date Who Comment
apply.pdfpdf apply.pdf manage 100.4 K 24 Aug 2012 - 10:28 SvetlanaEden  
handouts.pdfpdf handouts.pdf manage 1878.5 K 04 Jun 2012 - 10:25 SvetlanaEden Talk about efficiency in R loops
regExprTalk.pdfpdf regExprTalk.pdf manage 107.4 K 06 Mar 2007 - 14:48 SvetlanaEden  
Topic revision: r11 - 24 Aug 2012, SvetlanaEden
 

This site is powered by FoswikiCopyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback