Re: [R] Prediction using ARCH
Preetam, You are more likely to want garch than arch. These models are data-hungry, so I'm sceptical that a model with CPI is going to be very good. See for instance: www.portfolioprobe.com/2012/09/20/garch-estimation-on-impossibly-long-series/ This question is really more appropriate for r-sig-finance (you need to subscribe before you can post). Pat On 15/11/2014 10:23, Preetam Pal wrote: Hi, I have two variables, FTSE100 and CPI . Call them Y and X respectively. I want to fit an ARCH(1) to model Y on X. I also intend to predict the values of Y for future (given) values of X. How can I use R for such prediction? Another question is: is there a way I can call an R function which would return me the optimal p for the ARCH(p) model? I have attached the Excel data in case it is required. Thanks, Preetam __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Ifelse statement on a factor level data frame
I believe you are in Circle 8.2.7 of The R Inferno. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 28/09/2014 05:49, Kate Ignatius wrote: Quick question: I am running the following code on some variables that are factors: dbpmn$IID1new - ifelse(as.character(dbpmn[,2]) == as.character(dbpmn[,(21)]), dbpmn[,20], '') Instead of returning some value it gives me this: c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)) Playing around with the code, gives me some kind of variation to it. Is there some way to get me what I want. The variable that its suppose to give back is a bunch of sampleIDs. Thanks! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] 21 R navigation tools
Peter and Po Su, thanks. For those who haven't read it, it attempts three things: * provide an overview of the world of R (from the point of view of an R session) * tell of ways to find objects and information * push you towards organizing your attack on a problem Pat On 27/08/2014 14:03, peter dalgaard wrote: C'mon! Already now, Google gives a handful of links to Twitter or R-help or r-bloggers or other places that talk _about_ the write-up. It's http://www.burns-stat.com/r-navigation-tools -pd On 27 Aug 2014, at 09:01 , PO SU rhelpmaill...@163.com wrote: I think the article from burns-stat is very worth reading , You can google it ,and hope you find it useful. -- PO SU mail: desolato...@163.com Majored in Statistics from SJTU __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Logical operators and named arguments
On 07/08/2014 07:21, Joshua Wiley wrote: Hi Ryan, It does work, but the *apply family of functions always pass to the first argument, so you can specify e2 = , but not e1 =. For example: sapply(1:3, ``, e2 = 2) [1] FALSE FALSE TRUE That is not true: gt - function(x, y) x y sapply(1:3, gt, y=2) [1] FALSE FALSE TRUE sapply(1:3, gt, x=2) [1] TRUE FALSE FALSE Specifying the first argument(s) in an apply call is a standard way of getting flexibility. I'd hazard to guess that the reason the original version doesn't work is because `` is Primitive. There's speed at the expense of not behaving quite the same as typical functions. Pat From ?sapply 'lapply' returns a list of the same length as 'X', each element of which is the result of applying 'FUN' to the corresponding element of 'X'. so `` is applied to each element of 1:3 ``(1, ...) ``(2, ...) ``(3, ...) and if e2 is specified than that is passed ``(1, 2) ``(2, 2) ``(3, 2) Further, see ?Ops If the members of this group are called as functions, any argument names are removed to ensure that positional matching is always used. and you can see this at work: ``(e1 = 1, e2 = 2) [1] FALSE ``(e2 = 1, e1 = 2) [1] FALSE If you want to the flexibility to specify which argument the elements of X should be *applied to, use a wrapper: sapply(1:3, function(x) ``(x, 2)) [1] FALSE FALSE TRUE sapply(1:3, function(x) ``(2, x)) [1] TRUE FALSE FALSE HTH, Josh On Thu, Aug 7, 2014 at 2:20 PM, Ryan rec...@bwh.harvard.edu wrote: Hi, I'm wondering why calling with named arguments doesn't work as expected: args() function (e1, e2) NULL sapply(c(1,2,3), ``, e2=0) [1] TRUE TRUE TRUE sapply(c(1,2,3), ``, e1=0) [1] TRUE TRUE TRUE Shouldn't the latter be FALSE? Thanks for any help, Ryan The information in this e-mail is intended only for th...{{dropped:23}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] incorrect correlation coefficients
If the argument to the function you give in the 'by' call were 'z' instead of 'x', then it would have been easier to see what was wrong. You are using 'xx' inside the function rather than 'x'. I think you want something like: function(z) {cor(z$Data1.MEAN, z$Data2.MEAN)} Pat On 27/07/2014 08:04, Mateusz Kędzior wrote: Hello all, I strongly believe, that my issue is quite easy to resolve. However, I have no idea how to find/debug what is wrong and I would blame myself for insufficient knowledge about data tables and some useful functions in R! as apply, sapply, lapply. My question is as follows: I would like to display correlation coefficients in a table (ideally - with p-value, but below there's only Pearson correlation coefficients). However, my code produces exactly the same values for each period (so something is obviously wrong). Could you give me any advice: #first of all, I read my data table from CSV file: imported - read.table (file=/home/someone/data_for_R.csv, header=TRUE, sep='\t', quote='\'', dec=',', fill=FALSE, comment.char=#, na.strings = NA, nrows = -1, skip = 0, check.names = TRUE, strip.white = FALSE, blank.lines.skip = TRUE) # Typing: class(imported[[Period]]) produces: # [1] factor #Typing: levels(imported[[Period]]) produces: # [1] Summer 2010 Summer 2011 Winter 2010 Winter 2011 Winter 2012 xx - imported[c(Period,Data1.MEAN,Data2.MEAN)] result - by(xx, xx$Period, function(x) {cor(xx$Data1.MEAN, xx$Data2.MEAN)}) result.dataframe - as.data.frame(as.matrix(result)) result.dataframe$C - rownames(result) Best regards, Mateusz [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how does a valid subscript can produce an subscript out of bounds error?
I think you are somewhere between Circle 8.2.6 of 'The R Inferno' http://www.burns-stat.com/documents/books/the-r-inferno/ and the basics of subscripting http://www.burns-stat.com/documents/tutorials/impatient-r/more-r-subscript/ Pat On 05/07/2014 15:19, Witold E Wolski wrote: Thank you. You example helped to FIX IT. The problem is I guess somehow related to: class(msexp$pepinfo$transition_group_id) [1] factor and the whole R type conversion , riddle. For subscripts my intuition is: either require integer or do the cast to the rowname type (character). However, it seems that R somehow prefers to cast factors to integers... it seems that %in% casts both vectors to the same type. But to which one? Oh, I guess all this is neatly explained in the R standard ... but websearching for it just returns: standard deviation a frustrated R user. On 5 July 2014 02:18, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 04/07/2014, 6:35 PM, Witold E Wolski wrote: how does a valid subscript (see first 2 lines) can produce an subscript out of bounds error (see line 4)? 1 sum(!rownames(msexp$rt) %in% msexp$pepinfo$transition_group_id) [1] 0 2 sum(!msexp$pepinfo$transition_group_id %in% rownames(msexp$rt)) [1] 0 3 class(msexp$rt) [1] matrix 4 msexp$rt = as.matrix(msexp$rt[msexp$pepinfo$transition_group_id,]) Error in msexp$rt[msexp$pepinfo$transition_group_id, ] : subscript out of bounds x - matrix(1,1,1) rownames(x) - colnames(x) - 23 23 %in% rownames(x) [1] TRUE x[23,] Error in x[23, ] : subscript out of bounds -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Discrete and Continuous variables
Perhaps something involving: sapply(dataFrame, function(z) length(unique(z))) Pat On 17/05/2014 17:31, a...@dpi.inpe.br wrote: Hello everybody! I have a data frame with discrete and continuous variables something like this: 011.462707165309.0171912.57041000. 101.552373295559.0171809.01701000. 011.195954445809.0171750.1000. 220.964678766059.0171707.10681000. 101.469273923250.559.0170750. 201.652678853500.707.1068750. Discrete variables should be specified as factor as.factor() and continuos variables as numeric as.numeric(). The problem is: The users that will enter with the variables. Is it possible identify which input variable are continuous or discrete? Thank guys! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Construction of Optimal Designs in the Language R
The Experimental Design task view seems like a reasonable place to start: http://cran.r-project.org/web/views/ExperimentalDesign.html Pat On 03/05/2014 08:27, kabir opeyemi wrote: Dear All, Please can someone help advise on procedures for construction of optimal designs and estimation of parameters in the language R? Guide on useful R libraries, codes and books would be appreciated. Kabir Olorede, Department of Statistics and Mathematical Sciences, Kwara State University, Malete, Nigeria. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Inquiry
One place to look is: http://www.burns-stat.com/documents/tutorials/impatient-r/ This gives basic information on using R, and if that doesn't suffice, gives some hints about asking questions. Welcome to the R world. Pat On 13/02/2014 02:01, Kei_Takeuchi wrote: Dear R users, Hello, this is Kei Takeuchi in Japan. I have started to use R(windows, version3.0.2) and have a problem. I think it is not difficlut but since I am beginner of R, I have no way to solve it. Where can I post my question? Thank you in advance. Kei Takeuchi __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] DanteR problems
On 09/01/2014 10:41, Assa Yeroslaviz wrote: Hi, I am trying to run the DanteR package, but keep getting some problems. Isn't that the expected behavior? -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] DanteR problems
On 09/01/2014 11:18, Assa Yeroslaviz wrote: What's that suppose to mean? Do you always expect problem when you do something? Actually, yes I do always expect problems, but more pertinently it is an oblique reference to 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On Thu, Jan 9, 2014 at 12:11 PM, Patrick Burns pbu...@pburns.seanet.com mailto:pbu...@pburns.seanet.com wrote: On 09/01/2014 10:41, Assa Yeroslaviz wrote: Hi, I am trying to run the DanteR package, but keep getting some problems. Isn't that the expected behavior? -- Patrick Burns pbu...@pburns.seanet.com mailto:pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/__blog http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to apply correctly.
I think you will be okay if you change one line to: defMat-sapply(defData[,-1, drop=FALSE], function(x) breakUpFun(freq, x)) In your example that doesn't work you are ending up with a vector rather than a one column data frame. Pat On 07/01/2014 17:44, Keith S Weintraub wrote: Folks, # I have the following function: breakByFreq-function(freq, defData) { breakUpFun-function(freq, defs) { if(freq != 1) { defs-diff(c(0, defs)) defs-cumsum(rep(defs/freq, each = freq)) } defs } defMat-sapply(defData[,-1], function(x) breakUpFun(freq, x)) data.frame(Year = 1:nrow(defMat), defMat) } # And this data (year column and then 2 columns of data): dum-structure(list(Year = 1:10, c1 = c(0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1), c2 = c(0.02, 0.04, 0.06, 0.08, 0.1, 0.12, 0.14, 0.16, 0.18, 0.2)), .Names = c(Year, c1, c2), row.names = c(NA, -10L), class = data.frame) # This works: breakByFreq(1, dum) # This doesn't: breakByFreq(1, dum[,-3]) # How do I use and choose the appropriate apply function to make this work when there is one and or more columns to be processed. Thanks for your time, KW -- __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] error in ca.jo
There is a fundamental problem with your code, and there is the problem that you have (sort of) identified. The fundamental problem is that you are only going to get the results of the last call to 'ca.jo' that is done -- assuming it were to run. You presumably want to save some information from each of the computations. You can get the loops to run even when you run into an error with some combinations by using 'try' or 'tryCatch'. There is an example in Circle 8.3.13 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ If you have a question related to the actual function as opposed to general problems with R, then the r-sig-finance mailing list would be appropriate (you need to subscribe before posting). I'm not sure if this is enough of a hint for you or not. If not, then trying to formulate a more explicit question might help. (There are some suggestions in Circle 9 of 'The R Inferno'.) Pat On 23/12/2013 17:07, mamush bukana wrote: Dear all, I fit co-integration function between two integrated variables(y1 and y2) over different grid points: for(i in 1:N1){ for(j in 1:N2){ co-ca.jo(data.frame(cbind(y2[i,j,],y1[i,j,])),type=trace, K=2, spec=transitory,ecdet=const,season=NULL,dumvar=NULL) }} I have already extracted grid points with integrated time series. However, when I run the above function, there happens an error Error in solve.default(t(V) %*% SKK %*% V) : system is computationally singular: reciprocal condition number = 1.10221e-35 May you suggest me how to fix this problem please? Thanks in advance [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Strange subvector output -- x[n] != x[1:n][n]
You've found an interesting corner of Circle 1 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ The issue is that your 'n' in the final case is slightly less than 11. So: n [1] 11 as.integer(n) [1] 10 1:n [1] 1 2 3 4 5 6 7 8 9 10 11 The mystery to me is why `:` thinks it is doing an integer sequence but ends in 11 rather than 10. Most people are likely to think the mystery is why as.integer(n) is 10. The reason is that coercion to integer is truncation (except if the number is really close to the integer farther from 0). Why that and not round? Well, just because. (Actually probably speed back in the day when it could matter.) Pat On 20/12/2013 04:33, Gewart wrote: Hi, Can anyone explain what is going on...!? For a vector x=seq(min,max,0.01), when generating sub-vector a based on a starting value st, things go as expected as long as st is not too close to the beginning of x. For example, if x starts at -5 and increments by 0.01, whenever I try to generate the sub-vector a (as below) with a starting value of 0.49 or less it does not generate the expected output: The initial value of a is wrong. Thanks in advance for any clarity you can shed. Gary ...(please see two versions of code below) #THIS WORKS...(st -4.9) min = -5; max = 1; x=seq(min,max,0.01) st= -4.8 ; end= 0 a=x[((st-min)/0.01+1):((end-min)/0.01+1)] n=(st-min)/0.01+1 #compare a[1:10]; c(x[n:(n+9)]) #test... n x[1:n]; x[n] ### x[n]== x[1:n][n] ; As expected ## # BUT THIS IS WEIRD!!...(st = -4.9) st= -4.90 ; end= 0 ### - BUG in generation of a!! a=x[((st-min)/0.01+1):((end-min)/0.01+1)]; n=(st-min)/0.01+1 #compare a[1:10]; c(x[n:(n+9)]) #test n x[1:n]; x[n] ### NOW x[n] != x[1:n][n] !!?? What is going on!? -- View this message in context: http://r.789695.n4.nabble.com/Strange-subvector-output-x-n-x-1-n-n-tp4682526.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Question about compatibility
If you are anything like me, then the main thing that you could do to win the game is to type the name of the file correctly. Given that is a near impossibility for me, when I'm on Windows I use 'file.choose' to get the correct name. Your command would look like: example=read.table(file=file.choose(), header=TRUE,sep=,) The other likely problem is that you and R are not agreeing on what the working directory is. You can see what R thinks it is by typing: getwd() You can change what it thinks it is with the 'setwd' function (or via the menu -- the file menu in Rgui or session menu in RStudio). Welcome to the R world. Pat On 23/11/2013 16:49, Juan Manuel Reyes S wrote: Dear R- project I am beginning to work in R. When I was trying to read data for external files with command read.table in R, R was reporting me: example=read.table(file=example.text, header=TRUE,sep=,) Error in file(file, rt) : It is not posible to open connection Además: Mensajes de aviso perdidos In file(file, rt) : It is not posible to open the file 'example.text': No such file or directory I am using office 365 and my computer has Windows 8. What can I do? Thank you members of R- project Juan Manuel [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] data manipulation
I think a list is the wrong structure, a vector would be better since you can use 'match': # transform data structure: neutralVec - unlist(neutral_classes) names(neutralVec) - names(neutral_classes[rep(1:length(neutral_classes), sapply(neutral_classes, length))] # get one or more results with 'match': names(neutralVec[match(c(50, 20, 10, -4), neutralVec)]) # result: # [1] B D D NA Pat On 22/11/2013 16:58, philippe massicotte wrote: Hi everyone. I have a list like this: neutral_classes = list(A = 71:100, B = 46:70, C = 21:45, D = 0:20) and I'm trying to return the letter of the named vector for with an integer belong. For example, B if I use the value 50. Any help would be greatly appreciated. Regards,Phil [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] data manipulation
The final ) went missing in the command starting 'names(neutralVec) - '. On 22/11/2013 17:51, Patrick Burns wrote: I think a list is the wrong structure, a vector would be better since you can use 'match': # transform data structure: neutralVec - unlist(neutral_classes) names(neutralVec) - names(neutral_classes[rep(1:length(neutral_classes), sapply(neutral_classes, length))] # get one or more results with 'match': names(neutralVec[match(c(50, 20, 10, -4), neutralVec)]) # result: # [1] B D D NA Pat On 22/11/2013 16:58, philippe massicotte wrote: Hi everyone. I have a list like this: neutral_classes = list(A = 71:100, B = 46:70, C = 21:45, D = 0:20) and I'm trying to return the letter of the named vector for with an integer belong. For example, B if I use the value 50. Any help would be greatly appreciated. Regards,Phil [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] repeating values in an index two by two
f1 function(x) { one - matrix(1:x, nrow=2) as.vector(rbind(one, one)) } environment: 0x0daaf1c0 f1(8) [1] 1 2 1 2 3 4 3 4 5 6 5 6 7 8 7 8 Pat On 11/11/2013 12:11, Federico Calboli wrote: Hi All, I am trying to create an index that returns something like 1,2,1,2,3,4,3,4,5,6,5,6,7,8,7,8 and so on and so forth until a predetermined value (which is obviously even). I am trying very hard to avoid for loops or for loops front ends. I'd be obliged if anybody could offer a suggestion. BW F __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Newbie Question: Repeatable Tasks
Breaking up the task is an excellent way of thinking. John's solution is probably simplest in the short run but not necessarily best in the long run. How I'd do it is write a function for each of your three tasks and then have a script to call the functions. There would be two source files: one with the function definitions and one with the script. The advantage of functions is that they tend to encourage breaking up tasks, and they are more likely to be useful in more than one instance. They help achieve long-term laziness. Pat On 04/11/2013 13:09, John Kane wrote: Just type the commands in the Source window of RStudioo, debug, save as a .r file and source it. I don't see any particular reason to have three scripts once everything is running correctly but you may find it useful. John Kane Kingston ON Canada -Original Message- From: csvirt...@gmx.de Sent: Mon, 4 Nov 2013 09:34:54 +0100 To: r-help@r-project.org Subject: [R] Newbie Question: Repeatable Tasks Hello mailing list, I am new to R using RStudio in Windows and I just want to have repeated tasks each day but could not find an answer to that reading a lot of intros, scrolling through even more and reading on S.O. I connect to a database, load data, do calculation, write results to the database and close connection. Can I save what I typed into the console as a script (I just figured how to type into the Source window)? My idea is to have three scripts (if scripts are the right way to do it): one for connection, second for calculation, third to close connection. Would I then call them by source(..R) or is there a click-button-solution? I would really appreciate if someone guides me in the right direction (link, tutorial, example would be sufficient). Thanks for your help Chris [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks orcas on your desktop! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] omitting integer(0) rows from data frame
This is Circle 8.1.13 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 30/10/2013 13:04, Jack Tanner wrote: I'm not sure if this is correct behavior or not, but it seems counterintuitive to me: dat - data.frame(id=1:5, let=letters[1:5]) # A. omits the first row dat[- 1, ] # B. unexpectedly omits ALL rows dat[- integer(0), ] It would be less surprising if there were no rows omitted in the (B) case. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] apply question
Homework? A hint is: ?diff Pat On 27/10/2013 11:37, Andras Farkas wrote: Dear All, please help with the following problem: I have t -seq(0,24,by=6) a -600 g -0.05 b -a*exp(-g*t) I would like to establish a vector called z (for example) based on b where the results are calculated as : z -c(a-b[1],b[1]-b[2],b[2]-b[3],b[3]-b[4],b[4]-b[5]) so the results are: [1] 0.0 155.50907 115.20395 85.34519 63.22527 as always your input is appreciated Andras [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Strange result from single [] extract operator
See Circle 8.1.3 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 28/09/2013 16:18, rafae...@poczta.fm wrote: Hi All, I am using Rx64 3.0.1 on Windows 7 x64, and wanted to get two last rows from dataset. First, I tried library(datasets) data-airquality data[nrow(data)-1:nrow(data),] and received 152 rows sorted desc. Could you explain why it worked this way? I changed the extract line to: data[(nrow(data)-1):nrow(data),] and then I received what I wanted but still am curious about the performance of my previous code. Yours faithfully, Rafał __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Plot lines whose angle and length depict vector quantities
On 27/09/2013 21:01, Bert Gunter wrote: On Fri, Sep 27, 2013 at 12:44 PM, Sarah Goslee sarah.gos...@gmail.com wrote: It's a straightforward trigonometry problem, isn't it? Indeed! ( (r,theta) to (x,y) coordinates ) . So I wonder if this is a homework problem. If so, the OP should note that we try not to do homework here. So that would make it unanimous that everyone here is trying not to do homework. Pat Cheers, Bert On Fri, Sep 27, 2013 at 2:56 PM, Conor Ryan miol...@gmail.com wrote: I am trying to plot points on a map for each ship locations (lat/long), where each point is a line whose angle (degrees) denotes ships heading and whose line length denotes it's speed. Unfortunately arrows(); p.arrows (sfsmisc) and ms.arrows (TeachingDemos) require start and end coordinates but I only have a single coordinate and an angle to work with. Can you suggest any other packages or commands that might allow me to plot this? Alternatively, can anyone suggest a method of making 'angle' a vector quantity when using the arrows function? Any advice would be much appreciated! -- Sarah Goslee http://www.functionaldiversity.org __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Logical indexing not working
This is Circle 8.1.6 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 27/09/2013 19:20, Mariki Zietsman wrote: I have a data frame frugivore.abundance.S1 where some columns are factors and others are numbers.For example these are my independent variables and density is my dependent variable. census-c(1:70)sites-c(1:5)birds-c(1:45) I want to select the data where sites is 1 and birds are 1,23,24 or 29 So I write:fa1-frugivore.abundance.S1attach(fa1)(abund.frug.RN1-fa1[sites==1 birds==c(1,23,24,29),]) This code doesn't print all the data it should for some reason. It seems to not print rows where density has the same value as another row with the same criteria. i.e. if in the original data we have the following then only rows 1 and 3 will be printed, not all of them: census sites birds density1 1 1 0.0032 1 1 0.0033 1 1 0.001 Can anyone help me out with this please? RegardsMariki [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to find interval?
I believe you want to use 'diff' and 'which' as in: diff(which(c(TRUE, 1:10 %in% c(3, 5, 6, 10) ))) [1] 3 2 1 4 Pat On 17/09/2013 11:15, gildororo...@mail-on.us wrote: I can do this: 1:10 %in% c(3, 5, 6, 10) [1] FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE but what I wish to get is: [1] 3 2 1 4 let me explain: 3 # [1:3] ends with TRUE, i.e. FALSE FALSE TRUE 2 # [4:5] ends with TRUE, i.e. FALSE TRUE 1 # [6:6] ends with TRUE, i.e. TRUE 4 # [7:10] ends with TRUE, i.e. FALSE FALSE FALSE TRUE That is, %in% gave me a serial whether or not the element is in a set, the length is equal to the former, i.e. 1:10 But I wish to get a serial of intevals of occurance of the element in the set, the length is equal to the latter i.e. c(3, 5, 6, 10) With ths task of finding the intervals, I found, with googling, a function called findInterval. I did read every line of that manual, and it seems to be for a completely different purpose. Kindly help the poor newbie:) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Trouble with arguments to 'order'
If I understand your problem correctly, you want to use '[[' instead of '$': order(parameters[[ItemColumn]], parameters[[PriceColumn]]) On 28/05/2013 07:06, Barry King wrote: I have an Excel worksheet with 20 rows. Using XLConnect I successfully read the data into 'indata'. In order to sort it on the 'Item' column and the 'Price_Per_Item' column I submit: index - with(indata, order(Item, Price_Per_Item)) sortedData - indata[index, ] The above works fine but now I do not want to name the columns in the R program directly but pass the names of the columns from a parameter file: index - with(indata, order(parameters$ItemColumn, parameters$PriceColumn)) sortedData - indata[index, ] This does not work. Only one row appears in 'sortedData'. I've tried unlisting the two arguments to 'order' but this does not correct the problem. Can anyone suggest a solution to my problem? Your assistance is appreciated. - Barry King -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Unexpected behavior of apply when FUN=sample
This is Circle 8.1.47 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 14/05/2013 09:52, Luca Nanetti wrote: Dear experts, I wanted to signal a peculiar, unexpected behaviour of 'apply'. It is not a bug, it is per spec, but it is so counterintuitive that I thought it could be interesting. I have an array, let's say test, dim=c(7,5). test - array(1:35, dim=c(7, 5)) test [,1] [,2] [,3] [,4] [,5] [1,]18 15 22 29 [2,]29 16 23 30 [3,]3 10 17 24 31 [4,]4 11 18 25 32 [5,]5 12 19 26 33 [6,]6 13 20 27 34 [7,]7 14 21 28 35 I want a new array where the content of the rows (columns) are permuted, differently per row (per column) Let's start with the columns, i.e. the second MARGIN of the array: test.m2 - apply(test, 2, sample) test.m2 [,1] [,2] [,3] [,4] [,5] [1,]1 10 18 23 32 [2,]79 16 25 30 [3,]6 14 17 22 33 [4,]4 11 15 24 34 [5,]2 12 21 28 31 [6,]58 20 26 29 [7,]3 13 19 27 35 perfect. That was exactly what I wanted: the content of each column is shuffled, and differently for each column. However, if I use the same with the rows (MARGIIN = 1), the output is transposed! test.m1 - apply(test, 1, sample) test.m1 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,]12345 13 21 [2,] 22 30 17 18 19 20 35 [3,] 15 23 24 32 26 27 14 [4,] 29 16 31 25 33 34 28 [5,]89 10 11 1267 In other words, I wanted to permute the content of the rows of test, and I expected to see in the output, well, the shuffled rows as rows, not as column! I would respectfully suggest to make this behavior more explicit in the documentation. Kind regards, Luca Nanetti -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] I don't understand the 'order' function
There is a blog post about this: http://www.portfolioprobe.com/2012/07/26/r-inferno-ism-order-is-not-rank/ And proof that it is possible to confuse them even when you know the difference. Pat On 16/04/2013 19:10, Julio Sergio wrote: Julio Sergio juliosergio at gmail.com writes: I thought I've understood the 'order' function, using simple examples like: Thanks to you all!... As Sarah said, what was damaged was my understanding ( ;-) )... and as Duncan said, I was confusing 'order' with 'rank', thanks! Now I understand the 'order' function. -Sergio __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Value at Risk using a volatility model?
There is an example of using the t distribution for VaR in: http://www.portfolioprobe.com/2012/11/19/the-estimation-of-value-at-risk-and-expected-shortfall/ The trick is to know what the variance of the distribution is for a given value of the degrees of freedom. Pat On 06/04/2013 10:54, Stat Tistician wrote: Hi, I want to calculate the Value at Risk with using some distirbutions and a volatility model. I use the following data(http://uploadeasy.net/upload/cdm3n.rar) which are losses (negative returns) of a company of approx. the last 10 years. So I want to calculated the Value at Risk, this is nothing else than the quantile. Since I have losses I consider the right tail of the distribution. Consider a first simple example, I assume the returns to follow a normal distribution with mean zero and a volatility, which is estimated for each day with a volatility model. Let's use a simple volatility model: The empirical standard deviation of the last 10 days. So I calculate the standard deviation of the first ten days and this is my estimate for the 11th day and so on, until the end of my data. So I assume for each day a normal distribution with mean zero and a sigma estimated by the voaltility mdoel. So I use this estimated sigma to calculate the quantile, which gives me the Value at Risk. The code would be: volatility-0 quantile-0 for(i in 11:length(dat)){ volatility[i]-sd(dat[(i-10):(i-1)]) } for(i in 1:length(dat)){ quantile[i]-qnorm(0.975,mean=0,sd=volatility[i]) } # the first quantile value is the VaR for the 11th date #plot the volatility plot(c(1:length(volatility)),volatility,type=l) #add VaR lines(quantile,type=l,col=red) So in this case I understand everything and I can implement this. But now comes my problem: I want to use a t-distribution with paramters mu, nu and beta or even a generalized hyperbolic distribution. So in this case, I don't know how to plug in the estimates for sigma, since there is no sigma in the paramters? How can I implement the volatility model and e.g. the generalized hyperbolic distribution in this case to calculate the Value at Risk? Thanks [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] E-learning environment for R
Does 'Try R' suit your requirements? http://tryr.codeschool.com/ Pat On 28/03/2013 07:05, Pekka Pere wrote: Hello, Does an e-learning environment for R (in English) exist? I would like to point out to students a way to learn R if they have missed the course on R. Pekka Pere University Lecturer University of Helsinki __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] nested 'while' loops
Your immediate problem is that 'y' is not reset to 1. Easier code to write would be to use 'for' loops rather than 'while' loops. Better still would be to use neither if possible. I suspect that you are in Circle 3 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 25/03/2013 17:43, Sahana Srinivasan wrote: Hi everyone, I'm using the following code to go over every element of a data frame (row wise). The problem I am facing is that the outer 'x' variable is not incrementing itself, thus, only one row of values is obtained, and the program does not proceed to the next row. This is the code: while(x=coln) { while(y=rown) { n-as.numeric(df[[y]][x]); if(n0) { lim-(n-1); S-100; (...) } opdf[[y]][x]-sum; } y-y+1; } x-x+1; } Here is a sample of the input data: GENE A CD EF GH IK LM NP QR ST VW Y 2amt:Amet_0001 29 023 3417 1612 4229 39635 20 1325 3427 323 12 3amt:Amet_0002 19 315 4212 188 3525 437 2613 914 2120 30 0 8 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] !0 + !0 == !0 - !0
Chuck, What an absolutely wonderful R Infernoism. Pat On 18/03/2013 02:17, Charles Berry wrote: Hi all, The subject line is TRUE. Today I accidentally typed rnorm(!0). My old eyes took a minute to focus clearly enough to see what I really typed and why I got '!0' random numbers instead of '10' random normal numbers. If the subject line is disturbing, be assured that this is TRUE: !0^2 == !0 * !0 # ;-) Anyway, I hope the hands who have been around long enough to know FAQ 7.31 by heart and know why precedence of operators matters have a laugh. Best, Chuck __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] rugarch: GARCH with Johnson Su innovations
You want to give returns rather than prices to the garch fitting function. Log returns are more appropriate than simple returns. Actually a negative lambda is what I would expect. Higher volatility (across time) is usually associated with lower returns. The risk premium is more likely a cross-sectional phenomenon than a time series one. Some people are not so believing in risk premia in the first place -- see for instance, Eric Falkenstein. This would have been better sent to the r-sig-finance list. Pat On 12/03/2013 12:15, Wyss Patrick wrote: Hey, I'm trying to implement a GARCH model with Johnson-Su innovations in order to simulate returns of financial asset. The model should look like this: r_t = alpha + lambda*sqrt(h_t) + sqrt(h_t)*epsilon_t h_t = alpha0 + alpha1*epsilon_(t-1)^2 + beta1 * h_(t-1). Alpha refers to a risk-free return, lambda to the risk-premium. I've implemented it like this: #specification of the model spec = ugarchspec(variance.model = list(model = sGARCH, garchOrder = c(1,1), submodel = NULL, external.regressors = NULL, variance.targeting = FALSE), mean.model = list( armaOrder = c(0,0), include.mean = TRUE, archm = TRUE, archpow = 1, arfima = FALSE, external.regressors = NULL, archex = FALSE), distribution.model = jsu, start.pars = list(), fixed.pars = list()) #fit the model to historical closing price (prices) fit = ugarchfit(data = prices, spec = spec) #save coefficients of the fitted model into 'par' par - coef(fit) m = coef(fit)[mu] lambda = coef(fit)[archm] gamma = coef(fit)[skew] delta = coef(fit)[shape] #GARCH parameter a0 = coef(fit)[omega] a1 = coef(fit)[alpha1] b1 = coef(fit)[beta1] My problem is that I often get negative values for lambda, i.e. for the intended risk-premium. So I'm wondering if I've made a mistake in the implementation, as one would usually expect a positive lambda. And a second question is about the Johnson-Su distribution: Am I right by extracting the Johnson-Su parameters gamma (delta) by the keywords skew (shape)? Many thanks in advance, Patrick [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Unexpected behaviour of apply()
This is nice fodder for 'The R Inferno' -- thanks. As Milan said, 'which' will suffice as the function. Here is a specialized function that only returns a list and is only implemented to work with matrices. It should solve your current dilemma. applyL - function (X, MARGIN, FUN, ...) { stopifnot(length(dim(X)) == 2, length(MARGIN) == 1) FUN - match.fun(FUN) ans - vector(list, dim(X)[MARGIN]) if(MARGIN == 1) { for(i in seq_along(ans)) { ans[[i]] - FUN(X[i,], ...) } } else { for(i in seq_along(ans)) { ans[[i]] - FUN(X[,i], ...) } } names(ans) - dimnames(X)[[MARGIN]] ans } Pat On 08/03/2013 08:29, Pierrick Bruneau wrote: Hello everyone, Considering the following code sample : indexes - function(vec) { vec - which(vec==TRUE) return(vec) } mat - matrix(FALSE, nrow=10, ncol=10) mat[1,3] - mat[3,1] - TRUE Issuing apply(mat, 1, indexes) returns a 10-cell list, as expected. Now if I do: mat[1,3] - mat[3,1] - FALSE apply(mat, 1, indexes) I would expect a 10-cell list with integer(0) in each cell - instead I get integer(0), which wrecks my further process. Is there a simple way to get the result I expect (and the only consistent one, IMHO) ? Thanks by advance for your help, Pierrick Bruneau http://www.bruneau44.com [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Learning the R way – A Wish
On 06/03/2013 07:20, Andrew Hoerner wrote: Dear Patrick-- After the official Core Team's R manuals and the individual function help pages, I have found The R Inferno to be the single most useful piece of documentation when I have gotten stuck with a R problems. It is the only introduction that seems to be aware of the ambiguities present in the official documentation and of some of the ways one can get stuck in traps of misunderstanding. Plus, it is enjoyably witty. When I first started using it, I found it ranged from very useful to pretty frustrating. I did not always understand what the examples you presented were trying to say. It is still true that I occasionally wish for a little more discursive explanatory style, but as time goes by I Actually I find myself sometimes thinking the same thing. Pat find that I am increasingly likely to get the point just from the example. Many thanks, Andrew On Tue, Mar 5, 2013 at 1:46 AM, Patrick Burns pbu...@pburns.seanet.com mailto:pbu...@pburns.seanet.com wrote: Andrew, That sounds like a sensible document you propose. Perhaps I'll do a few blog posts along that vein -- thanks. I presume you know of 'The R Inferno', which does a little of what you want. Pat On 04/03/2013 23:42, andrewH wrote: There is something that I wish I had that I think would help me a lot to be a better R programmer, that I think would probably help many others as well. I put the wish out there in the hopes that someone might think it was worth doing at some point. I wish I had the code of some substantial, widely used package – lm, say – heavily annotated and explained at roughly the level of R knowledge of someone who has completed an intro statistics course using R and picked up some R along the way. The idea is that you would say what the various blocks of code are doing, why the authors chose to do it this way rather than some other way, point out coding techniques that save time or memory or prevent errors relative to alternatives, and generally, to explain what it does and point out and explain as many of the smarter features as possible. Ideally, this would include a description at least at the conceptual level if not at the code level of the major C functions that the package calls, so that you understand at least what is happening at that level, if not the nitty-gritty details of coding. I imagine this as a piece of annotated code, but maybe it could be a video of someone, or some couple of people, scrolling through the code and talking about it. Or maybe something more like a wiki page, with various people contributing explanations for different lines, sections, and practices. I am learning R on my own from books and the internet, and I think I would learn a lot from a chatty line-by-line description of some substantial block of code by someone who really knows what he or she is doing – perhaps with a little feedback from some people who are new about where they get lost in the description. There are a couple of particular things that I personally would hope to get out of this. First, there are lots of instances of good coding practice that I think most people pick up from other programmers or by having individual bits of code explained to them that are pretty hard to get from books and help files. I think this might be a good way to get at them. Second, there are a whole bunch of functions in R that I call meta-programming functions – don’t know if they have a more proper name. These are things that are intended primarily to act on R language objects or to control how R objects are evaluated. They include functions like call, match.call, parse and deparse, deparen, get, envir, substitute, eval, etc. Although I have read the individual documentation for many of these command, and even used most of them, I don’t think I have any fluency with them, or understand well how and when to code with them. I think reading a good-sized hunk of code that uses these functions to do a lot of things that packages often need to do in the best-practice or standard R way, together with comments that describe and explain them would help a lot with that. (There is a good smaller-scale example of this in Friedrich Leisch’s tutorial on creating R packages). These are things I think I probably share with many others. I
Re: [R] R function for estimating historical-VaR
You might have a look at: http://www.portfolioprobe.com/2012/12/17/a-look-at-historical-value-at-risk/ which points to a function for historical VaR. As Nello said, we really need to know what it is that you think doesn't work, before we can help you with what you have. It probably doesn't really matter, but doing a full sort is wasteful compared with what 'quantile' does. If you have further questions and they are finance oriented as opposed to being about R programming, then you should post to R-sig-finance (you have to subscribe before posting). Pat On 04/03/2013 16:25, Blaser Nello wrote: Does it just not work or does it not do the right thing? The reason it doesn't work is that you are writing 'T = length(returns) x_foc = vector(length=n) N = T-(n+1)' on one line instead of using three lines. However, your description of what you want to do also doesn't seem to correspond to the function. Please clarify what exactly you want the function to do. You could also write the current function as follows. VaR_foc - function(returns, value=1000, p=.01, n=300) { N - length(returns)-n-1 op - N*p unlist(lapply(1:n, function(i) {-sort(returns[i:(N+i)])[op]*value})) } Nello Blaser -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of ? ??? Sent: Montag, 4. März 2013 14:07 To: R-help@r-project.org Subject: [R] R function for estimating historical-VaR Hi everyone!! I am new in R and I want to create a simple R function for estimating historical-VaR. In y_IBM returns, there are 2300 observations. For evaluation I take the next 2000 observations, then I abandon the latest 300 observations. Firstly, I use the window which has the fix length and contains the observations from 1 to 2000 to estimate the VaR. At first I take 2000 obs. and reorder these series in ascending order, from smallest return to largest return. Each ordered return is assigned an index value (1, 2, ...). At the 99% confidence level, the daily VaR under historical simulation method equals the return corresponding to the index number calculated as follows: (1-0.99)*2000 (the number of our window) =20. The return corresponding to index 20 is the daily historical simulation VaR. I repeat the first step except the window changes the observations from 2 to 2001. Such a process provides 300 one-step ahead VaR. My function is: VaR_foc - function (returns, value = 1000, p = 0.01, n=251) { T = length(returns) x_foc = vector(length=n) N = T-(n+1) m=sort(returns[1:N]) op = as.integer(N*p) # p % smallest for (i in 2:n) { g= returns[i:(N+i)] ys = sort(g) # sort returns x_foc[[1]] = -m[op]*value # VaR number x_foc[i] = -ys[op]*value } return(x_foc) } VaR_foc (returns=y_IBM) But the fucntion doesn't work, can smbd help me wh [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] if value is in vector, perform this function
I think the command you want is: if(t %in% feed_days) C_A - 1.5 else C_A - 0 Do not confuse `%in%` (which is essentially are the left-hand values in the right-hand vector) with in of the `for` loop. By the way, if(t == TRUE) is redundant -- better is: if(t) Pat On 02/03/2013 23:57, Louise Stevenson wrote: Hi, I'm trying to set up R to run a simulation of two populations in which every 3.5 days, the initial value of one of the populations is reset to 1.5. I'm simulation an experiment we did in which we fed Daphnia populations twice a week with algae, so I want the initial value of the algal population to reset to 1.5 twice a week to simulate that feeding. I've use for loops and if/else loops before but I can't figure out how to syntax if t is in this vector of possible t values, do this command, else, do this command if that makes sense. Here's what I have (and it doesn't work): params = c(1, 0.15, 0.164, 1) init = c(1.5, 0.05) t=seq(1,60, by=0.5) #all time values, experiment ran for 60 days #feeding sequence - every 3.5 days feed_days = seq(1,60,by=3.5) Daphnia - function(t,x,params){ C_D = x[2]; C_A = 0; for(t %in% feed_days){ if t == TRUE { C_A = 1.5 } else{ C_A = 0 }} gamma = params[1]; m_D = params[2]; K_q = params[3]; q_max = params[4]; M_D = m_D * C_D I_A = (C_D * q_max * C_A) / (K_q + C_A) r_D = gamma * I_A return( list(c( - I_A, r_D - M_D ))) } library(deSolve) results - ode(init, t, Daphnia, params, method = lsoda) Let me know if there's any other info that would be helpful and thanks very much for your help! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] if value is in vector, perform this function
I forgot to say: Also do not depend on equality in this situation. You want to test equality with a tolerance. See Circle 1 of 'The R Inferno': http://www.burns-stat.com/documents/books/the-r-inferno/ I also see that 't' is a vector unlike what I was thinking before, thus you want to use 'ifelse': C_A - ifelse(t %in% feed_days, 1.5, 0) except that still leaves out the tolerance. If you are always only going to go by half-days, then the following should work: C_A - ifelse( round(2*t) %in% round(2 * feed_days), 1.5, 0) Pat On 02/03/2013 23:57, Louise Stevenson wrote: Hi, I'm trying to set up R to run a simulation of two populations in which every 3.5 days, the initial value of one of the populations is reset to 1.5. I'm simulation an experiment we did in which we fed Daphnia populations twice a week with algae, so I want the initial value of the algal population to reset to 1.5 twice a week to simulate that feeding. I've use for loops and if/else loops before but I can't figure out how to syntax if t is in this vector of possible t values, do this command, else, do this command if that makes sense. Here's what I have (and it doesn't work): params = c(1, 0.15, 0.164, 1) init = c(1.5, 0.05) t=seq(1,60, by=0.5) #all time values, experiment ran for 60 days #feeding sequence - every 3.5 days feed_days = seq(1,60,by=3.5) Daphnia - function(t,x,params){ C_D = x[2]; C_A = 0; for(t %in% feed_days){ if t == TRUE { C_A = 1.5 } else{ C_A = 0 }} gamma = params[1]; m_D = params[2]; K_q = params[3]; q_max = params[4]; M_D = m_D * C_D I_A = (C_D * q_max * C_A) / (K_q + C_A) r_D = gamma * I_A return( list(c( - I_A, r_D - M_D ))) } library(deSolve) results - ode(init, t, Daphnia, params, method = lsoda) Let me know if there's any other info that would be helpful and thanks very much for your help! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] temp seems ineffective in SANN (optim)
I don't have an answer for your real question, but you might try a different optimizer. The blog post: http://www.portfolioprobe.com/2012/07/23/a-comparison-of-some-heuristic-optimization-methods/ shows SANN to not do very well relative to other heuristic optimizers. That is for one problem, but it could well be true for your problem as well. Pat On 27/02/2013 00:31, Ross Boylan wrote: I am trying to control the behavior of the SANN method in optim (R 2.14.1) via control$temp. In my toy tests it works; in my real use, it doesn't. As far as I can tell my code with different temp values is loaded; I even traced into the function that calls optim and verified temp had the value I had set. Could the fact that I have NaN's coming back from the objective function be a factor? Here are the results I've gotten from 20 iterations with temp varying from 2 to 9. The first column is the value of the objective function, and the rest are the parameter values (the objective function is augmented to leave a trace). The rows represent SANN's different guesses, in sequence. history9 [,1] [,2][,3][,4] [,5][,6] [1,] -3507.346 -4.50 1. 1. 1.000 0.69314718 [2,] -3828.071 -3.942424 0.03090623 0.30739233 1.7062554 -0.01814918 [3,] -4007.624 -3.126794 1.79592189 1.41855332 1.2060574 1.54479512 [4,] NaN -4.064653 -0.25017279 1.30476170 0.2559306 -0.31140650 [5,] -4222.272 -3.058714 -0.93063613 -0.54296159 0.8287307 1.92103676 [6,] NaN -3.833080 1.00721123 1.66564249 0.7923725 -0.04967723 [7,] NaN -5.050322 -0.45545409 0.83209653 1.4976764 -0.47211795 [8,] NaN -3.717588 0.62400594 0.73424007 0.1359730 1.62073131 [9,] NaN -6.078701 0.1219 0.36961894 0.2633589 0.67651053 [10,] NaN -3.404865 2.92992664 1.45204623 0.2020535 1.49936000 [11,] NaN -3.387337 2.17682158 0.06994319 1.1717615 0.68526889 [12,] NaN -4.534316 0.88676089 1.34499190 0.9148238 0.98417597 [13,] NaN -4.445174 1.06230896 1.51960345 0.4651780 1.14127715 [14,] -3784.848 -4.007890 0.77866330 1.01243770 1.1957120 1.33305656 [15,] NaN -3.707500 1.30038651 1.30480610 0.6210218 0.81355299 [16,] -3730.219 -4.155193 0.76779830 1.06686987 1.0546294 1.45601474 [17,] -3524.462 -5.074722 1.21296408 0.59787431 0.9228195 1.07755859 [18,] -3588.086 -5.146427 1.28721218 0.74634447 1.1107613 0.63009540 [19,] -3715.411 -4.501889 0.72491408 0.75046935 0.8476556 1.64229603 [20,] -3711.158 -4.813507 0.88125227 1.10291836 0.1452430 0.07181056 [,7] [,8] [,9][,10] [1,] 0. 0.5493061 -4.50 4.00 [2,] -1.33969887 2.6881171 -5.797714 4.712738 [3,] 1.10373337 1.5164159 -4.666298 4.551507 [4,] 0.36425367 0.5755519 -3.558595 3.84 [5,] -0.77555882 0.4863321 -5.060481 4.987640 [6,] -1.14686363 0.5164433 -4.759286 3.650409 [7,] -0.43179263 1.1326352 -4.611431 3.920483 [8,] 1.67696259 0.8754158 -4.352415 3.095768 [9,] 1.10927659 0.5779504 -4.952128 4.649442 [10,] -0.67478207 2.8174240 -4.704395 2.986569 [11,] 0.45878472 0.6479467 -4.122482 2.934156 [12,] -0.04871212 0.9457826 -4.617438 4.377056 [13,] -0.01321339 0.3833625 -4.591240 4.729049 [14,] -0.49075803 0.3322742 -3.971298 4.357731 [15,] 0.16922427 0.4820518 -4.683029 3.875409 [16,] -0.18047923 -0.4957090 -4.492014 4.317694 [17,] -0.28481705 0.1923373 -4.288773 3.956130 [18,] 0.12102775 -0.2332984 -4.981987 4.301450 [19,] 0.15961575 1.1644561 -4.459003 3.777286 [20,] -0.24130528 0.6126422 -4.075133 3.628426 sum(is.nan(history9[,1])) [1] 10 max(abs(history9-history5), na.rm=TRUE) [1] 9.094947e-13 # historyN has a temp of N BTW the values of the objective function have their sign reversed to make it a maximization problem. Ross Boylan __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] cut a vector in equal parts
There is a discussion of the ways to do this task (which can be trickier than it seems) at: http://www.portfolioprobe.com/2012/12/24/miles-of-iles/ Pat On 26/02/2013 17:39, Martin Batholdy wrote: Hi, I would like to cut a vector of values in parts. Each part should have an equal number of elements. for example: x - (rnorm(500)^2) now I want 5 vectors each with 100 elements. The first vector should include the 100 lowest values of x and so on (so that the fifth vector contains the 100 highest values of x). thanks for any help! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Compare each element of a list to a vector
My attempt similar to Jim's is: which(sapply(datalist, function(z) all(z == x))) However, a safer approach is: which(sapply(datalist, function(z) isTRUE(all.equal(z, x This latter approach avoids Circle 1 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 03/02/2013 18:24, jim holtman wrote: try this: x-c(1,2,3) datalist-list(c(1,2,3),c(2,3,4),c(3,4,5),c(4,5,6)) result - sapply(datalist, function(.vec){ + all(.vec == x) + }) result [1] TRUE FALSE FALSE FALSE On Sun, Feb 3, 2013 at 1:15 PM, mtb...@gmail.com wrote: Hello R-helpers, I have a vector x-c(1,2,3) and a list that contains vectors datalist-list(c(1,2,3),c(2,3,4),c(3,4,5),c(4,5,6)) and I would like to identify those list elements that are identical to x. I tried datalist %in% x [1] FALSE FALSE FALSE FALSE but I am obviously using %in% incorrectly. I also tried messing around with lapply but I can't figure out how to specify the function within lapply. I would appreciate any suggestions you may have. Many thanks! Mark Na [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to use ...
There is now a blog post that attempts to answer the question in the subject line: http://www.burns-stat.com/the-three-dots-construct-in-r/ Pat On 17/01/2013 14:36, Ivan Calandra wrote: Dear users, I'm trying to learn how to use the I have written a function (simplified here) that uses doBy::summaryBy(): # 'dat' is a data.frame from which the aggregation is computed # 'vec_cat' is a integer vector defining which columns of the data.frame should be use on the right side of the formula # 'stat_fun' is the function that will be run to aggregate stat.group - function(dat, vec_cat, stat_fun){ require(doBy) df - summaryBy(as.formula(paste0(.~,paste0(names(dat)[vec_cat],collapse=+))), data=dat, FUN=stat_fun) return(df) } Example, works fine: my_data - structure(list(cat = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c(A, B), class = factor), varnum = c(-0.754816565434373, -1.94101630973709, -0.102461836059522, -0.519952759645808, -1.73772800855664, -1.13939178585609, 0.522356715260142, -0.701428514907824, 1.45197576541159, 0.0844567413828095)), .Names = c(cat, varnum), row.names = c(NA, -10L), class = data.frame) stat.group(dat=my_data, vec_cat=1, stat_fun=mean) Now summaryBy() has an ... argument and I would like to use it. For example, I would like to be able to add the trim argument to my call like this: stat.group(dat=my_data, vec_cat=1, stat_fun=mean, trim=0.2) I know I can do it using this ... but I have no idea how to do it. I've tried to search for it, but a search with ... doesn't yield interesting results! Thank you in advance for your help! Ivan -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] c(), rbind and cbind functions - why type of resulting object is double
One place that talks about what Bill says is: http://www.burns-stat.com/documents/tutorials/impatient-r/more-r-key-objects/more-r-numbers/ Pat On 22/01/2013 17:35, William Dunlap wrote: I was wondering why I don't get an integer vector or and integer matrix with the following code: z - c(1, 2:0, 3, 4:8) typeof(z) [1] double It is because the literals 1 and 3 have type double. Append L to make them literal integers. typeof(c(1L, 2:0, 3L, 4:8)) [1] integer The colon function (:) returns an integer vector if it can do so without giving a numerically incorrect answer. typeof(1.0:3.0) [1] integer typeof(1.5:3.5) [1] double Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Lourdes Peña Castillo Sent: Tuesday, January 22, 2013 9:26 AM To: r-help@r-project.org Subject: [R] c(), rbind and cbind functions - why type of resulting object is double Hello Everyone, I am using R 2.15.0 and I came across this behaviour and I was wondering why I don't get an integer vector or and integer matrix with the following code: z - c(1, 2:0, 3, 4:8) typeof(z) [1] double z - rbind(1, 2:0, 3, 4:8) Warning message: In rbind(1, 2:0, 3, 4:8) : number of columns of result is not a multiple of vector length (arg 2) typeof(z) [1] double z - matrix(c(1, 2:0, 3, 4:8), nrow = 5) typeof(z) [1] double Shouldn't be typeof integer? According to the online help if everything is integer the output should be integer. But if I do this, I get an integer matrix. z - matrix(1:20, nrow = 5) typeof(z) [1] integer Thanks! Lourdes [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Impatient R' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Naming an object after another object...can it be done?
You are thinking that 'names' does something different than it does. What you seem to be after is the deparse-substitute idiom: dat - data.frame(Col1=1:10, Col2=rnorm(10)) myPlotFun - function(x, y) { plot(y ~ x, xlab=deparse(substitute(x)), ylab=deparse(substitute(y))) } myPlotFun(dat$Col1, dat$Col2) Pat On 17/01/2013 18:53, mtb...@gmail.com wrote: Hello R-helpers, I have run the following line of code: x-dat$col and now I would like to assign names(x) to be dat$col (e.g., a character string equal to the column name that I assigned to x). What I am trying to do is to assign columns in my dataframe to new objects called x and y. Then I will use x and y within a new function to make plots with informative axis labels (e.g., dat$col instead of x. So, for example, I would like to plot (y~x,xlab=names(x)) and have dat$col printed in the x-axis label. I can do this all manually, by typing names(x)- dat$col) but I'd like to do it with non-specific code within my function so I don't have to type the variable names manually each time. Many thanks, Mark Na [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Naming an object after another object...can it be done?
If you want the column names but not the data frame name, then you could do: with(dat, myPlotFun(Col1, Col2)) Pat On 17/01/2013 20:07, Patrick Burns wrote: You are thinking that 'names' does something different than it does. What you seem to be after is the deparse-substitute idiom: dat - data.frame(Col1=1:10, Col2=rnorm(10)) myPlotFun - function(x, y) { plot(y ~ x, xlab=deparse(substitute(x)), ylab=deparse(substitute(y))) } myPlotFun(dat$Col1, dat$Col2) Pat On 17/01/2013 18:53, mtb...@gmail.com wrote: Hello R-helpers, I have run the following line of code: x-dat$col and now I would like to assign names(x) to be dat$col (e.g., a character string equal to the column name that I assigned to x). What I am trying to do is to assign columns in my dataframe to new objects called x and y. Then I will use x and y within a new function to make plots with informative axis labels (e.g., dat$col instead of x. So, for example, I would like to plot (y~x,xlab=names(x)) and have dat$col printed in the x-axis label. I can do this all manually, by typing names(x)- dat$col) but I'd like to do it with non-specific code within my function so I don't have to type the variable names manually each time. Many thanks, Mark Na [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] matrix manipulation with its rows
Not a great solution, I don't think, but: kronecker(diag(2), matrix(1:6, 2, byrow=TRUE))[c(1,4),] [,1] [,2] [,3] [,4] [,5] [,6] [1,]123000 [2,]000456 So using a function that does this in 'lapply' should solve the problem you state. I'm guessing the real problem might be more complex. Pat On 16/01/2013 07:59, Kathryn Lord wrote: Dear R users, I have a question about matrix manipulation with its rows. Plz see the simple example below sample - list(matrix(1:6, nr=2,nc=3), matrix(7:12, nr=2,nc=3), matrix(13:18,nr=2,nc=3)) sample [[1]] [,1] [,2] [,3] [1,]135 [2,]246 [[2]] [,1] [,2] [,3] [1,]79 11 [2,]8 10 12 [[3]] [,1] [,2] [,3] [1,] 13 15 17 [2,] 14 16 18 With this list, I'd like to create this below [[1]] [,1] [,2] [,3] [,4] [,5] [,6] [1,]135 000 [2,]000 246 [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [1,]79 11 000 [2,] 0008 10 12 [[3]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 13 15 17 000 [2,] 000 14 16 18 Any suggestion will be greatly appreciated. Regards, Kathryn Lord [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Subset in, not in
Did you try: mm - subset(agr1, subset= !(lmpcrd %in% c(11697,149823,7654))) (Actually the parentheses that I added are not necessary, but they make me feel better.) Pat On 10/01/2013 19:54, ramoss wrote: Hello, I need to subset my dataframe into 2 parts; in: mm - subset(agr1, subset=lmpcrd %in% c(11697,149823,7654)) not in: but where do I stick the ! in the above? I've tried every position. Thanks for your help. -- View this message in context: http://r.789695.n4.nabble.com/Subset-in-not-in-tp4655178.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] aggregate / collapse big data frame efficiently
I'd suggest the 'data.table' package. That is one of the prime uses it was created for. Pat On 25/12/2012 16:34, Martin Batholdy wrote: Hi, I need to aggregate rows of a data.frame by computing the mean for rows with the same factor-level on one factor-variable; here is the sample code: x - data.frame(rep(letters,2), rnorm(52), rnorm(52), rnorm(52)) aggregate(x, list(x[,1]), mean) Now my problem is, that the actual data-set is much bigger (120 rows and approximately 100.000 columns) – and it takes very very long (actually at some point I just stopped it). Is there anything that can be done to make the aggregate routine more efficient? Or is there a different approach that would work faster? Thanks for any suggestions! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Memory filling up while looping
__ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Peter Meißner Workgroup 'Comparative Parliamentary Politics' Department of Politics and Administration University of Konstanz Box 216 78457 Konstanz Germany +49 7531 88 5665 http://www.polver.uni-konstanz.de/sieberer/home/ -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R beginner: matrix algebra
Convenient ways of computing both simple and log returns are at the very end of: http://www.portfolioprobe.com/2012/11/05/an-easy-mistake-with-returns/ Those work whether you have a vector or a matrix. Pat On 17/12/2012 17:16, kevj1980 wrote: Hi, I have an n x m matrix of numerical observations. ie. stock prices I wish to convert the matrix of observations to a matrix of simple returns (by taking the differences between (column) observations.) Can any good soul suggest a function for this? -- View this message in context: http://r.789695.n4.nabble.com/R-beginner-matrix-algebra-tp4653335.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to speed up the for loop by releasing memeory
You are in Circle 2 of 'The R Inferno'. You are wise to want to leave. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf Pat On 15/12/2012 15:10, Yong Wang wrote: Dear list; How can I speed up the run of following code (illustrative) # con-vector(numeric) for (i in 1:limit) { if(matched data for the ith item found) { if(i==1) {con-RowOfMatchedData } else {con-rbind(con,matchedData)} } } # each RowOfMatchedData contains 105 variables, when i runs over 10^7 and the data container con get large enough, the codes get extremely slow, I know this is a working memory problem (2GB only), is there anyway to circumvent this problem without dicing and slicing the data. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Books for fully understanding internal logics on some packages(quantmod, xts, zoo and chron)
One place to look would be the archives of the r-sig-finance list. A blog post with suggestions on how to achieve that is: http://www.portfolioprobe.com/2012/01/19/how-to-search-the-r-sig-finance-archives/ Pat On 27/11/2012 19:41, 박상규 wrote: Hello, I'm very interested in using financial time series data, but I'm a beginner of R programming. I'd like to fully understand internal logics on several time-series related packages such as quantmod, xts, zoo, chron, etc. So, I read some books, 'R Cookbook' and 'Art of R Programming' and another simple tutorials. But I still can't understand grammars of the packages codes. Could you recommend some other books or educational materials about S/R language ? Thanks in advance, [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] I don't know the difference between rank and order
Obviously something that is possible to get wrong even when you know it: http://www.portfolioprobe.com/2012/07/26/r-inferno-ism-order-is-not-rank/ Pat On 21/11/2012 08:13, (Ted Harding) wrote: On 21-Nov-2012 02:57:19 li1127217ye wrote: I don't know the difference between rank and order.For example: x=c(10,30,30,20,10,20) x[rank(x,ties.method=first)] [1] 10 10 20 30 30 20 x[order(x)] [1] 10 10 20 20 30 30 the result is quite different, x[rank(x,ties.method=first)] [1] 10 10 20 30 30 20 It is not sorted,why? It is because rank() gives, for each element of x, the position of that value within the sorted series of all the values in x. This will not, in general, be the same as the index, within x, of the value that should be in that position. Example: x1=c(6,5,4,2,3,1) x1[rank(x1,ties.method=first)] # [1] 1 3 2 5 4 6 rank(x1,ties.method=first) # [1] 6 5 4 2 3 1 So 2 indeed has rank 2, and 3 has rank 3; but what will be returned by x1[rank(x)] will depend on what is in x[2] and x[3] (in this case 5 and 4 respectively). Ted. - E-Mail: (Ted Harding) ted.hard...@wlandres.net Date: 21-Nov-2012 Time: 08:13:23 This message was sent by XFMail __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Problem with if
This is Circle 8.1.30 of 'The R Inferno'. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf The example even uses 3. On 10/11/2012 16:58, David Winsemius wrote: On Nov 10, 2012, at 8:34 AM, Haszun wrote: Why it always gives me a 3? fun=function(x) { + if (x-3) { The above code assigns 3 to x. + return(x) + } else { + if(x2) { + return(x^2-1) + } else { + return(log(x)) + }}} fun(-5) [1] 3 fun(0) [1] 3 fun(10) [1] 3 fun(-10) [1] 3 -- View this message in context: http://r.789695.n4.nabble.com/Problem-with-if-tp4649180.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD Alameda, CA, USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] New Learner
national Age Concerns in Scotland, Northern Ireland and Wales have also merged with Help the Aged in these nations to form three registered charities: Age Scotland, Age NI, Age Cymru. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] A general question: Is language S a component part of R?
There is a bit of history in: http://www.portfolioprobe.com/2012/05/31/inferno-ish-r/ Pat On 05/11/2012 17:09, R. Michael Weylandt wrote: On Mon, Nov 5, 2012 at 4:43 PM, Iurie Malai iurie.ma...@gmail.com wrote: In the Introduction and preliminaries the An Introduction to R manual says about R: ... Among other things it has ... a well developed, simple and effective programming language (Called 'S') ... . Now I'm a little confused. This means that language S is a component part of R? And S is not free? But R is free? Or the mentioned S is only a free implementation of the true S? Can anybody explain this? I want to know. Thank you! 'S' is a language, invented at Bell Labs (http://en.wikipedia.org/wiki/S_(programming_language)) which has two major implementations. S-Plus, which is a commercial product, and R, which you know well. R was originally quite like S/S-Plus, but it's changed over time and diverged aways and now I believe the R README says R is 'not unlike' S. Consider, e.g., Python, which is a language (specified in documentation) with multiple implementations: CPython, PyPy, Jython, IronPython, etc. If R and S-Plus had identical functionality they would be different concrete realizations of the abstract 'S' language, but they're more than slightly different in practice. Not sure if that helps at all Michael __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] optim .C / Crashing on run
That is a symptom of the C/C++ code doing something like using memory beyond the proper range. It's entirely possible to have crashes in some contexts but not others. If you can run the C code under valgrind, that would be the easiest way to find the problem. Pat On 03/11/2012 18:15, Paul Browne wrote: Hello, I am attempting to use optim under the default Nelder-Mead algorithm for model fitting, minimizing a Chi^2 statistic whose value is determined by a .C call to an external shared library compiled from C C++ code. My problem has been that the R session will immediately crash upon starting the simplex run, without it taking a single step. This is strange, as the .C call itself works, is error-free (as far as I can tell!) does not return NAN or Inf under any initial starting parameters that I have tested it with in R. It only ever crashes the R session when the Chi^2 function to be minimized is called from optim, not under any other circumstances. In the interests of reproducibility, I attach R code that reads attached data files attempts a N-M optim run. The required shared library containing the external code (compiled in Ubuntu 12.04 x64 with g++ 4.6.3) is also attached. Calculating an initial Chi^2 value for a starting set of model parameters works, then the R session crashes when the optim call is made. Is there something I'm perhaps doing wrong in the specification of the optim run? Is it inadvisable to use external code with optim? There doesn't seem to be a problem with the external code itself, so I'm very stumped as to the source of the crashes. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] optim .C / Crashing on run
When invoking R, you can add -d valgrind to run it under valgrind. On 04/11/2012 11:35, Paul Browne wrote: It looks like my attached files didn't go through, so I'll put them in a public Dropbox folder instead; optim_rhelp.tar.gz http://dl.dropbox.com/u/1113102/optim_rhelp.tar.gz Thanks, I'll run a compiled binary of the C++ code through Valgrind see what it reports, then perhaps I'll try an Rscript execution of the R code calling the C++ in optim (not sure if Valgrind can process that though!). It does seem to be a memory error of some kind, since occasionally the OS pops up a crash report referencing a segmentation fault after optim crashes the R session. Though it is strange that the code has never crashed from a straight .C call in R, or when run from a compiled C++ binary. - Paul On 4 November 2012 09:35, Patrick Burns pbu...@pburns.seanet.com mailto:pbu...@pburns.seanet.com wrote: That is a symptom of the C/C++ code doing something like using memory beyond the proper range. It's entirely possible to have crashes in some contexts but not others. If you can run the C code under valgrind, that would be the easiest way to find the problem. Pat On 03/11/2012 18:15, Paul Browne wrote: Hello, I am attempting to use optim under the default Nelder-Mead algorithm for model fitting, minimizing a Chi^2 statistic whose value is determined by a .C call to an external shared library compiled from C C++ code. My problem has been that the R session will immediately crash upon starting the simplex run, without it taking a single step. This is strange, as the .C call itself works, is error-free (as far as I can tell!) does not return NAN or Inf under any initial starting parameters that I have tested it with in R. It only ever crashes the R session when the Chi^2 function to be minimized is called from optim, not under any other circumstances. In the interests of reproducibility, I attach R code that reads attached data files attempts a N-M optim run. The required shared library containing the external code (compiled in Ubuntu 12.04 x64 with g++ 4.6.3) is also attached. Calculating an initial Chi^2 value for a starting set of model parameters works, then the R session crashes when the optim call is made. Is there something I'm perhaps doing wrong in the specification of the optim run? Is it inadvisable to use external code with optim? There doesn't seem to be a problem with the external code itself, so I'm very stumped as to the source of the crashes. R-help@r-project.org mailto:R-help@r-project.org mailing list https://stat.ethz.ch/mailman/__listinfo/r-help https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/__posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com mailto:pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/__blog http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Egarch (1,1) with Student t distribution using rugarch
Dheeraj, You need to give us some more hints of what you get and what you don't get. My guess is that what has happened is that the optimization algorithm didn't converge. The R-sig-finance mailing list would be a more appropriate place for this discussion (you have to subscribe before you can post there). Pat On 22/10/2012 09:56, Dheeraj Pandey wrote: Hi I was trying to implement Egarch (1,1) with Student t distribution using rugarch. But I was not getting any value. Following were the commands that I was using: library(rugarch) spec=ugarchspec(variance.model=list(model=eGARCH, garchOrder=c(1,1)), mean.model=list(armaOrder=c(1,1), arfima=FALSE), distribution.model=std) fit=ugarchfit(data=b,spec=spec) sigma(fit) May I know the error that I'm making in implementing the model? Any help with the syntax/commands or any useful content will be appreciated? Dheeraj [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Can anyone help why the errors are coming and rectify it?
This is Circle 8.1.43 of 'The R Inferno'. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf Pat On 17/09/2012 10:47, Sri krishna Devarayalu Balanagu wrote: Hi Everyone, Can anyone help why the errors are coming and rectify it? invalid.ids - c(1,3,5) if (length(invalid.ids)==0) { cat(No Errors found) } else { cat(paste(invalid.ids), sep=\n) } Error: unexpected 'else' in else Error: unexpected '}' in } Thank you in advance Warm Regards Rayalu Notice: The information contained in this electronic mail message is intended only for the use of the designated recipient. This message is privileged and confidential. and the property of GVK BIO or its affiliates and subsidiaries. If the reader of this message is not the intended recipient or an agent responsible for delivering it to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify us immediately by telephone +91-40-6692tel:+91-40-6692 and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies). [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] run EGARCH package on REXCEl
Ank, Welcome to R. I suspect that what you are missing is: require(betategarch) If you don't know it, you may be interested in the blog post: http://www.portfolioprobe.com/2012/07/06/a-practical-introduction-to-garch-modeling/ Also questions about garch are generally best sent to R-sig-finance (you have to subscribe before you can post). Though if I'm correct in my diagnosis, this was the right list for this question. Pat On 05/09/2012 07:34, Ankit Mital wrote: Hi, I have limited experience on R and recently started using REXcel. Although I have been able to run both simple functions (like mean etc) and some complex ones (like Principal Component analysis, PCA) using RExcel, I am facing some problems while running EGARCH model. For this I have downloaded the 'betategarch' package for R to run EGARCH with student t dist. Although the package has been installed properly, whenever I run function, I get an error saying - could not find function tegarch.est. I'd appreciate any help on the above. Regards Ank [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] r data structures
To slightly correct what's been said: In general lists are linear objects, but a list can have dimension. An example is in Circle 8.1.8 of 'The R Inferno'. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf Pat On 16/08/2012 21:50, Schumacher, Jay S wrote: are these correct/accurate/sensible statements: a vector is a one dimensional object. a matrix is a two dimensional object. a list is a one dimensional object. i'm working from this web page: http://www.agr.kuleuven.ac.be/vakken/statisticsbyR/someDataStructures.htm - On Aug 16, 2012, at 11:49 AM, Schumacher, Jay S wrote: hi, i'm trying to understand r data structures. i see that vectors, matrix, factors and arrays have a dimension. there seems to be no mention of dimensionality anywhere for lists or dataframes. can i consider lists and frames to be of fixed dimension 2? About half of what you have deduced is wrong. Matrices, arrays, and dataframes do have dimensions, at least in technical R parlance, namely they have an attribute which can be queried with dim(). By definition matrices and dataframes have 2 dimensions. Arrays and matrices can be redimensioned, but dataframes cannot. Factors, lists, and atomic vectors do not have dimensions, but they do have lengths. An appropriately structured list (one with vectors all the same length) can be coerced to a dataframe with as.data.frame(). -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] The best solver for non-smooth functions?
There is a new blog post that is pertinent to this question: http://www.portfolioprobe.com/2012/07/23/a-comparison-of-some-heuristic-optimization-methods/ Pat On 18/07/2012 21:00, Cren wrote: # Hi all, # consider the following code (please, run it: # it's fully working and requires just few minutes # to finish): require(CreditMetrics) require(clusterGeneration) install.packages(Rdonlp2, repos= c(http://R-Forge.R-project.org;, getOption(repos))) install.packages(Rsolnp2, repos= c(http://R-Forge.R-project.org;, getOption(repos))) require(Rdonlp2) require(Rsolnp) require(Rsolnp2) N - 3 n - 10 r - 0.0025 ead - rep(1/3,3) rc - c(AAA, AA, A, BBB, BB, B, CCC, D) lgd - 0.99 rating - c(BB, BB, BBB) firmnames - c(firm 1, firm 2, firm 3) alpha - 0.99 # One year empirical migration matrix from Standard Poor's website rc - c(AAA, AA, A, BBB, BB, B, CCC, D) M - matrix(c(90.81, 8.33, 0.68, 0.06, 0.08, 0.02, 0.01, 0.01, 0.70, 90.65, 7.79, 0.64, 0.06, 0.13, 0.02, 0.01, 0.09, 2.27, 91.05, 5.52, 0.74, 0.26, 0.01, 0.06, 0.02, 0.33, 5.95, 85.93, 5.30, 1.17, 1.12, 0.18, 0.03, 0.14, 0.67, 7.73, 80.53, 8.84, 1.00, 1.06, 0.01, 0.11, 0.24, 0.43, 6.48, 83.46, 4.07, 5.20, 0.21, 0, 0.22, 1.30, 2.38, 11.24, 64.86, 19.79, 0, 0, 0, 0, 0, 0, 0, 100 )/100, 8, 8, dimnames = list(rc, rc), byrow = TRUE) # Correlation matrix rho - rcorrmatrix(N) ; dimnames(rho) = list(firmnames, firmnames) # Credit Value at Risk cm.CVaR(M, lgd, ead, N, n, r, rho, alpha, rating) # Risk neutral yield rates Y - cm.cs(M, lgd) y - c(Y[match(rating[1],rc)], Y[match(rating[2],rc)], Y[match(rating[3],rc)]) ; y # The function to be minimized sharpe - function(w) { - (t(w) %*% y) / cm.CVaR(M, lgd, ead, N, n, r, rho, alpha, rating) } # The linear constraints constr - function(w) { sum(w) } # Results' matrix (it's empty by now) Results - matrix(NA, nrow = 3, ncol = 4) rownames(Results) - list('donlp2', 'solnp', 'solnp2') colnames(Results) - list('w_1', 'w_2', 'w_3', 'Sharpe') # See the differences between different solvers rho Results[1,1:3] - round(donlp2(fn = sharpe, par = rep(1/N,N), par.lower = rep(0,N), par.upper = rep(1,N), A = t(rep(1,N)), lin.lower = 1, lin.upper = 1)$par, 2) Results[2,1:3] - round(solnp(pars = rep(1/N,N), fun = sharpe, eqfun = constr, eqB = 1, LB = rep(0,N), UB = rep(1,N))$pars, 2) Results[3,1:3] - round(solnp2(par = rep(1/N,N), fun = sharpe, eqfun = constr, eqB = 1, LB = rep(0,N), UB = rep(1,N))$pars, 2) for(i in 1:3) { Results[i,4] - abs(sharpe(Results[i,1:3])) } Results # In fact the sharpe function I previously defined # is not smooth because of the cm.CVaR function. # If you change correlation matrix, ratings or yields # you see how different solvers produce different # parameters estimation. # Then the main issue is: how may I know which is the # best solver at all to deal with non-smooth functions # such as this one? -- View this message in context: http://r.789695.n4.nabble.com/The-best-solver-for-non-smooth-functions-tp4636934.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] complexity of operations in R
It looks to me like the following should do what you want: f2 - function(dotot) array(FALSE, c(dotot, 1)) What am I missing? Pat On 17/07/2012 21:58, Johan Henriksson wrote: thanks for the link! I should read it through. that said, I didn't find any good general solution to the problem so here I post some attempts for general input. maybe someone knows how to speed this up. both my solutions are theoretically O(n) for creating a list of n elements. The function to improve is O(n^2) which should suck tremendously - but the slow execution of R probably blows up the constant factor of the smarter solutions. Array doubling comes close in speed for large lists but it would be great if it could be comparable for smaller lists. One hidden cost I see directly is that allocating a list in R is O(n), not O(1) (or close), since it always fills it with values. Is there a way around this? I guess by using C, one could just malloc() and leave the content undefined - but is there no better way? thanks, /Johan # the function we wish to improve f-function(dotot){ v-matrix(ncol=1,nrow=0) for(i in 1:dotot){ v-rbind(v,FALSE) } return(v) } ## # first attempt: linked lists emptylist - NA addtolist - function(x,prev){ return(list(x,prev)) } g-function(dotot){ v-emptylist for(i in 1:dotot){ v-addtolist(FALSE,v) } return(v) } # second attempt: array doubling emptyexpandlist-list(nelem=0,l=matrix(ncol=1,nrow=0)) addexpandlist-function(x,prev){ if(nrow(prev$l)==prev$nelem){ nextsize-max(nrow(prev$l),1) prev$l-rbind(prev$l,matrix(ncol=1,nrow=nextsize)) } prev$nelem-prev$nelem+1 prev$l[prev$nelem]-x return(prev) } compressexpandlist-function(prev){ return(as.vector(prev$l[1:prev$nelem])) } h-function(dotot){ v-emptyexpandlist for(i in 1:dotot){ v-addexpandlist(FALSE,v) } return(compressexpandlist(v)) } # dotot=10 system.time(f(dotot)) #system.time(g(dotot)) system.time(h(dotot)) On Tue, Jul 17, 2012 at 8:42 PM, Patrick Burns pbu...@pburns.seanet.com mailto:pbu...@pburns.seanet.com wrote: Johan, If you don't know 'The R Inferno', it might help a little. Circle 2 has an example of how to efficiently (relatively speaking) grow an object if you don't know the final length. http://www.burns-stat.com/__pages/Tutor/R_inferno.pdf http://www.burns-stat.com/pages/Tutor/R_inferno.pdf If you gave a simple example of how your code looks now and what you want it to do, then you might get some ideas of how to improve it. Pat On 17/07/2012 12:47, Johan Henriksson wrote: Hello! I am optimizing my code in R and for this I need to know a bit more about the internals. It would help tremendously if someone could link me to a page with O()-complexities of all the operations. In this particular case, I need something like a linked list with O(1) insertLast/First ability. I can't preallocate a vector since I do not know the final size of the list ahead of time. The classic array-doubling trick would give me O(1) amortized time but it's a bit messy. The other alternative I see would be to recursively store lists (elem, (elem, (elem, (..., which I take also would work? But I'd rather go for a standard R solution if there is one! cheers, /Johan -- Patrick Burns pbu...@pburns.seanet.com mailto:pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/__blog http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') -- -- --- Johan Henriksson, PhD Karolinska Institutet Ecobima AB - Custom solutions for life sciences http://www.ecobima.se http://www.ecobima.com http://mahogny.areta.org http://www.endrov.net http://www.endrov.net -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Return
It depends on which return you want. See the R computation section of A tale of two returns. http://www.portfolioprobe.com/2010/10/04/a-tale-of-two-returns/ Pat On 05/07/2012 05:58, Akhil dua wrote: Hello Every one I have data on Stock prices and I want to calculate the return on all the stocks and then replace all the stock prices with the returns can any one tell me how to do My data is in the format given below Date Stock1 Stock2 Stock3 01/01/20001 2 3 01/02/20005 6 7 01/03/20001 2 3 01/04/20005 6 7 Thanks [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Assigning a vector to every element of a list.
b - rep(list(d), length(b)) On 02/07/2012 23:16, Spencer Maynes wrote: I have a vector d of unknown length, and a list b of unknown length. I would like to replace every element of b with d. Simply writing b-d does not work as R tries to fit every element of d to a different element of d, and b-rep(d,length(b)) does not work either as it makes a list of length length(d)*length(b) not a list of length(b). I know how to do this with a for loop, but I feel that there has to be a more efficient way. Any suggestions? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Accessing named members of a list in an array
Your problem is that you are trying to use `$` on an atomic vector rather than a list: a- array(list(NULL),dim=c(2,2)) a[[1,1]] - c(a=2,b=3) a[[1,1]]$a Error in a[[1, 1]]$a : $ operator is invalid for atomic vectors a[[1,1]] a b 2 3 a[[1,1]] - list(a=2,b=3) a[[1,1]]$a [1] 2 a[[1,1]] $a [1] 2 $b [1] 3 From the description of the problem, perhaps it would be easier to just have a 3-dimensional array. Pat On 30/06/2012 14:35, mlell08 wrote: Dear List, I've created a two-dimensional array which shall contain a value and its error, respectively. These two values are concatenated in al list and bear the names sl and sl_err But I can't adress them using the $-notation. a- array(list(NULL),dim=c(2,2)) a[[1,1]]- c(a=2,b=3) a[[1,1]]$a ## Fehler in a[[1, 1]]$a : $ operator is invalid for atomic vectors a[[1,1]][a] # This works however. ## a ## 2 I always thought these two methods of indexing are equal? Is there any way to use the $-Style indexing? Thank you, Moritz -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] incorrect number of subscripts on matrix
Andre, 1) The matrix you created was called 'x.3', not 'x'. I guess this could be an item in 'The R Inferno', perhaps it falls into Circle 8.3.32. 2) You don't need a loop at all: x.3 - matrix(rexp(3000, rate=2/3), nrow=1000) This is Circle 3. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf Pat On 30/06/2012 05:09, andre bedon wrote: Hi, Wondering if anyone could help me out with this error.Im trying to fill a matrix with random numbers taken from an exponential distribution using a loop: x.3-matrix(rep(0,3000),nrow=1000,byrow=T)for(i in 1:1000){x[i,]-rexp(3,rate=2/3)} I get the error message: Error in x[i, ] - rexp(3, rate = 2/3) : incorrect number of subscripts on matrix Any ideas??? Appreciate any thoughts. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Event Studies in R
Bill Alpert and I thought about creating a package from when we worked together on an event study. But our experience was that most of the work was in aligning the data and virtually none of what we did was generalizable. Maybe someone has found a way to make a useful package, but I'm doubtful. Pat On 23/06/2012 13:36, ivan wrote: Dear all I tried finding a package for event studies but unfortunately without success. Does anyone know which package suits best for such an analysis? Thank you in advance. Regards [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R learning
You have already had one response that interpreted learn R as use R to do statistics. I have a rather different interpretation of learn R. An answer to that interpretation (learn the mechanics of the language) is: http://www.burns-stat.com/pages/Tutor/hints_R_begin.html If you search the web, you are sure to find several other interpretations as well. Pick the ones that suit you. Pat On 30/05/2012 12:14, paragkulkarni11 wrote: Hi, What is quickest way to learn R? I am unnecessarily having fear of learning R. rgds Parag Kulkarni Haridwar,India -- View this message in context: http://r.789695.n4.nabble.com/R-learning-tp4631814.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R Memory Issues
You are on a 64-bit machine, but are you using 64-bit R? Are you using memory intensive constructs like those discussed in Circle 2 of 'The R Inferno'? http://www.burns-stat.com/pages/Tutor/R_inferno.pdf Pat On 20/05/2012 17:09, Emiliano Zapata wrote: -- Forwarded message -- From: Emiliano Zapataezapata...@gmail.com Date: Sun, May 20, 2012 at 12:09 PM Subject: To: R-help@r-project.org Hi, I have a 64 bits machine (Windows) with a total of 192GB of physical memory (RAM), and total of 8 CPU. I wanted to ask how can I make R make use of all the memory. I recently ran a script requiring approximately 92 GB of memory to run, and got the massage: cannot allocate memory block of size 2.1 Gb I read on the web that if you increase the memory you have to reinstall R; would that be enough. Could I just increase the memory manually. Take you for any comments, or links on the web. EZ [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] understanding the FUNCTION function
I suspect you are trying to find your way into Circle 6 of 'The R Inferno' but haven't yet got in. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf Pat On 26/04/2012 03:06, michaelyb wrote: Hello, I am trying to understand why the FUNCTION used in several codes, won't create the object after it finishes running the code. For instance, look at the following: Number- function(x) {MyNumberIs-x} When I run Number(5) Everything goes well, except that if I try to call the object MyNumberIs, I won't find it. I understand that this function can assume many parameters, but why won't it create the object? Besides, if I try assing, it won't work either, no matter how I do it. Any advice/explanation? Thankk you very much! -- View this message in context: http://r.789695.n4.nabble.com/understanding-the-FUNCTION-function-tp4588681p4588681.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to remove $ (Dollar sign) from string
Why you need a double backslash is alluded to in Circle 8.1.23 of 'The R Inferno'. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf Pat On 22/04/2012 10:18, Giuseppe Marinelli wrote: In data martedì 10 aprile 2012 13:34:13, Nevil Amos ha scritto: How do I remove a $ character from a string sub() and gsub() with $ or \$ as pattern do not work. sub($,,ABC$DEF) [1] ABC$DEF sub(\$,,ABC$DEF) Error: '\$' is an unrecognized escape in character string starting \$ sub(\$,,ABC$DEF) Error: unexpected input in sub(\ Thanks You just need a double backslash: sub(\\$,,ABC$DEF) [1] ABCDEF __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Can a matrix have 'list' as rows/columns?
That is a fine section of 'The R Inferno' but I don't think it applies to your problem. The answer to your question in the subject line is obviously yes. It happens when the matrix (or more generally any array) is of mode list. A useful example of this is in Circle 8.1.8. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf In your case you can do things like: M[[1,1]] [1] aaa M[[1,2]] [1] bbb M[[2,2]] [1] 0.274498 But not: M[[,2]] Error in M[[, 2]] : invalid subscript type 'symbol' Pat On 17/04/2012 05:13, Worik R wrote: After a lot of processing I get a matrix into M. I expected each row and column to be a vector. But it is a list. R-Inferno says... Arrays (including matrices) can be subscripted with a matrix of positive numbers. The subscripting matrix has as many columns as there are dimensions in the array—so two columns for a matrix. The result is a vector (not an array) containing the selected items. My version of R: version.string R version 2.12.1 (2010-12-16) Here is an example... Qm- c(aaa, bbb, ccc) DF- data.frame(Name=sample(Qm, replace=TRUE, size=22), Value=runif(22), stringsAsFactors=FALSE) M- sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,])}, DF) class(M) [1] matrix class(M[,1]) [1] list class(M[1,]) [1] list M aaa bbb ccc Name aaa bbbccc Value 0.4702648 0.274498 0.5529691 DF Name Value 1 ccc 0.99948920 2 aaa 0.51921281 3 aaa 0.10803943 4 aaa 0.82265847 5 ccc 0.83237260 6 bbb 0.88250933 7 aaa 0.41836131 8 aaa 0.66197290 9 ccc 0.01911771 10 ccc 0.4699 11 bbb 0.35719884 12 ccc 0.86274858 13 bbb 0.57528579 14 aaa 0.12452158 15 aaa 0.44167731 16 aaa 0.11660019 17 ccc 0.55296911 18 aaa 0.12796890 19 bbb 0.44595741 20 bbb 0.93024768 21 aaa 0.47026475 22 bbb 0.27449801 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] no partial matching of argument names after dots argument - why?
Circle 8.1.20 of 'The R Inferno' http://www.burns-stat.com/pages/Tutor/R_inferno.pdf gives the rules for argument matching. When you see them explicitly set out there, perhaps you will see why the matching is as it is. Pat On 05/03/2012 09:24, Mark Heckmann wrote: I noticed that the argument names after the dots argument are not partially matched. foo- function(one, two, ...){ one + two } foo(o=1, t=2) [1] 3 foo- function(one, ..., two){ one + two } foo(o=1, t=2) Fehler in one + two : 'two' fehlt Can someone explain me the reason for this behavior? THX Mark Mark Heckmann Blog: www.markheckmann.de R-Blog: http://ryouready.wordpress.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Global Variable in R
Possible, but not necessarily easier in the long run. See Circle 6 of 'The R Inferno'. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf On 03/03/2012 08:56, Alaios wrote: Dear all, I would like to ask you if there is a way to define a global variable in R? I am having a bunch of function and I think the easier would be to have a global function defined at some point... Would that be possible? Regards Alex [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] What does [[1]] mean?
Here is a page that should help: http://www.burns-stat.com/pages/Tutor/more_R_subscript.html Also Circle 8.1.54 of 'The R Inferno' http://www.burns-stat.com/pages/Tutor/R_inferno.pdf On 26/01/2012 18:27, Ajay Askoolum wrote: I know that [] is used for indexing. I know that [[]] is used for reference to a property of a COM object. I cannot find any explanation of what [[1]] does or, more pertinently, where it should be used. Thank you. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] physical books of 'The R Inferno' and 'S Poetry'
It is now possible to buy paperback copies of 'The R Inferno' and 'S Poetry'. I think it is a bit silly to buy them, but silliness that I won't discourage. See: http://www.portfolioprobe.com/2012/01/12/physical-books-of-the-r-inferno-and-s-poetry/ for links to both free and costly versions, and why you should care. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] [newbie] stack operations, or functions with side effects (or both)
There is a crude implementation of stacks in 'S Poetry' (available on www.burns-stat.com). I haven't looked at it, but I'd guess that code would work in R as well. On 04/01/2012 21:22, Tom Roche wrote: summary: Specifically, how does one do stack/FIFO operations in R? Generally, how does one code functions with side effects in R? details: I have been a coder for years, mostly using C-like semantics (e.g., Java). I am now trying to become a scientist, and to use R, but I don't yet have the sense of good R and R idiom (i.e., expressions that are to R what (e.g.) the Schwartzian transform is to Perl). I have a data-assimilation problem for which I see a solution that wants a stack--or, really, just a pop(...) such that * s- c(1:5) * print(s) [1] 1 2 3 4 5 * pop(s) [1] 1 * print(s) [1] 2 3 4 5 but in fact I get pop(s) Error: could not find function pop and Rseek'ing finds me nothing. When I try to write pop(...) I get pop1- function(vector_arg) { + length(vector_arg) - lv + vector_arg[1] - ret + vector_arg- vector_arg[2:lv] + ret + } pop1(s) [1] 1 print(s) [1] 1 2 3 4 5 i.e., no side effect on the argument pop2- function(vector_arg) { + length(vector_arg) - lv + vector_arg[1] - ret + assign(vector_arg, vector_arg[2:lv]) + return(ret) + } pop2(s) [1] 1 print(s) [1] 1 2 3 4 5 ditto :-( What am I missing? * Is there already a stack API for R (which I would expect)? If so, where? * How to cause the desired side effect to the argument in the code above? TIA, Tom Rochetom_ro...@pobox.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] if statement problem
This is almost Circle 8.1.7 of 'The R Inferno': http://www.burns-stat.com/pages/Tutor/R_inferno.pdf but is making the mistake in the other direction. On 23/12/2011 22:40, reena wrote: Hello, I want to do fisher test for the rows in data file which has value less than 5 otherwise chi square test .The p values from both test should be stored in one resulted file. but there is some problem with bold if statement. I don't know how implement this line properly. x = cbind(obs1,obs2,exp1,exp2) a = matrix(c(0,0,0,0), ncol=2, byrow =TRUE)#matrix with initialized values for (i in 1: length(x[,1])) { *if((x[i,1] || x[i,2] || x[i,3] || x[i,4]) 5)* { a[1,1]- x[i,1]; a[1,2]- x[i,2]; a[2,1]- x[i,3]; a[2,2]- x[i,4]; result- fisher.test(a) write.table(result[[p.value]],file=results.txt, sep=\n, append=TRUE, col.names=FALSE, row.names=FALSE); } else { a[1,1]- x[i,1]; a[1,2]- x[i,2]; a[2,1]- x[i,3]; a[2,2]- x[i,4]; result- chisq.test(a) write.table(result[[p.value]],file=results.txt, sep=\n, append=TRUE, col.names=FALSE, row.names=FALSE);} } Regards R -- View this message in context: http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230026.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to assign a value?
You are basically in R Inferno Circle 8.1.40. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf On 11/12/2011 15:27, Jinsong Zhao wrote: Hi there, I hope to modify values in a vector or matrix in the following code: for (i in 1:9) { assign(paste(a., i, sep = ), 1:i) get(paste(a., i, sep = ))[i] - i+50 } I get the following error message: Error in get(paste(a., i, sep = ))[i] - i + 50 : target of assignment expands to non-language object I have read the FAQ How can I turn a string into a variable?, however, I don't find a way to deal with: get(paste(a., i, sep = ))[i] - i+50 Any suggestions? Thanks in advance! Regards, Jinsong __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] perfectionism
Did you try: z[f] On 25/11/2011 19:23, Jack Tanner wrote: I have a named vector: z- c(1, 2, 3, 2) names(z)- c(a,b,c,b) f- c(b,c) I want to know the index in z of the first occurrence of each of the values in f. One implementation is sapply(f, function(x) which(names(z)==x)[1]) b c 2 3 Is which() smart enough to stop when it finds in z the first occurrence of every value from f, or does it search through all the values in z only to report the first one? Are some more elegant ways of writing this code? Just curious. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] First read (was: Re: Looping and paste)
It's very seldom that I disagree with Bert, but here is one time. I don't think An Introduction to R is a suitable first read for people with little computational experience. Better (I modestly suggest) would be: http://www.burns-stat.com/pages/Tutor/hints_R_begin.html which includes some other references. 'Hints' is imperfect and incomplete but it suffers slightly less from the curse of knowledge than a lot of other R documentation. Pat On 24/11/2011 00:15, Bert Gunter wrote: ... and you can of course do the assignment: Bndy- paste (BndY,to,50+seq_len(BndY), mN, sep = ) An Introduction to R tells you about such fundamentals and should be a first read for anyone learning R. --- Bert On Wed, Nov 23, 2011 at 4:10 PM, Bert Gunterbgun...@gene.com wrote: Don't do this! paste() is vectorized. paste (BndY,to,50+seq_len(BndY), mN, sep = ) Cheers, Bert On Wed, Nov 23, 2011 at 3:31 PM, B77Sbps0...@auburn.edu wrote: out- vector(list) Ylab- for(i in 1:length(BndY)) { out[i]- paste(BndY[i], to ,BndY[i],mN) } Ylab- do.call(c, out) markm0705 wrote Dear R helpers I'm trying to make up some labels for plot from this vector BndY-seq(from = 18900,to= 19700, by = 50) using Ylab-for(i in BndY) {c((paste(i, to ,i+50,mN)))} but the vector created is NULL However if i use for(i in BndY) {print(c(paste(i, to ,i+50,mN)))} I can see the for loop is making the labels I'm looking for but not sure on my error in assigning them to a vector Thanks in advance -- View this message in context: http://r.789695.n4.nabble.com/Looping-and-paste-tp4101892p4102066.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] First read
Bert, Your laziness is well founded -- it is not on CRAN, you have to go all the way over to another website. And thanks for the kind words (even though we Europeans are free to be ingrates today). Pat On 24/11/2011 14:23, Bert Gunter wrote: Pat: 1. Thank you for this. Having not read your tutorial, but based on what I know of your other efforts, I am sure that you are correct. Is there a link to this on CRAN somewhere so I can refer to it in future (too lazy to search myself)? 2. Thank you also for your continuing contributions to R documentation. I know this takes a lot of work and you do it well. Would that more R learners would read them -- there would be a lot less RTFM type queries on r-help. Best, Bert On Thu, Nov 24, 2011 at 12:27 AM, Patrick Burns pbu...@pburns.seanet.com wrote: It's very seldom that I disagree with Bert, but here is one time. I don't think An Introduction to R is a suitable first read for people with little computational experience. Better (I modestly suggest) would be: http://www.burns-stat.com/pages/Tutor/hints_R_begin.html which includes some other references. 'Hints' is imperfect and incomplete but it suffers slightly less from the curse of knowledge than a lot of other R documentation. Pat On 24/11/2011 00:15, Bert Gunter wrote: ... and you can of course do the assignment: Bndy- paste (BndY,to,50+seq_len(BndY), mN, sep = ) An Introduction to R tells you about such fundamentals and should be a first read for anyone learning R. --- Bert On Wed, Nov 23, 2011 at 4:10 PM, Bert Gunterbgun...@gene.comwrote: Don't do this! paste() is vectorized. paste (BndY,to,50+seq_len(BndY), mN, sep = ) Cheers, Bert On Wed, Nov 23, 2011 at 3:31 PM, B77Sbps0...@auburn.eduwrote: out- vector(list) Ylab- for(i in 1:length(BndY)) { out[i]- paste(BndY[i], to ,BndY[i],mN) } Ylab- do.call(c, out) markm0705 wrote Dear R helpers I'm trying to make up some labels for plot from this vector BndY-seq(from = 18900,to= 19700, by = 50) using Ylab-for(i in BndY) {c((paste(i, to ,i+50,mN)))} but the vector created is NULL However if i use for(i in BndY) {print(c(paste(i, to ,i+50,mN)))} I can see the for loop is making the labels I'm looking for but not sure on my error in assigning them to a vector Thanks in advance -- View this message in context: http://r.789695.n4.nabble.com/Looping-and-paste-tp4101892p4102066.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] alpha_1 + beta_1 1 in GARCH(1,1)
This would have been a more appropriate question on r-sig-finance. The short answer is: no, you can't trust the coefficients. You don't say how much data you have, but this situation is common when you don't have much data (meaning fewer than 2000 daily observations). The sum of those two parameters is saying how fast shocks dissipate (in the variance). If it looks like there is a trend in volatility over the in-sample period, then a reasonable answer given that information is that the shocks do not dissipate -- meaning the sum of the parameters is greater than 1. On 20/11/2011 10:25, user84 wrote: Hi, as i suppose to know in a stationary GARCH(1,1) model the sum of alpha and beta has to be smaller than 1. But if i use the garchfit() function from the package fGarch for my timeseries the sum is bigger than 1. The adf.test tells me a p-value smaller than 0.01 instead. What does this mean for me? Can i trust in the coefficients in this case? mfg user84 -- View this message in context: http://r.789695.n4.nabble.com/alpha-1-beta-1-1-in-GARCH-1-1-tp4088342p4088342.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help
You've been told how to do what you ask. But I'm not convinced that you really want to do what you asked. It might be better to do whatever you want with the data leaving it all in one object. There are many ways of doing that, the 'by' function is one of them. On 11/11/2011 20:24, Francesca wrote: Dear Contributors I would like to perform this operation using a loop, instead of repeating the same operation many times. The numbers from 1 to 4 related to different groups that are in the database and for which I have the same data. x-c(1,3,7) datiP1- datiP[datiP$city ==1,x]; datiP2- datiP[datiP$city ==2,x]; datiP3- datiP[datiP$city ==3,x] datiP4- datiP[datiP$city ==4,x]; -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] set seed for random draws
I'm suspecting this is confusion about default behavior. R automatically updates the random seed when random numbers are generated (or other random operations are performed). The original poster may have experienced systems where it is up to the user to change the seed. I'd suggest two rules of thumb when coming up against something in R that you aren't sure about: 1. If it is a mundane task, R probably takes care of it. 2. Experiment to see what happens. Of course you could read documentation, but no one does that. On 05/11/2011 00:59, R. Michael Weylandt wrote: This might be more fundamental, but why do you feel the need to reset the seed each loop? There's nothing that suggests you need to... Michael On Fri, Nov 4, 2011 at 8:38 PM, Md Desa, Zairul Nor Deana Binti znde...@ku.edu wrote: Hello, all! I need help on these two problems: 1) If I want to randomly draw numbers from standard normal (or other distributions) in loops e.g.: ty=0; ks=0 for (i in 1:5) { set.seed(14537+i) k-rnorm(1) ks[i]-.3*k+.9 if (ty==0) { while ((ks.2)||(ks3)) { #set.seed(13237+i*100) k-rnorm(1) ks[i]-.3*k+.9 } } } } Question: Here I draw initial a, then if the drawn initial a satisfied 2 conditions I redraw a. I set.seed(13237) in the first draw of a, should I set.seed() in the redraw part? 2) I also have more loops after this i loop that also draw from normal(0,1). I want to randomly draws from normal(0,1) for loop j (inside loop j I draw another random numbers from N(0,1)) My question: Should I or shouldn't I set seed again and again for each loop? Why or why not. I guess this problem concerned about setting seed as I want to have different number for each i. Thanks! Deana __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] array manipulation
You seem to be looking for 'aperm'. There is a chapter in 'S Poetry' (available on http://www.burns-stat.com) that talks about working with higher dimensional arrays. I don't think any changes need to be made for R. On 02/11/2011 16:16, Simone Salvadei wrote: Hello, I'm at the very beginning of the learning process of this language. Sorry in advance for the (possible but plausible) stupidity of my question. I would like to find a way to permute the DIMENSIONS of an array. Something that sounds like the function permute() in matlab. Given an array C of dimensions c x d x T , for instance, the command permute(C, [2 1 3]) would provide (in Matlab) an array very similar to C, but this time each one of the T matrices c x d has changed into its transposed. Any alternatives to the following (and primitive) 'for' cycle? *# (previously defined) phi=array with dimensions c(c,d,T)* * * *temp=array(0,dim=c(c,d,T))* * for(i in 1:T)* * {* * temp[,,i]=t(phi[,,i])* * }* * phi=temp* * * Thank you very much! S -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] element-by-element comparison
Given that you want to compare columns, you can just do: A B If you wanted to compare rows, then it is more troublesome. One approach would be: rep(A, each=nrow(B)) B On 30/10/2011 03:51, Wendy wrote: Hi, I have a vector and a matrix. For example, A = [ 12 3 4]; B = [ 4 13 10 2 4 8]; I am comparing A to each column of B using AB[,ii], so the expected result is C = [ 10 01 00]; I am looking for a way to do this quickly instead of going through the for loop, but haven't had any luck yet? Any advice is appreciated. Thank you very much. Wendy -- View this message in context: http://r.789695.n4.nabble.com/element-by-element-comparison-tp3952301p3952301.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] program never enters browser mode when I add browser()
I see two possibilities: 1) the call to 'browser' is inside an 'if' clause that doesn't exectute. 2) the call forgets the parentheses, so it is: browser rather than browser() On 28/10/2011 20:04, M. Tran wrote: Dear All I have a program that breaks at the following lines of code: bigfunction = { ... object1 = myfunction(x) object2 = strsplit(object1, ,)[[1]] ... } where myfunction is defined elsewhere outside of bigfunction. The error I get is error in strsplit() -- object1 not found. However, when I insert browser() into my code so that the above reads, bigfunction = { ... browser() object1 = myfunction(x) object2 = strsplit(object1, ,)[[1]] ... } my entire program runs successfully, and oddly *never enters browser mode*. Has anyone encountered a similar problem? Any advice would be greatly appreciated. Thank you, Michelle -- View this message in context: http://r.789695.n4.nabble.com/program-never-enters-browser-mode-when-I-add-browser-tp3948920p3948920.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] fast or space-efficient lookup?
I think you are looking for the 'data.table' package. On 09/10/2011 17:31, ivo welch wrote: Dear R experts---I am struggling with memory and speed issues. Advice would be appreciated. I have a long data set (of financial stock returns, with stock name and trading day). All three variables, stock return, id and day, are irregular. About 1.3GB in object.size (200MB on disk). now, I need to merge the main data set with some aggregate data (e.g., the SP500 market rate of return, with a day index) from the same day. this market data set is not a big data set (object.size=300K, 5 columns, 12000 rows). let's say my (dumb statistical) plan is to run one grand regression, where the individual rate of return is y and the market rate of return is x. the following should work without a problem: combined- merge( main, aggregate.data, by=day, all.x=TRUE, all.y=FALSE ) lm( stockreturn ~ marketreturn, data=combined ) alas, the merge is neither space-efficient nor fast. in fact, I run out of memory on my 16GB linux machine. my guess is that by whittling it down, I could work it (perhaps doing it in chunks, and then rbinding it), but this is painful. in perl, I would define a hash with the day as key and the market return as value, and then loop over the main data set to supplement it. is there a recommended way of doing such tasks in R, either super-fast (so that I merge many many times) or space efficient (so that I merge once and store the results)? sincerely, /iaw Ivo Welch (ivo.we...@gmail.com) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Running *slow*
Probably most of the time you're waiting for this you are in Circle 2 of 'The R Inferno'. If the values are numbers, you might also be in Circle 1. On 06/10/2011 13:37, Thomas wrote: Anyone got any hints on how to make this code more efficient? An early version (which to be fair did more than this one is) ran for 330 hours and produced no output. I have a two column table, Dat, with 12,000,000 rows and I want to produce a lookup table, ltable, in a 1 dimensional matrix with one copy of each of the values in Dat: for (i in 1:nrow(Dat)) { for (j in 1:2) { #If next value is already in ltable, do nothing if (is.na(match(Dat[i,j], ltable))){ltable - rbind(ltable,Dat[i,j])} } } but it takes forever to produce anything. Any advice gratefully received. Thomas __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] SPlus to R
+0.5)/(nc+0.5))-(xe-xc)/sqrt(1/(4*(ne+0.5))) + return(1-pnorm(ans)) + } [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] troubles with a for loop
Sarah has told you how to get from where you are to where you want to be. However, it is easier to start somewhere else. The more R way to do what you are doing is to use a list to put the results of your regressions into. It is then very easy to use that list, and it is easier to keep track of. The two documents mentioned in my signature may help you get up and running in R. Since you are coming from Stata, you should also look at http://r4stats.com On 18/09/2011 17:05, Francesco Sarracino wrote: Dear all, I am a stata user and I am moving my first steps in R. I am dealing with a silly issue concerning looping over variables. I read previous posts on similar topics in the R help archive, but I did not find a solution. Here is my case: I run a simple bivariate linear regression saving the output in objects called: pd.memb.0, pd.memb.1, pd.memb.2, pd.memb.3 pd.memb.0- lm(E.fs.memb ~ M.fs.memb, data, subset = (tag2 == 1)) pd.memb.1- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb= quantile(M.fs.memb, .25) tag2 == 1)) pd.memb.2- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .25) M.fs.memb= quantile(M.fs.memb, .75) tag2 == 1)) pd.memb.3- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .75) tag2 == 1)) Subsequently, I wish to plot my data superimposing a different line for each regression model. Here I am trying to apply my loop. Looking at the info I found around, the following code sound to me as correct, but I am wrong. plot(M.fs.memb , E.fs.memb) for(i = 1:3){ curve(coef(pd.memb.i)[1] + coef(pd.memb.i)[2]*x, add = T, cex = 100, col = red) } The plot is plotted, but the curves are not printed and I get the following error message: Error in coef(pd.memb.i) : error in evaluating the argument 'object' in selecting a method for function 'coef' }Error: unexpected '}' in } What am I doing wrong? Thanks a lot for your help, f. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Solve your R problems
There has been one update of 'The R Inferno' made in spring 2011. It is a fairly mild revision but has changes prompted by a number of people -- most especially Mark Difford. That other Pat Burns may have known about hockey, but I suspect he was a real laggard when it came to random portfolios. On 13/09/2011 00:27, Carl Witthoft wrote: Love the page. http://www.portfolioprobe.com/2011/09/12/solve-your-r-problems/ Just out of interest, is this an updated version or the same ol' Inferno document? And why do I keep thinking you (Patrick Burns) are the Hab's coach? :-) -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Solve your R problems
R-help is all about solving R problems. So here ya go: http://www.portfolioprobe.com/2011/09/12/solve-your-r-problems/ -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Solve your R problems
Far be it from me to misquote someone. On 12/09/2011 09:17, peter dalgaard wrote: On Sep 12, 2011, at 09:41 , Patrick Burns wrote: R-help is all about solving R problems. So here ya go: http://www.portfolioprobe.com/2011/09/12/solve-your-r-problems/ Grin. Incidentally, I don't think it is quite true that I called you that infernal guy at useR. I might have done so (tongue in cheek, of course), but more likely it was ah, the R Inferno guy upon glancing at your name tag. Anyways, Si non e vero, e ben trovato. ;-) -p -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Avoiding for Loop for moving average
The 'filter' function should be able to do what you want efficiently. On 02/09/2011 18:06, Noah Silverman wrote: Joshua, Thanks for the tip. I need to roll my own code on this. But perhaps I can borrow some code from the package you mentioned. Is the package just performing the loop, but in a faster language? -- Noah Silverman UCLA Department of Statistics 8117 Math Sciences Building #8208 Los Angeles, CA 90095 On Sep 2, 2011, at 9:58 AM, Joshua Ulrich wrote: On Fri, Sep 2, 2011 at 11:47 AM, R. Michael Weylandt michael.weyla...@gmail.com wrote: Have you looked at SMA/EMA from the TTR package? That's a pretty quick implementation. runmean from caTools is even better for the SMA but I don't think there's an easy way to turn that into an EWMA. SMA still calls Fortran code, so that's why it's slower than caTools::runmean. I've moved the EMA code to C, so it's about as fast as it can be. Noah, use EMA's ratio argument to replicate your for loop. Hope this helps, Michael Weylandt Best, -- Joshua Ulrich | FOSS Trading: www.fosstrading.com On Fri, Sep 2, 2011 at 12:43 PM, Noah Silvermannoahsilver...@ucla.eduwrote: Hello, I need to calculate a moving average and an exponentially weighted moving average over a fairly large data set (500K rows). Doing this in a for loop works nicely, but is slow. ewma- data$col[1] N- dim(data)[1] for(i in 2:N){ data$ewma- alpha * data$ewma[i-1] + (1-alpha) * data$value[i] } Since the moving average accumulates as we move through the data, I'm not sure on the best/fastest way to do this. Does anyone have any suggestions on how to avoid a loop doing this? -- Noah Silverman UCLA Department of Statistics 8117 Math Sciences Building #8208 Los Angeles, CA 90095 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Oh apply functions, how you confuse me
I would suggest using a 'for' loop rather than an apply function. The advantage is that you will probably understand the loop that you write, and it will run in roughly the same amount of time as a complicated call to an apply function that you don't understand. On 01/09/2011 18:11, LCOG1 wrote: Hi guys, I have a crap load of data to parse and have enjoyed creating a script that takes this data and creates a number of useful graphics for our area. I am unable to figure out one summary though and its all cause I dont fully understand the apply family of functions. Consider the following: #Create data Df..-rbind(data.frame(Id=1:1008,Dir=rep(c(NB,NB,SB,SB),252),Mph=runif(1008,0,65), Volume=runif(1008,0,19),Hour=rep(00,1008),Min5Break=rep(1:12,84),Day=rep(1,1008)), data.frame(Id=2009:2016,Dir=rep(c(NB,NB,SB,SB),252),Mph=runif(1008,0,65), Volume=runif(1008,0,19),Hour=rep(01,1008),Min5Break=rep(1:12,84),Day=rep(2,1008))) #Example calc Results_-list() #Sum Volume by 5 minute break by Day by Direction Results_$FiveMin.Direction-tapply(Df..$Volume,list(Df..$Min5Break,Df..$Day,Df..$Hour,Df..$Dir),sum) The data is a snap shot of what im working with and I am trying to get to something similar to the last line where the volumes are summed. What i want to do is to do a weighted average for the speed by 5 minute break. So for all the speeds and volumes in a given hour of 5 minute break(12 per hour), i would want to sum(Volumes[1:12]*Speed[1:12]) / sum(Volumes[1:12] The output resembling the one from the above but having these weighted values. I am assuming the sum function in the above would be replaced by a function doing the calculation but I am still not sure how to do this using apply functions, so perhaps this isnt the best option. Hope this is clear and hope you guys(and of course ladies) can offer some guidance. Cheers, Josh -- View this message in context: http://r.789695.n4.nabble.com/Oh-apply-functions-how-you-confuse-me-tp3784212p3784212.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] subsetting by rows
If your data frame is called 'df', then something like the following should work: df[ apply(as.matrix(df[,-1]), 1, function(x) any(x == P)), ] This creates a logical vector as long as the number of rows. As Bill Dunlap recently noted, 'apply' really wants a matrix and not a data frame. On 31/08/2011 18:28, Joao Fadista wrote: Dear all, I would like to know how to subset a data.frame by rows. Example: Probesets348843488834892 1 19676_at A AA 2 10001_atP P P 3 10002_atA A A 4 10003_atA A A 5 100048912_at P A A For this data.frame I want to retrieve only the rows where at least one ´P´ is found. So in this example it would be rows 2 and 5. Best regards and thanks in advance [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.