Hi, R-users,
data is a matrix like this
AMR BS GE HR MO UK SP500
1974 -0.3505 -0.1154 -0.4246 -0.2107 -0.0758 0.2331 -0.2647
1975 0.7083 0.2472 0.3719 0.2227 0.0213 0.3569 0.3720
1976 0.7329 0.3665 0.2550 0.5815 0.1276 0.0781 0.2384
1977 -0.2034 -0.4271 -0.0490 -0.0938 0.0712 -0.2721 -0.0718
1978 0.1663 -0.0452 -0.0573 0.2751 0.1372 -0.1346 0.0656
1979 -0.2659 0.0158 0.0898 0.0793 0.0215 0.2254 0.1844
1980 0.0124 0.4751 0.3350 -0.1894 0.2002 0.3657 0.3242
1981 -0.0264 -0.2042 -0.0275 -0.7427 0.0913 0.0479 -0.0491
1982 1.0642 -0.1493 0.6968 -0.2615 0.2243 0.0456 0.2141
1983 0.1942 0.3680 0.3110 1.8682 0.2066 0.2640 0.2251
I want to calculate the return say AMR,so I use
re=numeric(10)
for (i in 2:nrow(data))
re[1]=0
re[i]=log(data[i]/data[i-1])
to my surprise, the result is> re
[1] 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
[9] 0.00000 -1.70109
I don't know what's wrong with my code, and is there alternative way
to do the same?
thanks
Denver
Hi Denver,> I want to calculate the return say AMR,so I useIf data is a matrix or a data.frame you need to use the correct index: [row,col] instead of [row]. Try: re=numeric(10) for (i in 2:nrow(data)) { re[1]=0 re[i]=log(data[i,1]/data[i-1,1]) } This works, but of course you have negative values of the ratio, so the log cannot be computed. See also ?apply.
hi,scionforbai, Thank you very much. I made very stupid mistake for the number is not stock price. In fact, if the number is correct, data[i,1] is equivalent to data[i] 2007/11/24, Scionforbai <scionforbai at gmail.com>:> Hi Denver, > > > I want to calculate the return say AMR,so I use > > If data is a matrix or a data.frame you need to use the correct > index: [row,col] instead of [row]. Try: > > re=numeric(10) > for (i in 2:nrow(data)) { > re[1]=0 > re[i]=log(data[i,1]/data[i-1,1]) > } > > This works, but of course you have negative values of the ratio, so > the log cannot be computed. > See also ?apply. >
Are you sure its a matrix? or is it a data frame?> BOD # built into RTime demand 1 1 8.3 2 2 10.3 3 3 19.0 4 4 16.0 5 5 15.6 6 7 19.8> class(BOD)[1] "data.frame"> BOD[2]demand 1 8.3 2 10.3 3 19.0 4 16.0 5 15.6 6 19.8> BOD[[2]][1] 8.3 10.3 19.0 16.0 15.6 19.8> BOD[,2][1] 8.3 10.3 19.0 16.0 15.6 19.8> bod <- as.matrix(BOD) > class(bod)[1] "matrix"> bod[2][1] 2> bod[,2][1] 8.3 10.3 19.0 16.0 15.6 19.8 You may wish to review the Introduction to R manual and the many contributed introductions at the first hit found by googling for CRAN contributed documentation On Nov 24, 2007 9:07 AM, Denver XU <financialengine at gmail.com> wrote:> Hi, R-users, > data is a matrix like this > AMR BS GE HR MO UK SP500 > 1974 -0.3505 -0.1154 -0.4246 -0.2107 -0.0758 0.2331 -0.2647 > 1975 0.7083 0.2472 0.3719 0.2227 0.0213 0.3569 0.3720 > 1976 0.7329 0.3665 0.2550 0.5815 0.1276 0.0781 0.2384 > 1977 -0.2034 -0.4271 -0.0490 -0.0938 0.0712 -0.2721 -0.0718 > 1978 0.1663 -0.0452 -0.0573 0.2751 0.1372 -0.1346 0.0656 > 1979 -0.2659 0.0158 0.0898 0.0793 0.0215 0.2254 0.1844 > 1980 0.0124 0.4751 0.3350 -0.1894 0.2002 0.3657 0.3242 > 1981 -0.0264 -0.2042 -0.0275 -0.7427 0.0913 0.0479 -0.0491 > 1982 1.0642 -0.1493 0.6968 -0.2615 0.2243 0.0456 0.2141 > 1983 0.1942 0.3680 0.3110 1.8682 0.2066 0.2640 0.2251 > I want to calculate the return say AMR,so I use > re=numeric(10) > for (i in 2:nrow(data)) > re[1]=0 > re[i]=log(data[i]/data[i-1]) > to my surprise, the result is > > re > [1] 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 > [9] 0.00000 -1.70109 > I don't know what's wrong with my code, and is there alternative way > to do the same? > thanks > > Denver > > ______________________________________________ > R-help at 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. >
thank you Gabor, I am sure my data is a data.frame and I think you a lillte bit misunderstood my question. thank you Patrick, your answer is what I want to find. 2007/11/24, Denver XU <financialengine at gmail.com>:> Hi, R-users, > data is a matrix like this > AMR BS GE HR MO UK SP500 > 1974 -0.3505 -0.1154 -0.4246 -0.2107 -0.0758 0.2331 -0.2647 > 1975 0.7083 0.2472 0.3719 0.2227 0.0213 0.3569 0.3720 > 1976 0.7329 0.3665 0.2550 0.5815 0.1276 0.0781 0.2384 > 1977 -0.2034 -0.4271 -0.0490 -0.0938 0.0712 -0.2721 -0.0718 > 1978 0.1663 -0.0452 -0.0573 0.2751 0.1372 -0.1346 0.0656 > 1979 -0.2659 0.0158 0.0898 0.0793 0.0215 0.2254 0.1844 > 1980 0.0124 0.4751 0.3350 -0.1894 0.2002 0.3657 0.3242 > 1981 -0.0264 -0.2042 -0.0275 -0.7427 0.0913 0.0479 -0.0491 > 1982 1.0642 -0.1493 0.6968 -0.2615 0.2243 0.0456 0.2141 > 1983 0.1942 0.3680 0.3110 1.8682 0.2066 0.2640 0.2251 > I want to calculate the return say AMR,so I use > re=numeric(10) > for (i in 2:nrow(data)) > re[1]=0 > re[i]=log(data[i]/data[i-1]) > to my surprise, the result is > > re > [1] 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 > [9] 0.00000 -1.70109 > I don't know what's wrong with my code, and is there alternative way > to do the same? > thanks > > Denver >
Denver,> data is a matrix like this > AMR BS GE HR MO UK SP500 > 1974 -0.3505 -0.1154 -0.4246 -0.2107 -0.0758 0.2331 -0.2647 > 1975 0.7083 0.2472 0.3719 0.2227 0.0213 0.3569 0.3720 > 1976 0.7329 0.3665 0.2550 0.5815 0.1276 0.0781 0.2384 > 1977 -0.2034 -0.4271 -0.0490 -0.0938 0.0712 -0.2721 -0.0718 > 1978 0.1663 -0.0452 -0.0573 0.2751 0.1372 -0.1346 0.0656 > 1979 -0.2659 0.0158 0.0898 0.0793 0.0215 0.2254 0.1844 > 1980 0.0124 0.4751 0.3350 -0.1894 0.2002 0.3657 0.3242 > 1981 -0.0264 -0.2042 -0.0275 -0.7427 0.0913 0.0479 -0.0491 > 1982 1.0642 -0.1493 0.6968 -0.2615 0.2243 0.0456 0.2141 > 1983 0.1942 0.3680 0.3110 1.8682 0.2066 0.2640 0.2251It is perhaps more natural to specify this information as a data frame.> I want to calculate the return say AMR,so I use > re=numeric(10) > for (i in 2:nrow(data)) > re[1]=0 > re[i]=log(data[i]/data[i-1]) > to my surprise, the result is > > re > [1] 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.000000.00000> [9] 0.00000 -1.70109A couple of things: 1. You don't need re[1]=0 inside the loop (or at all, in fact, since numeric(10) will give a default value of 0). 2. It would be better to vectorise the code, to avoid the loop altogether. 3. You are looping over elements in the first column of data, which is why the log values are zero. Some better code: re = c(0, diff(log(data[,"AMR"]))) Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}