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] R beginner: matrix algebra
Thanks to all for help. The filter function appears most straightforward way for this problem. Kevin -- View this message in context: http://r.789695.n4.nabble.com/R-beginner-matrix-algebra-tp4653335p4653414.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.
[R] R beginner: matrix algebra
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.
Re: [R] R beginner: matrix algebra
I think this is what you are looking for. tmp - matrix(sample(20), 5, 4) tmp [,1] [,2] [,3] [,4] [1,]6 15 18 20 [2,]45 10 19 [3,]7913 [4,]8 14 11 13 [5,] 17 12 162 t(apply(tmp, 1, diff)) [,1] [,2] [,3] [1,]932 [2,]159 [3,]2 -82 [4,]6 -32 [5,] -54 -14 On Mon, Dec 17, 2012 at 12:16 PM, kevj1980 kevin.kid...@cameronhume.comwrote: 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. [[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] R beginner: matrix algebra
diff(x) will returns the diff's of each column when x is a matrix, so there is no need for the apply call. E.g., d - ts(cbind(SQRE=(1:7)^2, CUBE=(1:7)^3), start=2012.25, deltat=1/4) d SQRE CUBE 2012 Q211 2012 Q348 2012 Q49 27 2013 Q1 16 64 2013 Q2 25 125 2013 Q3 36 216 2013 Q4 49 343 diff(d) SQRE CUBE 2012 Q337 2012 Q45 19 2013 Q17 37 2013 Q29 61 2013 Q3 11 91 2013 Q4 13 127 filter() will do it also and may be more convenient because its output is the same length as its input: filter(d, c(1,-1), sides=1) [,1] [,2] 2012 Q2 NA NA 2012 Q337 2012 Q45 19 2013 Q17 37 2013 Q29 61 2013 Q3 11 91 2013 Q4 13 127 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 Richard M. Heiberger Sent: Monday, December 17, 2012 1:54 PM To: kevj1980 Cc: r-help@r-project.org Subject: Re: [R] R beginner: matrix algebra I think this is what you are looking for. tmp - matrix(sample(20), 5, 4) tmp [,1] [,2] [,3] [,4] [1,]6 15 18 20 [2,]45 10 19 [3,]7913 [4,]8 14 11 13 [5,] 17 12 162 t(apply(tmp, 1, diff)) [,1] [,2] [,3] [1,]932 [2,]159 [3,]2 -82 [4,]6 -32 [5,] -54 -14 On Mon, Dec 17, 2012 at 12:16 PM, kevj1980 kevin.kid...@cameronhume.comwrote: 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. [[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.