Re: [R] stats on transitions from one state to another
David: Brilliant! Thanks very much. As Berend pointed out, I was not precise in the query, sorry. Please note in the example that we have a run of three state 2 after 0, and later another run of two state 2 after 0. 0 2 2 2 0 0 2 2 Whenever the state moves from 0 to 2, I want to compute the mean of the run of values in state 2. I tried to modify David's example by doing the 'interaction' on states and runs: y - rle(sss[,State]) yy - cbind(y$lengths, y$values, c(NA, y$values[-length(y$values)])) colnames(yy) - c(RunLength,State,PrevState) tapply(yy[,RunLength], INDEX=interaction(yy[,PrevState], yy[,State]), c) This gives me the list of runs for each transition-pair. Now I need to match these lists of runs against the corresponding rows in sss of values V1, V2, and compute stats on them. Is there an easier way? Thanks, Murali -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: 20 February 2012 15:31 To: Menon Murali Cc: r-help@r-project.org Subject: Re: [R] stats on transitions from one state to another On Feb 20, 2012, at 10:11 AM, murali.me...@avivainvestors.com wrote: Folks, I'm trying to get stats from a matrix for each transition from one state to another. I have a matrix x as below. structure(c(0, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1, 2, 2, 1, 1, 1, 0, 0, 2, 2, 0.21, -0.57, -0.59, 0.16, -1.62, 0.18, -0.81, -0.19, -0.76, 0.74, -1.51, 2.79, 0.41, 1.63, -0.86, -0.81, 0.39, -1.38, 0.06, 0.84, 0.51, -1, -1.29, 2.15, 0.39, 0.78, 0.85, 1.18, 1.66, 0.9, -0.94, -1.29, -0.23, -0.92, -0.21, 1.02, -0.77, -0.68, -0.33, 0.04), .Dim = c(20L, 3L), .Dimnames = list(NULL, c(State, V1, V2))) Is it possible to get, say, mean values of each variable in state 1 when the previous state was 0, in state 2 when the previous state was 0, and so on with all available transitions between states 0, 1, 2? In the above case, mean of V1 in state 2 when previous state was 0 would be mean(c(-0.57, -0.59, 0.16, 0.06, 0.84)) = -0.02 while the mean of V1 in state 0 when previous state was 2 would be: mean(c(1.62, 0.18, -0.81)) = 0.33 If I try something like by(x[, 2:3], x[, 1], FUN = colMeans) I get the means for each state. I'm not sure how to get the split by transition? Add an extra column of previous states: and tabulate: sss -cbind(sss, c(NA, sss[,State][-nrow(sss)]) ) table(sss[,State], sss[,4]) 0 1 2 0 3 1 1 1 1 5 1 2 2 1 4 The requested means for V1 by transition types: tapply(sss[,V1], INDEX=interaction(sss[,State], sss[,4]), mean) 0.01.02.00.11.12.10.21.22.2 -0.670 -0.190 -0.255 0.390 -0.640 2.790 -1.620 1.630 0.205 The counts on which those means are based: tapply(sss[,V1], INDEX=interaction(sss[,State], sss[,4]), length) 0.0 1.0 2.0 0.1 1.1 2.1 0.2 1.2 2.2 3 1 2 1 5 1 1 1 4 -- David Winsemius, MD West Hartford, CT __ 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] stats on transitions from one state to another
Folks, I'm trying to get stats from a matrix for each transition from one state to another. I have a matrix x as below. structure(c(0, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1, 2, 2, 1, 1, 1, 0, 0, 2, 2, 0.21, -0.57, -0.59, 0.16, -1.62, 0.18, -0.81, -0.19, -0.76, 0.74, -1.51, 2.79, 0.41, 1.63, -0.86, -0.81, 0.39, -1.38, 0.06, 0.84, 0.51, -1, -1.29, 2.15, 0.39, 0.78, 0.85, 1.18, 1.66, 0.9, -0.94, -1.29, -0.23, -0.92, -0.21, 1.02, -0.77, -0.68, -0.33, 0.04), .Dim = c(20L, 3L), .Dimnames = list(NULL, c(State, V1, V2))) Is it possible to get, say, mean values of each variable in state 1 when the previous state was 0, in state 2 when the previous state was 0, and so on with all available transitions between states 0, 1, 2? In the above case, mean of V1 in state 2 when previous state was 0 would be mean(c(-0.57, -0.59, 0.16, 0.06, 0.84)) = -0.02 while the mean of V1 in state 0 when previous state was 2 would be: mean(c(1.62, 0.18, -0.81)) = 0.33 If I try something like by(x[, 2:3], x[, 1], FUN = colMeans) I get the means for each state. I'm not sure how to get the split by transition? Thanks, Murali __ 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] choosing best 'match' for given factor
Interesting variety of solutions! Thanks very much. Murali -Original Message- From: Henrique Dallazuanna [mailto:www...@gmail.com] Sent: 31 March 2011 18:26 To: Menon Murali Cc: r-help@r-project.org Subject: Re: [R] choosing best 'match' for given factor Try this: bestMatch - function(search, match) { colnames(match)[pmax(apply(match[,search], 2, which.max) - 1, 1)] } On Thu, Mar 31, 2011 at 11:46 AM, murali.me...@avivainvestors.com wrote: Folks, I have a 'matching' matrix between variables A, X, L, O: a - structure(c(1, 0.41, 0.58, 0.75, 0.41, 1, 0.6, 0.86, 0.58, 0.6, 1, 0.83, 0.75, 0.86, 0.83, 1), .Dim = c(4L, 4L), .Dimnames = list( c(A, X, L, O), c(A, X, L, O))) a A X L O A 1.00 0.41 0.58 0.75 X 0.41 1.00 0.60 0.86 L 0.58 0.75 1.00 0.83 O 0.60 0.86 0.83 1.00 And I have a search vector of variables v - c(X, O) I want to write a function bestMatch(searchvector, matchMat) such that for each variable in searchvector, I get the variable that it has the highest match to - but searching only among variables to the left of it in the 'matching' matrix, and not matching with any variable in searchvector itself. So in the above example, although X has the highest match (0.86) with O, I can't choose O as it's to the right of X (and also because O is in the searchvector v already); I'll have to choose A. For O, I will choose L, the variable it's best matched with - as it can't match X already in the search vector. My function bestMatch(v, a) will then return c(A, L) My matrix a is quite large, and I have a long list of search vectors v, so I need an efficient method. I wrote this: bestMatch - function(searchvector, matchMat) { sapply(searchvector, function(cc) { y - matchMat[!(rownames(matchMat) %in% searchvector) (index(rownames(matchMat)) match(cc, rownames(matchMat))), cc, drop = FALSE]; rownames(y)[which.max(y)] }) } Any advice? Thanks, Murali __ 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ 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] choosing best 'match' for given factor
Folks, I have a 'matching' matrix between variables A, X, L, O: a - structure(c(1, 0.41, 0.58, 0.75, 0.41, 1, 0.6, 0.86, 0.58, 0.6, 1, 0.83, 0.75, 0.86, 0.83, 1), .Dim = c(4L, 4L), .Dimnames = list( c(A, X, L, O), c(A, X, L, O))) a A X L O A 1.00 0.41 0.58 0.75 X 0.41 1.00 0.60 0.86 L 0.58 0.75 1.00 0.83 O 0.60 0.86 0.83 1.00 And I have a search vector of variables v - c(X, O) I want to write a function bestMatch(searchvector, matchMat) such that for each variable in searchvector, I get the variable that it has the highest match to - but searching only among variables to the left of it in the 'matching' matrix, and not matching with any variable in searchvector itself. So in the above example, although X has the highest match (0.86) with O, I can't choose O as it's to the right of X (and also because O is in the searchvector v already); I'll have to choose A. For O, I will choose L, the variable it's best matched with - as it can't match X already in the search vector. My function bestMatch(v, a) will then return c(A, L) My matrix a is quite large, and I have a long list of search vectors v, so I need an efficient method. I wrote this: bestMatch - function(searchvector, matchMat) { sapply(searchvector, function(cc) { y - matchMat[!(rownames(matchMat) %in% searchvector) (index(rownames(matchMat)) match(cc, rownames(matchMat))), cc, drop = FALSE]; rownames(y)[which.max(y)] }) } Any advice? Thanks, Murali __ 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] questions on unstack()
Folks, I have the following dataframe: x - structure(list(name = c(EU B, EU B, EU B, EU B, EU B, EU B, AU A, AU A, AU A, AU A, AU A, AU A), date = c(2010-10-11, 2010-10-12, 2010-10-13, 2010-10-14, 2010-10-15, 2010-10-18, 2010-10-11, 2010-10-12, 2010-10-13, 2010-10-14, 2010-10-15, 2010-10-18), Jem = c(1.3924, 1.3888, 1.3867, 1.3949, 1.4054, 1.3992, 0.9864, 0.9859, 0.9842, 0.9919, 0.9925, 0.9901), Bim = c(1.3888, 1.3867, 1.3949, 1.4054, 1.3977, 1.3917, 0.9859, 0.9842, 0.9919, 0.9925, 0.9907, 0.9881)), .Names = c(name, date, Jem, Bim ), row.names = c(1L, 2L, 3L, 4L, 5L, 8L, 9L, 10L, 11L, 12L, 13L, 16L), na.action = structure(c(6L, 7L, 14L, 15L), .Names = c(6, 7, 14, 15), class = omit), class = data.frame) x name dateJemBim 1 EU B 2010-10-11 1.3924 1.3888 2 EU B 2010-10-12 1.3888 1.3867 3 EU B 2010-10-13 1.3867 1.3949 4 EU B 2010-10-14 1.3949 1.4054 5 EU B 2010-10-15 1.4054 1.3977 8 EU B 2010-10-18 1.3992 1.3917 9 AU A 2010-10-11 0.9864 0.9859 10 AU A 2010-10-12 0.9859 0.9842 11 AU A 2010-10-13 0.9842 0.9919 12 AU A 2010-10-14 0.9919 0.9925 13 AU A 2010-10-15 0.9925 0.9907 16 AU A 2010-10-18 0.9901 0.9881 I'm trying to collapse the frame so that I get columns of names: unstack(x, Jem ~ name) AU.A EU.B 1 0.9864 1.3924 2 0.9859 1.3888 3 0.9842 1.3867 4 0.9919 1.3949 5 0.9925 1.4054 6 0.9901 1.3992 Three questions: 1. The column names are converted from EU B to EU.B - how to preserve the original names? 2. The column names are sorted alphabetically - how to preserve the original order? I tried unstack(x, terms(Jem ~ name, keep.order = TRUE)) but it doesn't really do anything. 3. If I declare a variable wantedName - 'Jem', how can I use it to perform the unstack: unstack(x, `wantedName` ~ name) Error in tapply(eval(form[[2L]], x), eval(form[[3L]], x), as.vector) : arguments must have same length Thanks, Murali __ 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] graphics layout
Folks, I'm battling the layout() functionality in graphics, and getting a bit mixed up. I'd like to create subscreens like so: _ _ | | | |1|2| |_| | | | | |3|4| |_|_| | |6| |5|7| |_|8| Note that subscreens 1:5 are the same size, and 6,7,8 are the same width as, say, 1, but of one-third the height. How to achieve this? Thanks, Murali __ 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] graphics layout
Hiya, Thanks for this. It's the height parameters that baffle me. Why are they 1, 1, 1/3, 1/3, 1/3? Which subscreens do these heights correspond to? I did it like this: nf - layout(cbind(c(1,1,1,3,3,3,5,5,5),c(2,2,2,4,4,4,6,7,8))) mainly because I'm not clear about how the heights work. Thanks, Murali -Original Message- From: Eik Vettorazzi [mailto:e.vettora...@uke.uni-hamburg.de] Sent: 12 October 2010 13:43 To: Menon Murali Cc: r-help@r-project.org Subject: Re: [R] graphics layout Hi Murali try nf - layout(matrix(c(1:5,6,5,7,5,8), ncol=2, byrow=TRUE), heights=c(1,1,rep(1/3,3)),respect=TRUE) layout.show(nf) hth. Am 12.10.2010 14:19, schrieb murali.me...@avivainvestors.com: Folks, I'm battling the layout() functionality in graphics, and getting a bit mixed up. I'd like to create subscreens like so: _ _ | | | |1|2| |_| | | | | |3|4| |_|_| | |6| |5|7| |_|8| Note that subscreens 1:5 are the same size, and 6,7,8 are the same width as, say, 1, but of one-third the height. How to achieve this? Thanks, Murali __ 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. -- Eik Vettorazzi Institut für Medizinische Biometrie und Epidemiologie Universitätsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 __ 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] getting column names of row-by-row sorted matrix
Hi folks, I want to sort a matrix row-by-row and create a new matrix that contains the corresponding colnames of the original matrix. E.g. set.seed(123) a - matrix(rnorm(20), ncol=4); colnames(a) - c(A,B,C,D) a A B C D [1,] -0.56047565 1.7150650 1.2240818 1.7869131 [2,] -0.23017749 0.4609162 0.3598138 0.4978505 [3,] 1.55870831 -1.2650612 0.4007715 -1.9666172 [4,] 0.07050839 -0.6868529 0.1106827 0.7013559 [5,] 0.12928774 -0.4456620 -0.5558411 -0.4727914 I want to obtain a matrix that looks like this A C B D A C B D D B C A B A C D C D B A How best to achieve this? I was able to do it for the max and min of each row by which.min, which.max, but for the entire thing, I'm stymied. Thanks, Murali __ 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] simultaneous estimation
Hi folks, Not sure what this sort of estimation is called. I have a 2-column time-series x(i,t) [with (i=1,2; t=1,...T)], and I want to do the following 'simultaneous' regressions: x(1,t) = (d - 1)(x(1, t-1) - mu(1)) x(2,t) = (d - 1)(x(2, t-1) - mu(2)) And I want to determine the coefficients d, mu(1), mu(2). Note that the d should be the same for both estimations, whereas the coefficients mu will have two values mu(1), mu(2), one for each estimation. Is this possible to do in R? What would be the corresponding syntax in, say, lm? Thanks, Murali __ 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] simultaneous estimation
Hi Duncan, Thanks for your response. Indeed, independent normal errors were what I had in mind. As for variances, if I assume they are the same, would a 'pooled model' apply in this case? Is that equivalent to your suggestion of concatenating x(1,t) and x(2,t)? Cheers, Murali -Original Message- From: Duncan Murdoch [mailto:murdoch.dun...@gmail.com] Sent: 31 August 2010 12:31 To: Menon Murali Cc: r-help@r-project.org Subject: Re: [R] simultaneous estimation On 31/08/2010 6:58 AM, murali.me...@avivainvestors.com wrote: Hi folks, Not sure what this sort of estimation is called. I have a 2-column time-series x(i,t) [with (i=1,2; t=1,...T)], and I want to do the following 'simultaneous' regressions: x(1,t) = (d - 1)(x(1, t-1) - mu(1)) x(2,t) = (d - 1)(x(2, t-1) - mu(2)) And I want to determine the coefficients d, mu(1), mu(2). Note that the d should be the same for both estimations, whereas the coefficients mu will have two values mu(1), mu(2), one for each estimation. Is this possible to do in R? What would be the corresponding syntax in, say, lm? Your specification is not complete: you haven't said what the errors will be, or how x(1,1) and x(2,1) are determined. I assume you mean independent normal errors, but are you willing to assume the variance is the same in both series? If so, then your model is almost equivalent to a linear model with concatenated x(1,t) and x(2,t) values. (This would be the partial likelihood version of the model, where you don't try to fit x(i, 1), but you fit the rest of the values conditional on earlier ones.) If you want the full likelihood or you want separate variances for the two series, you probably need to write out the likelihood explicitly and maximize it. Duncan Murdoch __ 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] simultaneous estimation
Bert, I expect you are correct, burrito notwithstanding (wasn't Taco Bell, was it? :-) The full model adds differences and lags, and incorporates non-zero covariances in the innovations. I only simplified to get an idea of how to implement in R. For anyone interested, I'm looking at the Balvers and Wu (2006): Momentum and Mean Reversion across National Equity Markets, Journal of Empirical Finance 13, 24-48. Their model is as follows, with x(i, t) = log of stock index value of country i at time t: x(i, t) = (1 - d(i)) * mu(i) + d(i) * x(i, t - 1) + Sum[rho(i, j) * (x(i, t - j) - x(t - j - 1))] + eps(i, t) where Sum is across J periods, the d(i), mu(i) and rho(i, j) are all specific to each country (i), and the error terms eps(i) have some covariance structure. You can see that the mu(i) term is supposed to capture the drift of the random walk component of stock index movement, the rho(i) is a coefficient for the momentum component, and the d(i) represents long temporary swings in the index. But as there's now a large number of parameters to estimate, a simplifying assumption is that d and rho are common to all the countries, while the mu is specific. Thanks, Murali From: Bert Gunter [mailto:gunter.ber...@gene.com] Sent: 31 August 2010 17:12 To: Duncan Murdoch Cc: David Winsemius; r-help@r-project.org; Menon Murali Subject: Re: [R] simultaneous estimation I would hazard the guess that this would be better estimated as a multivariate time series (e.g. AR1) in which the covariance between the two innovation components was NOT assumed to be 0 (nor were their variances assumed to be the same). The R time series task view lists packages to do this, but ?ar might be a place to start. I would happily defer to expert opinion on this matter, however. I just always get this funny rumbling in my stomach whenever anyone proposes simple lagged regression models for time series. Maybe it's the burrito, though... -- Bert On Tue, Aug 31, 2010 at 8:53 AM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 31/08/2010 11:00 AM, David Winsemius wrote: On Aug 31, 2010, at 10:35 AM, murali.me...@avivainvestors.com murali.me...@avivainvestors.com wrote: Hi Duncan, Thanks for your response. Indeed, independent normal errors were what I had in mind. As for variances, if I assume they are the same, would a 'pooled model' apply in this case? Is that equivalent to your suggestion of concatenating x(1,t) and x(2,t)? Wouldn't this be equivalent to a segmented regression analysis that would estimate the slopes in the two periods as mu(1) and mu(2), throw- away any level shift estimate at the join-point, and which then estimated the residual one-lag autocorrelation (again omitting the join point) and assigned that value to d? That is a different model. In the given situation, successive observations are correlated, so if x(1, t) had a large residual above the line, x(1, t+1) would be expected to have a large residual as well, and as long as |d-1| is less than 1, the given model would have zero slope in the long run. Duncan Murdoch __ 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.
[R] semiparametric fractional autoregressive model
folks, does anyone know if the SEMIFAR model has been implemented in R? i see that there's a S-FinMetrics function SEMIFAR() that does the job, but I have no access to that software. essentially, this semiparametric fractional autoregressive model introduces a deterministic trend to the FARIMA(p,d,0) model (which, as i understand it, takes care of the random trend and short and long memory). if not, are there any suggestions for how to estimate the model: phi(L) (1 - L)^d [y(t)(1 - L) - g(t/T)] = epsilon(t) for t = 1, , T, and where -0.5 d 0.5, phi(L) is the lag polynomial, and g() is a smooth trend function on [0, 1]. what would be a good nonparametric kernel estimator of g()? i'm new at this so any pointers would be helpful. thanks, murali __ 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] converting a time to nearest half-hour
Hi folks, I've got a POSIXct datum as follows: Sys.time() [1] 2010-07-23 11:29:59 BST I want to convert this to the nearest half-hour, i.e., to 2010-07-23 11:30:00 BST (If the time were 11:59:ss, I want to convert to 12:00:00). How to achieve this? Thanks, Murali __ 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] converting a time to nearest half-hour
David, Stephen, You're right - it's the time zone conventions that threw me as well. I tried those round() operations earlier, but inevitably ended up being either an hour behind. Even when I specified my time zone, it didn't make any difference. So there's something else that I'm missing. I'll take a look at your various approaches, and get back to you. Cheers, Murali -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of David Winsemius Sent: 23 July 2010 17:15 To: stephen sefick Cc: r-help@r-project.org Subject: Re: [R] converting a time to nearest half-hour On Jul 23, 2010, at 12:04 PM, stephen sefick wrote: If you have a zoo series this should work. If it doesn't then please tell me because I think it works. snap2min - function(zoo, min=00:15:00){ min15 - times(min) a - aggregate(zoo, trunc(time(zoo), min15), function(x) mean(x, na.rm=TRUE)) } This works for producing 10 half-hour intervals of EDT times: as.POSIXct(60*30*( round( as.numeric( Sys.time()+ 60*30*(1:10))/ # the sequence creation (60*30))) - # divide prior to rounding 5*60*60,# the TZ offset origin=1970-01-01 ) [1] 2010-07-23 12:30:00 EDT 2010-07-23 13:00:00 EDT [3] 2010-07-23 13:30:00 EDT 2010-07-23 14:00:00 EDT [5] 2010-07-23 14:30:00 EDT 2010-07-23 15:00:00 EDT [7] 2010-07-23 15:30:00 EDT 2010-07-23 16:00:00 EDT [9] 2010-07-23 16:30:00 EDT 2010-07-23 17:00:00 EDT hth Stephen Sefick On Fri, Jul 23, 2010 at 11:00 AM, David Winsemius dwinsem...@comcast.net wrote: On Jul 23, 2010, at 11:35 AM, David Winsemius wrote: On Jul 23, 2010, at 11:20 AM, murali.me...@avivainvestors.com murali.me...@avivainvestors.com wrote: Hi folks, I've got a POSIXct datum as follows: Sys.time() [1] 2010-07-23 11:29:59 BST I want to convert this to the nearest half-hour, i.e., to 2010-07-23 11:30:00 BST (If the time were 11:59:ss, I want to convert to 12:00:00). How to achieve this? Couldn't you just coerce to numeric, divide by 60(sec)*30(half-hour minutes), round to integer, multiply by 60*30, coerce to POSIXct? When I tried my method I see that one also needs to add or subtract the proper number of seconds from Universal Time to get the output formatting correct. (Probably demonstrates that I do not have the proper understanding of the right place to employ a TZ specification.). David Winsemius, MD West Hartford, CT __ 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. -- Stephen Sefick | Auburn University | | Department of Biological Sciences | | 331 Funchess Hall | | Auburn, Alabama | | 36849| |___| | sas0...@auburn.edu | | http://www.auburn.edu/~sas0025 | |___| Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis David Winsemius, MD West Hartford, CT __ 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.
[R] min and max operations on matrix
Folks, I've got a matrix x as follows: x - matrix(c(1,2,3,5,3,4,3,2,1), ncol = 3, byrow = TRUE) x [,1] [,2] [,3] [1,]123 [2,]534 [3,]321 In each row of x, I want to replace the minimum value by -1, the maximum value by +1 and all other values by 0. So in the above case I want to end up as follows: [,1] [,2] [,3] [1,] -101 [2,]1 -10 [3,]10 -1 I tried the following, which seems to work: t(apply(x, 1, function(y) {z - numeric(NROW(y)); z[which.min(y)] - -1; z[which.max(y)]- 1; z})) Is there a neater way to do this? Thanks, Murali __ 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] differing behaviour between xts (0.6-7) and zoo (1.5-8)
Folks, I have some weekly dataseries that I convert to monthly xts (with yearmon indices), and obtain the two following extracts: str(sig) An 'xts' object from Apr 1998 to Sep 1998 containing: Data: num [1:6, 1] 0.0083 0.2799 -0.2524 -0.0119 0.18 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr e1 Indexed by objects of class: [yearmon] TZ: GMT xts Attributes: NULL str(ret) An 'xts' object from Mar 1998 to Aug 1998 containing: Data: num [1:6, 1] -0.007829 0.006452 -0.000276 -0.000644 0.002572 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr twi.Close Indexed by objects of class: [yearmon] TZ: GMT xts Attributes: NULL I understand that mathematical objects on xts objects will be performed only on the datapoints with common indices, in this case Apr 1998 to Aug 1998. So I do: sig * ret Data: numeric(0) Index: NULL Which doesn't give me what I expect. However, if I do: as.zoo(sig) * as.zoo(ret) e1 Apr 1998 5.351189e-05 May 1998 -7.716467e-05 Jun 1998 1.624531e-04 Jul 1998 -3.055679e-05 Aug 1998 4.122321e-04 Which is as I expect. I took a look at the structure of the two objects: dput(sig) structure(c(0.00829354917358671, 0.279914830605598, -0.252440486192738, -0.0118822201758384, 0.179972233000564, -0.209066714293924), index = c(891388800, 893980800, 896659200, 899251200, 901929600, 904608000), .Dim = c(6L, 1L), .Dimnames = list(NULL, e1), class = c(xts, zoo), .indexTZ = structure(GMT, .Names = TZ), .indexCLASS = yearmon) dput(ret) structure(c(-0.00782945094736132, 0.00645222996118644, -0.000275671952124412, -0.000643530245146628, 0.00257163991836062, 0.00229053194651918 ), index = c(890784000, 893808000, 896227200, 898646400, 901670400, 904089600), .Dim = c(6L, 1L), .Dimnames = list(NULL, twi.Close), .indexCLASS = yearmon, .indexTZ = structure(GMT, .Names = TZ), class = c(xts, zoo)) So clearly the internal values of the supposedly overlapping parts of the indices are different, although they are both 'yearmon' and seem to represent the same months. If I do dput(as.zoo(ret)) structure(c(-0.00782945094736132, 0.00645222996118644, -0.000275671952124412, -0.000643530245146628, 0.00257163991836062, 0.00229053194651918 ), .Dim = c(6L, 1L), .Dimnames = list(NULL, twi.Close), index = structure(c(1998.167, 1998.25, 1998.333, 1998.417, 1998.5, 1998.583 ), class = yearmon), class = zoo) dput(as.zoo(sig)) structure(c(0.00829354917358671, 0.279914830605598, -0.252440486192738, -0.0118822201758384, 0.179972233000564, -0.209066714293924), .Dim = c(6L, 1L), .Dimnames = list(NULL, e1), index = structure(c(1998.25, 1998.333, 1998.417, 1998.5, 1998.583, 1998.667), class = yearmon), class = zoo) Now the indices have the expected overlaps. I'm not sure if this is a bug in xts? sessionInfo() R version 2.9.2 (2009-08-24) i386-pc-mingw32 locale: LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices datasets tcltk utils methods base other attached packages: [1] xts_0.6-7 zoo_1.5-8 svSocket_0.9-43 svMisc_0.9-48 TinnR_1.0.3 R2HTML_1.59-1 Hmisc_3.6-1 rcom_2.2-1 rscproxy_1.3-1 loaded via a namespace (and not attached): [1] cluster_1.12.0 grid_2.9.2 lattice_0.17-25 tools_2.9.2 Please advise. Thanks, Murali __ 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] convert xts vector into matrix
hi folks, if i have an xts object as follows: library(xts) dd - as.POSIXct(strptime(c(2009-06-01 08:00:00, 2009-06-01 08:30:00,2009-06-01 09:00:00,2009-06-02 08:00:00, 2009-06-03 08:00:00, 2009-06-03 08:30:00),%Y-%m-%d %H:%M:%S)) a - xts(1:6,dd) a [[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.
Re: [R] Replace zeroes in vector with nearest non-zero value
Jim, Gabor, William, Thanks very much. Works a treat. Cheers, Murali -Original Message- From: William Dunlap [mailto:wdun...@tibco.com] Sent: 18 June 2009 18:27 To: MENON Murali; r-help@r-project.org Subject: RE: [R] Replace zeroes in vector with nearest non-zero value approx() almost does what you want, but you have to patch up its output to account for a possible initial run of 0's in the input: x - c(0, -1, -1, -1, 0, 0, 1, -1, 1, 0) y - approx(seq_along(x)[x!=0], x[x!=0], xout=seq_along(x), method=const, f=0, rule=2)$y y [1] -1 -1 -1 -1 -1 -1 1 -1 1 1 Doing tricks with cumsum will do it more directly: f-function(x){ i-cumsum(x!=0) # indices to last non-zero value in x c(x[i==0], x[x!=0][i]) # x[i==0] is initial run of 0's, x[x!=0][i] gets the rest } rbind(x,f(x)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] x0 -1 -1 -1001 -11 0 0 -1 -1 -1 -1 -11 -11 1 If there are NA's in x you will have to add an NA test to the x!=0 and x==0 tests or replace the NA's by 0's before doing this. Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of murali.me...@fortisinvestments.com Sent: Thursday, June 18, 2009 9:48 AM To: r-help@r-project.org Subject: [R] Replace zeroes in vector with nearest non-zero value Folks, If I have a vector such as the following: x - c(0, -1, -1, -1, 0, 0, 1, -1, 1, 0) and I want to replace the zeroes by the nearest non-zero number to the left, is there a more elegant way to do this than the following loop? y - x for (i in 2 : length(x)) { if (y[i] == 0) { y[i] - y[i - 1] } } y [1] 0 -1 -1 -1 -1 -1 1 -1 1 1 You can see the first zero is left as is, the next two zeroes become -1, which is the closest non-zero to the left of them, and the last zero becomes 1. Cheers, Murali __ 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.
[R] Replace zeroes in vector with nearest non-zero value
Folks, If I have a vector such as the following: x - c(0, -1, -1, -1, 0, 0, 1, -1, 1, 0) and I want to replace the zeroes by the nearest non-zero number to the left, is there a more elegant way to do this than the following loop? y - x for (i in 2 : length(x)) { if (y[i] == 0) { y[i] - y[i - 1] } } y [1] 0 -1 -1 -1 -1 -1 1 -1 1 1 You can see the first zero is left as is, the next two zeroes become -1, which is the closest non-zero to the left of them, and the last zero becomes 1. Cheers, Murali __ 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] series at low freq expanded into high freq
Folks, If I have a series mm of, say, monthly observations, and a series dd of daily dates, what's a good way of expanding mm such that corresponding to each day in dd within the corresponding month in mm, the values of mm are repeated? So e.g., if I have mm: mm - c(15, 10, 12, 13, 11) names(mm)-c(Nov 2008, Dec 2008, Jan 2009, Feb 2009, Mar 2009) library(zoo) mm - zoo(mm, order.by = as.yearmon(names(mm), format=%b %Y)) And days: dd - as.Date(c(03/11/2008, 05/11/2008, 04/01/2009,02/02/2009,17/02/2009,13/03/2009,14/03/2009,18/03/ 2009, 26/03/2009), format=%d/%m/%Y) I want to be able to create a series that looks like this: 03/11/2008 15 05/11/2008 15 04/01/2009 12 02/02/2009 13 17/02/2009 13 13/03/2009 11 14/03/2009 11 18/03/2009 11 26/03/2009 11 where because no daily dates in dd are in December, the final series has no entries for December; there are two entries for November because there are two dates in dd in November, etc. Thanks, Murali __ 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] series at low freq expanded into high freq
Hmm, silly of me. I have the solution, after some mucking about with coercion and so on: xx - zoo(mm[as.character(as.yearmon(dd))], order.by = dd) xx 2008-11-03 2008-11-05 2009-01-04 2009-02-02 2009-02-17 2009-03-13 2009-03-14 2009-03-18 2009-03-26 15 15 12 13 13 11 11 11 11 Right. Sorry to waste your time. Murali -Original Message- From: MENON Murali Sent: 27 April 2009 11:51 To: 'r-help@r-project.org' Subject: series at low freq expanded into high freq Folks, If I have a series mm of, say, monthly observations, and a series dd of daily dates, what's a good way of expanding mm such that corresponding to each day in dd within the corresponding month in mm, the values of mm are repeated? So e.g., if I have mm: mm - c(15, 10, 12, 13, 11) names(mm)-c(Nov 2008, Dec 2008, Jan 2009, Feb 2009, Mar 2009) library(zoo) mm - zoo(mm, order.by = as.yearmon(names(mm), format=%b %Y)) And days: dd - as.Date(c(03/11/2008, 05/11/2008, 04/01/2009,02/02/2009,17/02/2009,13/03/2009,14/03/2009,18/03/ 2009, 26/03/2009), format=%d/%m/%Y) I want to be able to create a series that looks like this: 03/11/2008 15 05/11/2008 15 04/01/2009 12 02/02/2009 13 17/02/2009 13 13/03/2009 11 14/03/2009 11 18/03/2009 11 26/03/2009 11 where because no daily dates in dd are in December, the final series has no entries for December; there are two entries for November because there are two dates in dd in November, etc. Thanks, Murali __ 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] adding matrices with common column names
Benjamin, Dimitris, Thanks very much. Neat work! Murali -Original Message- From: Nutter, Benjamin [mailto:nutt...@ccf.org] Sent: 27 March 2009 13:52 To: MENON Murali; r-help@r-project.org Subject: RE: [R] adding matrices with common column names Shucks, Dimitris beat me to it. And his code is a bit more elegant than mine. But since I did the work I may as well post it, right? This version incorporates a couple of error checks to make sure all your arguments are matrices with the same number of rows. add.by.name - function(...){ args - list(...) mat.test - sapply(args,is.matrix) if(FALSE %in% mat.test) stop(All arguments must be matrices) mat.row - unique(sapply(args,nrow)) if(length(mat.row)1) stop(All matrices must have the same number of rows) all.names - unique(as.vector(sapply(args,colnames))) sum.mat - matrix(0,nrow=mat.row,ncol=length(all.names)) colnames(sum.mat) - all.names for(i in 1:length(args)){ tmp - args[[i]] sum.mat[,colnames(tmp)] - sum.mat[,colnames(tmp)] + tmp } return(sum.mat) } m1 - matrix(1:20,ncol=4); colnames(m1) - c(a,b,c,d) m2 - matrix(1:20,ncol=4); colnames(m2) - c(b,c,d,e) m3 - matrix(1:20,ncol=4); colnames(m3) - c(a,b,d,e) add.by.name(m1,m2,m3) -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of murali.me...@fortisinvestments.com Sent: Friday, March 27, 2009 9:25 AM To: r-help@r-project.org Subject: [R] adding matrices with common column names folks, if i have three matrices, a, b, cc with some colnames in common, and i want to create a matrix which consists of the common columns added up, and the other columns tacked on, what's a good way to do it? i've got the following roundabout code for two matrices, but if the number of matrices increases, then i'm a bit stymied. a - matrix(1:20,ncol=4); colnames(a) - c(a,b,c,d) b - matrix(1:20,ncol=4); colnames(b) - c(b,c,d, e) cbind(a[,!(colnames(a) %in% colnames(b)), drop = FALSE], a[,intersect(colnames(a),colnames(b))] + b[,intersect(colnames(a),colnames(b)), drop = FALSE], b[,!(colnames(b) %in% colnames(a)), drop = FALSE]) a b c d e [1,] 1 7 17 27 16 [2,] 2 9 19 29 17 [3,] 3 11 21 31 18 [4,] 4 13 23 33 19 [5,] 5 15 25 35 20 now, what if i had a matrix cc? i want to perform the above operation on all three matrices a, b, cc. cc - matrix(1:10,ncol=2); colnames(cc) - c(e,f) i need to end up with: a b c d e f [1,] 1 7 17 27 17 6 [2,] 2 9 19 29 19 7 [3,] 3 11 21 31 21 8 [4,] 4 13 23 33 23 9 [5,] 5 15 25 35 25 10 and, in general, with multiple matrices with intersecting colnames? thanks, murali __ 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. === P Please consider the environment before printing this e-mail Cleveland Clinic is ranked one of the top hospitals in America by U.S. News World Report (2008). Visit us online at http://www.clevelandclinic.org for a complete listing of our services, staff and locations. Confidentiality Note: This message is intended for use ...{{dropped:11}} __ 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] adding matrices with common column names
folks, if i have three matrices, a, b, cc with some colnames in common, and i want to create a matrix which consists of the common columns added up, and the other columns tacked on, what's a good way to do it? i've got the following roundabout code for two matrices, but if the number of matrices increases, then i'm a bit stymied. a - matrix(1:20,ncol=4); colnames(a) - c(a,b,c,d) b - matrix(1:20,ncol=4); colnames(b) - c(b,c,d, e) cbind(a[,!(colnames(a) %in% colnames(b)), drop = FALSE], a[,intersect(colnames(a),colnames(b))] + b[,intersect(colnames(a),colnames(b)), drop = FALSE], b[,!(colnames(b) %in% colnames(a)), drop = FALSE]) a b c d e [1,] 1 7 17 27 16 [2,] 2 9 19 29 17 [3,] 3 11 21 31 18 [4,] 4 13 23 33 19 [5,] 5 15 25 35 20 now, what if i had a matrix cc? i want to perform the above operation on all three matrices a, b, cc. cc - matrix(1:10,ncol=2); colnames(cc) - c(e,f) i need to end up with: a b c d e f [1,] 1 7 17 27 17 6 [2,] 2 9 19 29 19 7 [3,] 3 11 21 31 21 8 [4,] 4 13 23 33 23 9 [5,] 5 15 25 35 25 10 and, in general, with multiple matrices with intersecting colnames? thanks, murali __ 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] Goldbach partitions code
Folks, I put up a brief note describing my naive attempts to compute Goldbach partitions, starting with a brute-force approach and refining progressively. http://jostamon.blogspot.com/2009/02/goldbachs-comet.html I'd welcome your suggestions on improvements, alternatives, other optimisations, esp. to do with space vs time tradeoffs. Is this an example interesting enough for pedagogical purposes, do you think? Please advise. Cheers, MM [[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.