Re: [R] new to R coding.
Not quite sure what you were trying to do, but try the slight modification below and see if it works for you: x = c (2434.1, 2463.7, 2451.6, 2444.5, 2431.3, 2436.3, 2412.6, 2417.9, 2380.1, 2366.2, 2349.1, 2373.9, 2336.9, 2335.0, 2297.1, 2291.1, 2278.6, 2289.9, 2314.5, 2328.8, 2322.0, 2337.3, 2345.8, 2355.1, 2348.4, 2321.1, 2307.4, 2331.0, 2313.6, 2286.9, 2293.2, 2298.3, 2313.8, 2325.9, 2297.1, 2277.0, 2259.7, 2269.2, 2236.7, 2249.3, 2254.8, 2255.4, 2238.4, 2254.8, 2230.5, 2216.0, 2230.3, 2250.0, 2234.3, .8, 2224.5, 2226.1, 2234.9, 2263.9, 2238.0, 2259.7, 2250.3, 2258.9, 2283.9, 2298.2, 2266.2, 2275.0, 2263.0, 2247.9, 2221.6, 2240.7, 2231.6, 2239.5, 2221.1) #extraction of the stock index returns n=length(x) d=diff (log(x)) # daily log returns # d is shorter than x n=length(d) # to get dx to index in the loop it must be defined before loop begins dx = NULL for (i in 2:n) { dx[i]=(d[i])/(d[i-1]) # delta doesn't really do anything so I commented out # delta=dx[i] } # this is what I have tried to do. -Original Message- From: tynashy Sent: Monday, October 24, 2011 11:29 AM To: r-help@r-project.org Subject: Re: [R] new to R coding. I am new to R coding and I am trying to model the returns on the ftse100 since 1990. I have got a vector with all the closing values on each trading day. however, instead of using the difference in the closing values of two consecutive days, (ie dx=diff(x) where x is the vector containing the closing values), i wanted to use the quotient of the two closing values. I have tried the following without any luck x = c (2434.1 2463.7 2451.6 2444.5 2431.3 2436.3 2412.6 2417.9 2380.1 2366.2 2349.1 2373.9 2336.9 2335.0 2297.1 2291.1 2278.6 2289.9 2314.5 2328.8 2322.0 2337.3 2345.8 2355.1 2348.4 2321.1 2307.4 2331.0 2313.6 2286.9 2293.2 2298.3 2313.8 2325.9 2297.1 2277.0 2259.7 2269.2 2236.7 2249.3 2254.8 2255.4 2238.4 2254.8 2230.5 2216.0 2230.3 2250.0 2234.3 .8 2224.5 2226.1 2234.9 2263.9 2238.0 2259.7 2250.3 2258.9 2283.9 2298.2 2266.2 2275.0 2263.0 2247.9 2221.6 2240.7 2231.6 2239.5 2221.1) #extraction of the stock index returns n=length(x) d=diff (log(x)) # daily log returns for (i in 2:n) { + dx[i]=(d[i])/(d[i-1]) + delta=dx[i] + } # this is what I have tried to do. how do i do this properly in R please. -- View this message in context: http://r.789695.n4.nabble.com/new-to-R-coding-tp3933588p3933738.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. -- Robert W. Baer, Ph.D. Professor of Physiology Kirksville College of Osteopathic Medicine A. T. Still University of Health Sciences 800 W. Jefferson St. Kirksville, MO 63501 660-626-2322 FAX 660-626-2965 __ 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 to R coding.
Assuming that d(x) is equal to x, (I don't know a d() function in R) these should be the same. log(a/b) = log(a) - log(b) = diff(log(c(a,b)) If you mean simple returns instead of continuous/log returns, perhaps try this: x[-1]/x[-length(x)] - 1 Michael On Mon, Oct 24, 2011 at 11:44 AM, tynashy tyna...@yahoo.co.uk wrote: how do I code the following in R. I want to produce a vector where dx=log( (d(x))/(d(x-1)) ). I can do it for dx=diff(log(x)). I am learning/trying to model log returns of a stock market index. But instead of using the difference of the closing values of two consecutive days, i want to use the log of the quotient of the two days. any help is most appreciated. d is a vector of the closing values of the stock market index of length 5000. -- View this message in context: http://r.789695.n4.nabble.com/new-to-R-coding-tp3933588p3933588.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-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 to R coding.
Try it yourself: x = seq(1, 11, by = 2) diff(log(x)) log(x[-1]/x[-length(x)]) all.equal(diff(log(x)), log(x[-1]/x[-length(x)])) It seems like you don't really understand logs / log returns and why they are used by some in quant finance: might I suggest you read this: http://quantivity.wordpress.com/2011/02/21/why-log-returns/ My quick explanation is that log returns aggregate nicely over time while simple returns aggregate nicely over a portfolio. Thus, for a single instrument portfolio, log returns play very nicely with vectorized thinking in R. Michael On Mon, Oct 24, 2011 at 12:39 PM, tyna...@yahoo.co.uk wrote: lets say x(t)=c(1,3,5,7,9,11) #value of the index at close of trading day does this mean these two are the same? d(t) = diff(log( x(t) ) and d(t) = log ( x(t)/x(t-1) ) quote author='Michael Weylandt' Assuming that d(x) is equal to x, (I don't know a d() function in R) these should be the same. log(a/b) = log(a) - log(b) = diff(log(c(a,b)) If you mean simple returns instead of continuous/log returns, perhaps try this: x[-1]/x[-length(x)] - 1 Michael On Mon, Oct 24, 2011 at 11:44 AM, tynashy tyna...@yahoo.co.uk wrote: how do I code the following in R. I want to produce a vector where dx=log( (d(x))/(d(x-1)) ). I can do it for dx=diff(log(x)). I am learning/trying to model log returns of a stock market index. But instead of using the difference of the closing values of two consecutive days, i want to use the log of the quotient of the two days. any help is most appreciated. d is a vector of the closing values of the stock market index of length 5000. -- View this message in context: http://r.789695.n4.nabble.com/new-to-R-coding-tp3933588p3933588.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-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. /quote Quoted from: http://r.789695.n4.nabble.com/new-to-R-coding-tp3933588p3933681.html __ 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 to R coding.
I am new to R coding and I am trying to model the returns on the ftse100 since 1990. I have got a vector with all the closing values on each trading day. however, instead of using the difference in the closing values of two consecutive days, (ie dx=diff(x) where x is the vector containing the closing values), i wanted to use the quotient of the two closing values. I have tried the following without any luck x = c (2434.1 2463.7 2451.6 2444.5 2431.3 2436.3 2412.6 2417.9 2380.1 2366.2 2349.1 2373.9 2336.9 2335.0 2297.1 2291.1 2278.6 2289.9 2314.5 2328.8 2322.0 2337.3 2345.8 2355.1 2348.4 2321.1 2307.4 2331.0 2313.6 2286.9 2293.2 2298.3 2313.8 2325.9 2297.1 2277.0 2259.7 2269.2 2236.7 2249.3 2254.8 2255.4 2238.4 2254.8 2230.5 2216.0 2230.3 2250.0 2234.3 .8 2224.5 2226.1 2234.9 2263.9 2238.0 2259.7 2250.3 2258.9 2283.9 2298.2 2266.2 2275.0 2263.0 2247.9 2221.6 2240.7 2231.6 2239.5 2221.1) #extraction of the stock index returns n=length(x) d=diff (log(x)) # daily log returns for (i in 2:n) { + dx[i]=(d[i])/(d[i-1]) + delta=dx[i] + } # this is what I have tried to do. how do i do this properly in R please. -- View this message in context: http://r.789695.n4.nabble.com/new-to-R-coding-tp3933588p3933738.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] new to R coding.
I believe I already showed you how to do this (though your code doesn't seem to do what your words ask, so I'm not sure which one to answer), but didn't draw attention to it: x[-1]/x[-length(x)] This creates two vectors, one consisting of everything but the first element of x and the other of everything but the last, and divides them, which gives the consecutive quotients you want. Also, if you would, next time you provide sample data (which is a wonderful thing to do) -- provide it in a directly pastable format (i.e., with commas for c()). The easiest way to get such data is just to dput(x) and copy the output. E.g., for this data I have lying around: P = c(128.59, 128.87, 127.84, 128.43, 128.32, 128.75, 129.31, 130.29, 130.04, 130.09, 129.66, 129.91, 129.51, 129.89, 130.94, 129.4, 129.28, 129.21, 129.88, 130.76, 131.54, 131.72, 131.76, 131.79, 132.09, 131.91, 130.93, 130.49, 131.59, 132.45, 132.62, 132.56, 132.84, 132.79, 132.69, 132.17, 132.05, 126.89, 128.19, 127.81, 126.14, 124.94, 127.07, 126.95, 128.02, 128.06, 128.25, 125.76, 126.69, 126.86, 126.51, 128.03, 128.74, 130.86, 130.76, 130.95, 130.77, 130.46, 129.51, 129.65, 129.68, 129.82, 131.22, 131.37, 131.72, 131.91, 132.06, 131.52, 132.11, 132.71, 133.97, 134.33, 134.49, 134.45, 135.72, 135.21, 135.27, 136.51, 136.66, 136.56, 135.42, 135.77, 136.56, 137.3, 137.82, 137.85, 137.67, 138.04, 136.6, 137.77, 137.47, 137.5, 138.45, 138.17, 139.38, 139.3, 139.19, 139.21, 137.95, 138.53) we get head(P[-1]/P[-length(P)]) 1.0021775 0.9920074 1.0046151 0.9991435 1.0033510 1.0043495 which I believe is what you are after. Michael On Mon, Oct 24, 2011 at 12:29 PM, tynashy tyna...@yahoo.co.uk wrote: I am new to R coding and I am trying to model the returns on the ftse100 since 1990. I have got a vector with all the closing values on each trading day. however, instead of using the difference in the closing values of two consecutive days, (ie dx=diff(x) where x is the vector containing the closing values), i wanted to use the quotient of the two closing values. I have tried the following without any luck x = c (2434.1 2463.7 2451.6 2444.5 2431.3 2436.3 2412.6 2417.9 2380.1 2366.2 2349.1 2373.9 2336.9 2335.0 2297.1 2291.1 2278.6 2289.9 2314.5 2328.8 2322.0 2337.3 2345.8 2355.1 2348.4 2321.1 2307.4 2331.0 2313.6 2286.9 2293.2 2298.3 2313.8 2325.9 2297.1 2277.0 2259.7 2269.2 2236.7 2249.3 2254.8 2255.4 2238.4 2254.8 2230.5 2216.0 2230.3 2250.0 2234.3 .8 2224.5 2226.1 2234.9 2263.9 2238.0 2259.7 2250.3 2258.9 2283.9 2298.2 2266.2 2275.0 2263.0 2247.9 2221.6 2240.7 2231.6 2239.5 2221.1) #extraction of the stock index returns n=length(x) d=diff (log(x)) # daily log returns for (i in 2:n) { + dx[i]=(d[i])/(d[i-1]) + delta=dx[i] + } # this is what I have tried to do. how do i do this properly in R please. -- View this message in context: http://r.789695.n4.nabble.com/new-to-R-coding-tp3933588p3933738.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-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.