I am trying to pass an equation as an arguement to a function, which seems pretty straightforward given lazy evaluation. I constructed the following little test to make sure I sort of knew what I was doing: go <- function(X,eq) { C <<- X*2; d <<- eq} a _ rnorm(100) go(10,eq=a[.5*length(a)]+C+1)> C[1] 20> d[1] 21.10551> a[.5*length(a)][1] 0.1055086 Everything looks good - (BTW seems like a really powerful feature of R) Now on to my code of interest, which doesn't work (shown below). rm(list=ls()) load("datasets/citycodes") increments _ seq(5000,100000,by=5000) makeindex <- function(Y,eq) { losers _ 0; levels_0 index _ 0; location _ 0; cutoff _0; totalapt_0; houses_0 for (C in city[1:2]) { counter _ 0 if (file.exists(paste("datasets/2000income/",C,sep=""))) { load(paste("datasets/2000income/",C,sep="")) counter _ counter+1} if (file.exists(paste("datasets/rents/2000",toupper(C),sep=""))) { load(paste("datasets/rents/2000",toupper(C),sep="")) counter _ counter+1} if (counter== 2) { totrent _ 0 totrent _ rep(rent,units) totrent _ totrent[!is.na(totrent)] income2000 _ sort(income2000) print(C) print(length(income2000)) # for debugging - notice this works fine print(ls()) # for debugging - notice this works fine level _ eq # FAILURE POINT print(level) maxrent _ level/12*Y tot _ length(totrent[totrent<=maxrent])/length(totrent) house _ length(income2000[income2000<=level])/length(income2000) location _ c(location,C) index _ c(index,tot) cutoff _ c(cutoff,maxrent) totalapt _ c(totalapt,length(totrent)) levels _ c(levels,level) houses _ c(houses,house) } if (counter<2) {losers _ cat(losers,C)} } overall _ data.frame(location,levels,index,houses,cutoff,totalapt) overall <<- overall[-1,] print(overall) losers <<- losers[-1] rm("overall","losers","level","maxrent") } makeindex(.35,eq=(income2000[.33*length(income2000)])) *************** GIVES ********************> source("finalindex.R")[1] "At" [1] 1459919 [1] "C" "counter" "cutoff" "eq" "houses" [6] "income2000" "index" "levels" "location" "losers" [11] "rent" "totalapt" "totrent" "units" "Y" [16] "yr" Error: Object "income2000" not found Ummm. I see it in the list which immediately preceded it. Please notice that I put a ls() in this function just prior to the calling of eq so as to verify that income2000 is indeed "found". while, makeindex(.35,eq=(20000*.9)) works fine. This smells like a "I am an idiot" sort of problem. I must be missing something simple. Thanks for any help - BTW sorry for the long post, trying to give sufficient data. ========================================Michaell Taylor, PhD -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear R-Users, I was wondering if anybody out there has written R/S code or knows of a library for doing randomization tests (for example, permutating group assignments to find a p-value that does not require making a normality assumption). I looked through the available functions and the packages, but did not find anything applicable. As an example of what I am referring to, please see the rudimentary function below, which should provide an approximate p-value for a 2-independent samples scenario (this is only an approximation, since the function does not generate all possible combinations, but instead just generate random permutations; but with a large number of permuts, the error should be quite small). If some similar code is already available, I would like to hear about it. Thanks! ------------- ran2ind <- function(x, y, permuts = 1000) { nx <- length(x) ny <- length(y) ntot <- nx + ny xy <- c(x, y) truet <- ( mean(x) - mean(y) ) / ( sqrt( ( (nx-1)*var(x) + (ny-1)*var(y) ) / (nx + ny - 2) ) * sqrt( 1/nx + 1/ny ) ) tstats <- matrix(NA, 1, permuts) for (i in 1:permuts) { permute <- sample(c(1:ntot), ntot, replace=F) group1 <- xy[permute[1:nx]] group2 <- xy[permute[(nx+1):ntot]] tstats[1,i] <- ( mean(group1) - mean(group2) ) / ( sqrt( ( (nx-1)*var(group1) + (ny-1)*var(group2) ) / (nx + ny - 2) ) * sqrt( 1/nx + 1/ny ) ) } list(tvalues=tstats, truet=truet, pvalue=mean( abs(tstats) >= abs(truet) ) ) } --------------------------------------------------------------------- Wolfgang Viechtbauer wviechtb at s.psych.uiuc.edu Department of Quantitative Psychology viechtba at students.uiuc.edu University of Illinois, Champaign-Urbana "Your mouse has moved. Room 409, Psychology Building Windows must be restarted 603 E. Daniel Street for the change to take Champaign, IL 61820 effect. Reboot now? [OK]" -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Michaell Taylor <pols1oh at bestweb.net> writes:> rm(list=ls()) > load("datasets/citycodes") > increments _ seq(5000,100000,by=5000) > makeindex <- function(Y,eq) { > losers _ 0; levels_0 > index _ 0; location _ 0; cutoff _0; totalapt_0; houses_0 > for (C in city[1:2]) { > counter _ 0 > if (file.exists(paste("datasets/2000income/",C,sep=""))) { > load(paste("datasets/2000income/",C,sep="")) > counter _ counter+1} > if (file.exists(paste("datasets/rents/2000",toupper(C),sep=""))) { > load(paste("datasets/rents/2000",toupper(C),sep="")) > counter _ counter+1} > if (counter== 2) { > totrent _ 0 > totrent _ rep(rent,units) > totrent _ totrent[!is.na(totrent)] > income2000 _ sort(income2000) > print(C) > print(length(income2000)) # for debugging - notice this works fine > print(ls()) # for debugging - notice this works fine > level _ eq # FAILURE POINT[snip]> makeindex(.35,eq=(income2000[.33*length(income2000)]))...> This smells like a "I am an idiot" sort of problem. I must be missing > something simple.Yes. Arguments are evaluated *in the caller environment*. If you type base <- 2 log(base) then you don't get log(exp(1)) even though the log function has a local variable called base. I assume that income2000 is defined by the load(...) constructs, and thus are only available in the evaluation environment of the function. If you really want to pass an expression to be evaluated internally in the function, try level <- eval(eq) and call it with an explicitly quoted argument, like this makeindex(.35,eq=quote(income2000[.33*length(income2000)])) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
yOn Tue, 27 Nov 2001, Wolfgang Viechtbauer wrote:> Dear R-Users, > > I was wondering if anybody out there has written R/S code or knows of a > library for doing randomization tests (for example, permutating group > assignments to find a p-value that does not require making a normality > assumption). I looked through the available functions and the packages, > but did not find anything applicable. As an example of what I am > referring to, please see the rudimentary function below, which should > provide an approximate p-value for a 2-independent samples scenario > (this is only an approximation, since the function does not generate all > possible combinations, but instead just generate random permutations; > but with a large number of permuts, the error should be quite small). If > some similar code is already available, I would like to hear about it. > Thanks!See MASS (the book) for ways to vectorize this problem (both all and random permutations), with code. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Apparently Analagous Threads
- Word missing after stemmed with Norwegian in Search::Xapian::TermGenerator
- Re: Bangdiwala
- reading row vectors from file
- [PATCH] virtio-net: Reporting traffic queue distribution statistics through ethtool
- [PATCH] virtio-net: Reporting traffic queue distribution statistics through ethtool