Padding out missing values with NAs - r

I have loop to analysis of 25 time series, In the analysis I need to get the mean of the individual columns for each station , for example the mean of all the Januaries, the same for all the Februaries, etc., etc. To complicate matters the time series are not all the same length, for example station 1 might run from 1900 to 1955 while station 2 might run from 1881 to 1945. So I need the mean of a Januaries of station 1, the mean of all Februaries of Station 1, etc., etc. and the same process for station 2 etc., etc. My times do not all start in January or end in December, but can start and finish in any month, each time series is individual. To get the colMeans, I need to change the time series to a matrix, but I need to pad out the empty spaces with NAs. Now how can I do that and put the function into the loop
Below is an example of my data
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1951 15.50 18.74 22.75 25.90 25.43 27.61
1952 27.60 27.72 27.63 24.38 20.34 17.74 17.90 20.57 23.13 25.60 26.41 26.98
1953 25.80 26.19 24.99 23.23 19.59 15.78 14.85 18.97 20.44 25.78 26.65 27.00
1954 26.25 26.97 25.33 23.16 20.47 15.47 15.64 18.33 22.71 26.71 25.77 25.94
1955 26.69 25.36 24.19 23.42 19.65 17.10 17.36 18.67 20.95 24.41 24.93 26.12
1956 26.02 26.48 25.81 23.91 20.78 17.40 17.48 19.96 21.06 25.44 26.16 25.92
1957 26.67 28.03 25.24 24.40 19.89 16.54 17.99 19.01 24.81 26.18 28.38 26.96
1958 25.90 26.49 24.90 24.67 21.36 16.19 16.29 17.20 22.18 24.52 29.13 26.65
1959 26.53 26.65 25.17 24.26 20.67 17.56 18.11 18.49 21.50 26.21 26.48 27.52
1960 27.25 26.04 26.58 22.80 19.41 17.16 15.57 20.24 22.86 26.68 25.71 27.58
1961 26.79 25.88 26.19 24.22 22.09 17.77 17.91 18.56 23.27 24.94 25.68 26.66
1962 27.03 28.11 26.05 23.81 18.79 17.32 16.04 19.23 23.14 27.57 27.37 27.09
1963 26.91 26.68 24.97 22.87 18.71 16.79 16.05 18.25 23.52 25.73 27.08 26.86
1964 28.63 28.04 28.16 23.98 19.78 15.40 14.98 18.32 22.88 25.60 26.55 25.23
1965 27.77 28.87 26.62 23.40 19.49 15.62 17.14 19.79 22.09 23.44 26.32 28.40
1966 29.68 26.63 25.50 23.13 19.49 18.65 17.69 19.32 22.12 23.88 27.37 27.75
1967 27.84 26.46 25.75 24.20 20.15 17.22 15.64 18.39 22.41 25.38 27.42 27.62
1968 30.27 27.91 26.32 22.56 19.86 14.16 17.07 19.76 22.42 26.05 24.61 26.38
1969 28.72 30.04 25.85 23.68 20.09 18.32 16.85 19.61 22.10 24.97 27.28 25.46
1970 29.62 27.24 26.62 23.34 20.20 16.95 17.40 20.23 24.21 24.74 27.25 28.71
1971 26.25 26.44 28.15 25.31 19.14 16.21 16.92 19.12 23.09 24.28 24.43 27.19
1972 26.64 26.07 25.17 24.13 19.45 15.83 16.14 18.45 22.74 24.98 25.54 30.09
1973 30.44 28.54 28.10 22.80 20.36 18.05 16.74 19.16 23.56 24.64 25.97 25.50
1974 27.46 26.52 25.44 22.36 19.79 16.83 16.70 19.89 21.39 26.22 25.84 25.93
1975 27.84 25.91 24.42 22.87 20.90 16.42 16.25 18.79 23.24 24.54 27.07 26.97
1976 26.05 26.33 24.95 22.07 18.42 16.88 15.79 17.24 23.01 25.26 27.65 29.15
1977 30.00 26.31 23.47 23.17 20.21 17.23 16.12 18.80 23.55 25.93 26.97 26.68
1978 25.99 26.36 26.31 22.49 19.72 15.88 15.99 21.64 23.57 24.68 24.78 26.47
1979 26.98 28.17 25.36 24.06 20.65 17.64 15.98 19.89 22.63 25.40 25.31 27.03
1980 27.74 28.05 25.47 23.72 20.55 15.96

Related

How to calculate average for columns?

I need to find the average of every 6 months, starting from v1 to v15. Now that i know that there are v15 columns hence its working with my below code. But there will more than 15 columns and I need a generic code that can solve the purpose.
Logic i am using is: taking the average of columns - 1:6 and printing, then 2:7 and so on- till 15, as i know there are 15 columns. But there will more columns in actual.
csv file:
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15
1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
2 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.11 0.04 0.04 0.04 0.04 0.04 0.04 0.04
3 3.29 3.56 3.97 3.23 2.96 2.35 0.06 1.72 2.19 1.92 1.84 2.87 2.57 2.24 3.06
4 11.79 15.01 14.76 13.19 18.29 4.51 16.24 11.92 10.49 13.05 12.74 12.95 12.25 14.46 14.27
5 20.11 21.76 21.92 23.67 19.87 25.59 23.04 16.67 22.78 21.32 20.85 21.57 21.99 22.69 22.96
6 24.85 26.56 29.45 24.96 25.91 16.31 27.51 22.56 28.35 26.96 26.53 28.23 28.24 29.85 29.79
7 29.02 32.75 29.95 27.7 29.6 17.91 32.08 25.71 33.16 31.56 30.89 32.68 34.05 36.26 33.27
8 32.83 33.09 17.03 33.23 31.22 39.71 35.43 28.77 37.09 34.18 34.05 36.98 37.16 38.74 37.32
9 32.86 36.34 35.47 33.6 35 42.79 37.22 30.62 38.74 35.83 36.17 39.48 39.18 42.87 39.54
10 36.02 37.66 36.15 34.79 36.84 22.19 38.9 32.62 40.28 37.87 38.09 41.04 41.62 44.94 42.18
11 36.96 39.22 19.13 36.68 37.43 46.26 40.84 33.88 41.31 39.09 39.14 43.46 42.75 47.2 43.8
12 37.34 40.87 35.91 37.66 39.22 46.95 42.26 35.19 42.93 41 40.61 44.73 45.2 48.14 44.49
13 38.92 38.37 41.01 39.01 41 48.89 43.8 37.16 44.1 42.46 41.3 45.47 46.65 50.48 47.6
14 21.67 43.16 20.98 39.84 42 49.62 44.35 37.46 44.63 43.15 42.64 48.48 48.53 53.55 48.57
a <- t(apply(mat,1,function(x){ c(mean(x[1:6]),mean(x[2:7]),mean(x[3:8]),mean(x[4:9]),mean(x[5:10]),mean(x[6:11]),mean(x[7:12]),mean(x[8:13]),mean(x[9:14]),mean(x[10:15])) }))
Please help. thanks in Advance.
We can do this with a rolling mean (rollmean
library(zoo)
t(apply(df1, 1, function(x) rollmean(x, 6)))
Using base R:
n=6
d=lapply(1:(ncol(data)-(n-1)),function(x) x:(x+n-1))
sapply(d,function(w) rowMeans(data[,w]))
another base solution:
rowlingRowMeans <- function(matrix, n_meanrows){
out <- NULL
for(z in 1:(nrow(matrix)-n_meanrows+2)){
out <- cbind(out, rowMeans(matrix[,z:(z+n_meanrows-1)]))
}
return(out)
}
mat <- matrix(rnorm(15*14, 1,10), ncol=15, nrow=14)
rowlingRowMeans(mat, 6)

PerformanceAnalytics + data.frame: Formatting issue when using package functions

I'm trying to pass a data.frame with dates in the rownames into a PerformanceAnalytics (package) function.
My data is below, I get the following error message when trying to run the code
> charts.PerformanceSummary(x$Return)
Error in checkData(R) :
The data cannot be converted into a time series. If you are trying to pass in names from a data object with one column, you should use the form 'data[rows, columns, drop = FALSE]'. Rownames should have standard date formats, such as '1985-03-15'.
How can this be solved? I thought that PerformanceAnalytics would recognize my rows as dates automatically.
My data
> str(x)
'data.frame': 201 obs. of 7 variables:
$ Open : num 69 67.8 64 67.2 68 ...
$ High : num 70 69 67.8 68.8 70.8 ...
$ Low : num 67 66 63.2 64.5 67.5 ...
$ Close : num 68.5 66 67.2 68 68.5 ...
$ Volume : num 2163200 2456100 2783000 2200300 2418000 ...
$ Adj.Close: num 55 53 54 54.6 55 ...
$ Return : num 0 0 0 0 0 0 0 0 0 0 ...
> x
Open High Low Close Volume Adj.Close Return
2008-11-28 69.00 70.00 67.00 68.50 2163200 54.9989 0.000000000
2008-12-01 67.75 69.00 66.00 66.00 2456100 52.9916 0.000000000
2008-12-02 64.00 67.75 63.25 67.25 2783000 53.9953 0.000000000
2008-12-03 67.25 68.75 64.50 68.00 2200300 54.5974 0.000000000
2008-12-04 68.00 70.75 67.50 68.50 2418000 54.9989 0.000000000
2008-12-05 68.00 68.75 63.50 64.50 2162200 51.7873 0.000000000
2008-12-08 67.25 73.50 67.25 72.00 4363100 57.8090 0.000000000
2008-12-09 71.25 76.25 70.00 74.50 4266500 59.8163 0.000000000
2008-12-10 73.75 78.00 73.00 77.50 3395900 62.2250 0.000000000
2008-12-11 76.75 76.75 74.25 76.00 2474600 61.0207 0.000000000
2008-12-12 74.00 74.75 72.25 74.50 3141800 59.8163 0.000000000
2008-12-15 70.00 74.50 67.25 67.25 7125600 53.9953 0.000000000
2008-12-16 66.25 69.50 66.00 69.50 3063200 55.8018 0.000000000
2008-12-17 69.25 70.00 67.00 70.00 2543400 56.2032 0.000000000
2008-12-18 69.50 73.50 69.25 72.25 4366000 58.0098 0.000000000
2008-12-19 70.50 72.00 69.75 71.75 2346600 57.6083 0.000000000
2008-12-22 70.50 71.00 68.25 69.75 3057600 56.0025 0.000000000
2008-12-23 68.75 69.50 67.50 69.25 2327700 55.6011 0.000000000
2008-12-24 69.25 69.25 69.25 69.25 0 55.6011 0.000000000
2008-12-25 69.25 69.25 69.25 69.25 0 55.6011 0.000000000
2008-12-26 69.25 69.25 69.25 69.25 0 55.6011 0.000000000
2008-12-29 69.00 69.75 67.50 68.00 1187600 54.5974 0.000000000
2008-12-30 67.75 68.50 66.50 66.75 1888700 53.5938 0.000000000
2008-12-31 66.75 66.75 66.75 66.75 0 53.5938 0.000000000
2009-01-01 66.75 66.75 66.75 66.75 0 53.5938 0.000000000
2009-01-02 67.75 72.75 67.00 72.25 1887500 58.0098 0.000000000
2009-01-05 73.75 75.50 73.00 74.25 2020400 59.6156 0.000000000
2009-01-06 74.25 74.25 74.25 74.25 0 59.6156 0.000000000
2009-01-07 73.75 75.00 73.50 74.50 2962600 59.8163 0.000000000
2009-01-08 73.00 74.25 71.00 72.25 2239100 58.0098 0.000000000
2009-01-09 72.00 72.25 70.25 71.25 1983600 57.2069 0.000000000
2009-01-12 69.50 71.00 69.00 69.75 1487000 56.0025 0.000000000
2009-01-13 68.75 69.75 67.75 68.75 2859100 55.1996 0.000000000
2009-01-14 69.75 69.75 63.25 64.25 2586900 51.5865 0.000000000
2009-01-15 64.00 66.50 60.75 63.00 4090800 50.5829 0.000000000
2009-01-16 65.00 66.00 64.00 64.75 2455600 51.9880 0.000000000
2009-01-19 66.00 66.00 64.75 65.50 2839200 52.5902 0.000000000
2009-01-20 66.00 66.75 62.25 62.75 2787600 50.3822 0.000000000
2009-01-21 62.00 67.50 61.75 65.25 5090700 52.3894 0.000000000
2009-01-22 67.00 69.00 65.50 65.75 2994600 52.7909 0.000000000
2009-01-23 64.75 66.00 61.25 62.00 4463400 49.7800 0.000000000
2009-01-26 62.00 65.75 61.50 65.75 3698700 52.7909 0.000000000
2009-01-27 66.00 67.25 64.75 65.75 3445900 52.7909 0.000000000
2009-01-28 66.25 69.00 66.00 67.00 2766400 53.7945 0.000000000
2009-01-29 67.00 67.25 62.25 62.75 2090700 50.3822 0.000000000
2009-01-30 63.00 63.75 59.00 60.50 6437600 48.5757 0.000000000
2009-02-02 60.25 62.00 58.75 60.25 3267600 48.3749 0.000000000
2009-02-03 60.75 62.50 59.25 62.50 3148000 50.1815 0.000000000
2009-02-04 62.75 69.50 61.75 68.25 6579500 54.7982 0.000000000
2009-02-05 66.00 69.50 66.00 69.00 6733600 55.4003 0.000000000
2009-02-06 69.00 71.00 68.25 70.00 5031600 56.2032 0.000000000
2009-02-09 69.50 72.00 68.75 71.75 3794200 57.6083 0.000000000
2009-02-10 70.25 72.00 69.75 70.75 2991500 56.8054 0.000000000
2009-02-11 68.75 70.75 68.25 68.50 2566400 54.9989 0.000000000
2009-02-12 67.75 70.25 66.75 70.00 3083800 56.2032 0.000000000
2009-02-13 70.25 71.50 68.00 69.25 2227000 55.6011 0.000000000
2009-02-16 68.50 69.25 67.25 68.75 1087100 55.1996 0.000000000
2009-02-17 67.50 67.50 65.00 65.50 1630200 52.5902 0.000000000
2009-02-18 65.00 65.50 62.50 65.00 40400 52.1887 0.000000000
2009-02-19 65.00 68.00 64.50 66.50 2701100 53.3931 0.000000000
2009-02-20 64.25 65.00 60.75 61.25 5534300 49.1778 0.000000000
2009-02-23 62.00 62.50 58.50 58.75 2524300 47.1706 0.000000000
2009-02-24 58.75 60.25 56.75 58.00 5117600 46.5684 0.000000000
2009-02-25 59.50 61.75 59.25 60.25 4418400 48.3749 0.000000000
2009-02-26 61.50 64.50 60.50 63.50 6482600 50.9844 0.000000000
2009-02-27 61.50 62.75 60.00 62.00 2834500 49.7800 0.000000000
2009-03-02 61.25 61.25 58.00 59.50 3087100 47.7727 0.000000000
2009-03-03 60.00 60.75 57.75 58.50 2606300 46.9698 0.000000000
2009-03-04 60.00 60.00 58.00 59.75 4554800 47.9735 0.000000000
2009-03-05 59.00 59.75 57.25 58.00 2777600 46.5684 0.000000000
2009-03-06 56.50 58.75 56.50 57.50 3338300 46.1669 0.000000000
2009-03-09 58.50 58.75 57.00 58.00 3708500 46.5684 0.000000000
2009-03-10 58.00 61.00 57.50 60.25 5163300 48.3749 0.000000000
2009-03-11 60.00 62.75 59.00 61.50 3668700 49.3786 0.000000000
2009-03-12 61.50 61.75 58.50 61.25 3404600 49.1778 0.000000000
2009-03-13 63.00 64.00 60.75 62.00 4048500 49.7800 0.000000000
2009-03-16 63.00 64.50 62.25 62.50 2945300 50.1815 0.000000000
2009-03-17 62.75 62.75 60.50 60.75 2369800 48.7764 0.000000000
2009-03-18 62.25 62.50 59.25 60.25 1968600 48.3749 0.000000000
2009-03-19 61.00 64.75 60.25 64.25 3730400 51.5865 0.000000000
2009-03-20 63.75 65.00 63.50 63.50 3186200 50.9844 0.000000000
2009-03-23 64.75 65.50 63.00 65.50 2738400 52.5902 0.000000000
2009-03-24 65.50 66.00 63.00 64.50 2994200 51.7873 0.000000000
2009-03-25 63.75 69.00 62.75 67.75 4342500 54.3967 0.000000000
2009-03-26 67.00 69.25 66.75 68.00 8300 54.5974 0.000000000
2009-03-27 67.75 69.50 66.75 68.50 3407200 54.9989 0.000000000
2009-03-30 66.75 67.00 63.75 64.50 3123200 51.7873 0.000000000
2009-03-31 64.00 66.25 63.00 64.50 3261600 51.7873 0.000000000
2009-04-01 64.50 64.75 60.75 64.25 6808900 51.5865 0.000000000
2009-04-02 67.00 73.25 66.75 72.75 8180500 58.4112 0.000000000
2009-04-03 72.00 76.50 71.25 75.75 5628500 60.8199 0.000000000
2009-04-06 76.00 76.75 74.25 75.00 3238900 60.2178 0.000000000
2009-04-07 75.75 76.00 72.25 73.75 3765800 59.2141 0.000000000
2009-04-08 72.00 75.75 71.50 75.50 3177100 60.6192 0.000000000
2009-04-09 75.50 76.00 74.25 76.00 887600 61.0207 0.000000000
2009-04-10 76.00 76.00 76.00 76.00 0 61.0207 0.000000000
2009-04-13 76.00 76.00 76.00 76.00 0 61.0207 0.000000000
2009-04-14 78.00 80.75 77.00 79.00 3923200 63.4294 0.000000000
2009-04-15 78.00 79.00 75.75 76.00 2425700 61.0207 0.000000000
2009-04-16 77.50 79.50 76.00 79.25 3331200 63.6301 0.000000000
2009-04-17 79.25 83.00 79.00 81.25 4058600 65.2359 0.000000000
2009-04-20 80.00 80.50 75.75 76.75 3682800 61.6228 0.000000000
2009-04-21 76.25 76.75 73.25 75.75 3293500 60.8199 0.000000000
2009-04-22 84.00 91.50 82.00 88.00 10525000 70.6555 0.000000000
2009-04-23 89.75 92.75 88.75 90.25 5664200 72.4620 0.000000000
2009-04-24 90.75 94.75 89.50 93.75 3982600 75.2722 0.000000000
2009-04-27 92.25 100.25 91.75 97.50 4702300 78.2831 0.000000000
2009-04-28 95.75 96.00 92.25 95.00 6479800 76.2758 0.000000000
2009-04-29 94.50 97.50 92.75 95.00 2565300 76.2758 0.000000000
2009-04-30 95.00 96.75 92.00 92.00 2480500 73.8671 0.000000000
2009-05-01 92.00 92.00 92.00 92.00 0 73.8671 0.000000000
2009-05-04 95.75 100.00 94.00 100.00 3653000 80.2903 0.086956169
2009-05-05 100.00 101.75 97.75 97.75 3400 78.4838 -0.022499605
2009-05-06 98.00 104.00 96.50 102.75 4010500 82.4983 0.051150683
2009-05-07 104.25 105.00 98.50 99.75 5145800 80.0896 -0.029196965
2009-05-08 101.25 103.25 99.25 100.25 2782200 80.4911 0.005013135
2009-05-11 100.25 100.75 93.50 97.25 3925100 78.0824 -0.029925048
2009-05-12 96.50 99.00 94.00 94.50 2770700 75.8744 -0.028277819
2009-05-13 94.75 95.50 88.25 89.00 4618500 71.4584 -0.058201449
2009-05-14 89.00 92.75 87.50 91.75 2712300 73.6664 0.030899097
2009-05-15 94.00 94.00 91.00 92.50 1675400 74.2686 0.008174690
2009-05-18 91.25 97.75 91.00 97.00 3331500 77.8816 0.048647746
2009-05-19 99.25 103.75 98.75 99.50 5343000 79.8889 0.025773739
2009-05-20 100.25 101.00 98.50 100.25 1169700 80.4911 0.007537968
2009-05-21 100.25 100.25 100.25 100.25 0 80.4911 0.000000000
2009-05-22 98.00 99.50 95.75 96.75 2095400 77.6809 -0.034913177
2009-05-25 96.50 97.75 95.50 96.00 1016600 77.0787 -0.007752227
2009-05-26 94.75 96.75 90.75 96.00 3642600 77.0787 0.000000000
2009-05-27 97.50 98.00 96.75 97.50 3729400 78.2831 0.015625588
2009-05-28 96.25 99.00 96.00 98.00 3575300 78.6845 0.005127543
2009-05-29 99.25 100.00 95.25 95.50 3396900 76.6773 -0.025509471
2009-06-01 97.00 100.75 97.00 100.75 1963400 80.8925 0.054973245
2009-06-02 98.00 99.25 95.75 98.00 3547300 78.6845 -0.027295485
2009-06-03 98.75 98.75 95.75 96.75 1865100 77.6809 -0.012754736
2009-06-04 97.25 98.00 96.75 98.00 2496300 78.6845 0.012919521
2009-06-05 98.75 102.25 98.50 100.25 3226800 80.4911 0.022960049
2009-06-08 100.25 101.00 97.25 99.50 2333200 79.8889 -0.007481572
2009-06-09 100.25 103.00 100.25 102.25 4285900 82.0969 0.027638383
2009-06-10 103.25 105.00 102.50 103.50 3933000 83.1005 0.012224579
2009-06-11 105.75 109.00 105.50 108.00 7400700 86.7136 0.043478679
2009-06-12 108.50 110.50 107.50 109.25 3325200 87.7172 0.011573732
2009-06-15 108.25 108.25 103.50 104.00 1848400 83.5019 -0.048055569
2009-06-16 103.75 105.00 102.75 102.75 21000 82.4983 -0.012018888
2009-06-17 101.50 102.25 97.50 99.00 4273700 79.4874 -0.036496510
2009-06-18 99.75 100.75 98.25 100.00 1910400 80.2903 0.010100972
2009-06-19 100.00 100.00 100.00 100.00 0 80.2903 0.000000000
2009-06-22 99.00 99.75 94.00 94.50 3168000 75.8744 0.000000000
2009-06-23 94.50 98.75 94.25 97.50 2356000 78.2831 0.000000000
2009-06-24 99.00 102.50 98.00 102.00 2184800 81.8961 0.046153001
2009-06-25 101.50 103.00 100.00 103.00 1950300 82.6990 0.009803886
2009-06-26 105.00 107.25 103.75 104.50 2649900 83.9034 0.014563659
2009-06-29 105.00 109.25 104.50 108.75 2420700 87.3157 0.040669389
2009-06-30 109.75 110.50 107.25 107.75 2999500 86.5128 -0.009195368
2009-07-01 108.50 111.50 108.50 111.00 2335800 89.1223 0.030163167
2009-07-02 110.50 111.00 106.75 108.00 3151600 86.7136 -0.027026906
2009-07-03 108.00 108.50 106.00 107.00 809300 85.9107 -0.009259217
2009-07-06 106.75 107.50 104.75 106.25 2030400 85.3085 -0.007009604
2009-07-07 107.00 109.50 105.75 107.50 2644100 86.3121 0.011764361
2009-07-08 107.00 108.75 106.25 106.75 3189000 85.7099 -0.006977006
2009-07-09 108.50 110.25 108.50 109.00 3032300 87.5165 0.021078078
2009-07-10 109.25 111.50 108.25 109.25 2637000 87.7172 0.002293282
2009-07-13 108.75 111.25 106.75 110.00 1901300 88.3194 0.006865244
2009-07-14 111.25 112.00 109.25 110.75 2061800 88.9215 0.006817302
2009-07-15 112.00 116.75 111.25 116.75 4185600 93.7390 0.054176999
2009-07-16 125.00 130.50 124.25 127.50 9537100 102.3702 0.092076937
2009-07-17 129.00 131.00 126.25 129.75 2709700 104.1767 0.017646737
2009-07-20 130.00 132.00 129.50 131.00 1762400 105.1803 0.009633632
2009-07-21 131.50 132.25 126.50 127.25 3644800 102.1695 -0.028625132
2009-07-22 127.50 132.25 127.50 130.75 4810100 104.9796 0.027504294
2009-07-23 131.25 131.25 126.75 130.00 3299000 104.3774 -0.005736353
2009-07-24 129.00 133.50 129.00 133.00 52200 106.7861 0.023076835
2009-07-27 134.50 134.75 130.00 132.00 2038900 105.9832 -0.007518769
2009-07-28 132.00 132.75 128.75 130.00 2058000 104.3774 -0.015151458
2009-07-29 130.00 131.50 127.00 128.25 2245700 102.9724 -0.013460768
2009-07-30 130.25 134.25 129.25 134.00 3305500 107.5890 0.044833373
2009-07-31 133.25 138.25 133.00 135.00 3155600 108.3920 0.007463588
2009-08-03 135.00 138.00 134.00 137.25 1916800 110.1985 0.016666359
2009-08-04 136.75 136.75 131.00 132.50 2139700 106.3847 -0.034608457
2009-08-05 132.00 136.00 132.00 134.50 2780800 107.9905 0.015094276
2009-08-06 135.50 139.25 135.00 136.00 2847500 109.1949 0.011152833
2009-08-07 135.25 141.00 134.50 140.00 3169600 112.4065 0.029411630
2009-08-10 139.75 139.75 137.75 139.00 1840700 111.6036 -0.007142825
2009-08-11 138.75 140.00 135.00 135.25 2143500 108.5927 -0.026978520
2009-08-12 134.75 138.50 134.50 138.50 2653600 111.2021 0.024029240
2009-08-13 139.00 149.75 138.75 143.75 8064400 115.4174 0.037906658
2009-08-14 144.25 148.50 141.50 142.75 40600 114.6145 -0.006956490
2009-08-17 141.25 142.00 137.25 137.50 2820500 110.3992 -0.036778069
2009-08-18 138.00 139.00 134.50 137.25 3389500 110.1985 -0.001817948
2009-08-19 136.00 139.75 134.25 138.50 3337300 111.2021 0.009107202
2009-08-20 141.00 148.25 141.00 147.50 5540400 118.4282 0.064981686
2009-08-21 147.50 154.00 147.25 153.50 5896100 123.2457 0.040678656
2009-08-24 155.50 157.00 150.00 154.00 3100300 123.6471 0.003256909
2009-08-25 151.00 151.00 148.75 149.75 3166000 120.2348 -0.027597089
2009-08-26 148.80 149.80 142.40 146.30 4975000 117.4648 -0.023038255
2009-08-27 144.70 146.30 142.70 145.30 2433000 116.6619 -0.006835239
2009-08-28 147.10 153.50 146.40 152.50 4590400 122.4428 0.049552596
2009-08-31 151.50 152.75 146.80 147.50 2984800 118.4282 -0.032787555
2009-09-01 148.60 148.90 145.10 145.30 3402800 116.6619 -0.014914522
2009-09-02 144.00 146.30 140.50 145.60 3403400 116.9027 0.002064084
2009-09-03 146.10 148.40 144.90 147.00 2835000 118.0268 0.009615689
2009-09-04 149.40 153.25 149.10 153.25 3937700 123.0449 0.042516615
x$Return is a vector, not a data.frame and therefore does not have rownames. Use x["Return"] or as.xts(d)[,"Return"] instead.

cor() giving a missing value error on small matrix but not large matrix

I'm trying to use cor() to return the most correlated elements in order of their correlation. I wrote this function adapting cor() to do it and it works perfectly, but only when I run it on a big input. When I try and run it on a small input, I get a missing value where TRUE/FALSE needed error and I don't understand why?
Here is an example of my input data:
This can be directly copied into R(printed via write.table):
"Col2" "Col3" "Col4" "Col5" "Col6"
"Market Capitalization" NA NA 17082.69 17879.8 16266.11
"Cash & Equivalents" NA NA 747 132 394
"Preferred & Other" NA NA 0 0 0
"Total Debt" NA NA 12379 11982 11309
"Enterprise Value" NA NA 28714.69 29729.8 27181.11
"Total Revenue" 2896.75 3461.25 2818 3184 2901
"Growth % YoY" -0.15 0.68 1.7 3.44 -0.48
"Gross Profit" NA NA 1874 2080 1981
"Margin %" NA NA 66.5 65.33 68.29
"EBITDA" 758 1074 641 777 699
"Margin %1" 26.17 31.03 22.75 24.4 24.1
"Net Income Before XO" 214.5 410 172 192 207
"Margin %2" 7.4 11.85 6.1 6.03 7.14
"Adjusted EPS" 0.7 1.42 0.59 1.07 0.69
"Growth % YoY1" 0.72 -1.67 -3.28 5.94 -6.76
"Cash from Operations" 375.79 812.21 991 -84 961
"Capital Expenditures" NA NA -660 -676 -608
"Free Cash Flow" NA NA 331 -760 353
"Adjusted Price" 2094.66 3689.2 3805.62 3588.42 3582.4
This is the mycor() function I wrote
mycor<-function(dataset, relative.to=19, neg.cor=0){
#This takes the dataset (as a matrix) and computes the best correleted value
#and returns the row (variable ID) that is the most strongly correlated
#to the variable row referenced by relative.to. Use neg.cor = 1 for neg correlation
if(neg.cor == 0){
best.cor <- -1.0 #Have to get better correlation then this
best.cor.row <- integer() #The row with the best correlation
all.cor <- numeric() #The correlation for everything else
index <- 1 #The index for the all.cor array
for(i in 1:nrow(dataset)){
if(i != relative.to){ #No self correlation
temp.cor <- cor(dataset[i,], dataset[relative.to,], use = "na.or.complete")
all.cor[index] <- temp.cor
index <- index+1 #I wish the ++ opperator worked in R...
cat(best.cor)
pause()
if(temp.cor > best.cor){ #This remembers the best seen cor value
best.cor <- temp.cor
best.cor.row <- i
} #End inner if
} #End outter if
} #End for loop
}else{
best.cor <- 1.0 #Have to get better correlation then this
best.cor.row <- integer() #The row with the best correlation
all.cor <- numeric() #The correlation for everything else
index <- 1 #The index for the all.cor array
for(i in 1:nrow(dataset)){
if(i != relative.to){ #No self correlation
temp.cor <- cor(dataset[i,], dataset[relative.to,], use = "na.or.complete")
all.cor[index] <- temp.cor
index <- index+1 #I wish the ++ opperator worked in R...
if(temp.cor < best.cor){ #This remembers the worst seen cor value
best.cor <- temp.cor
best.cor.row <- i
} #End inner if
} #End outter if
} #End for loop
} #End else
return(list(all.cor = all.cor, best.cor.row = best.cor.row))
)
When I try and run this I get: Error in if (temp.cor > best.cor) { : missing value where TRUE/FALSE needed. The part about this that is strange, is that the mycor function works perfectly and gives no error when I give it a larger chunk of the same data set.
This is the larger chunk of the same data set.
This can also be copied into R(printed via write.table):
"Col2" "Col3" "Col4" "Col5" "Col6" "Col7" "Col8" "Col9" "Col10" "Col11" "Col12" "Col13" "Col14" "Col15" "Col16" "Col17" "Col18" "Col19" "Col20" "Col21" "Col22" "Col23" "Col24" "Col25" "Col26" "Col27" "Col28" "Col29" "Col30" "Col31" "Col32" "Col33" "Col34" "Col35" "Col36" "Col37" "Col38" "Col39" "Col40" "Col41" "Col42" "Col43" "Col44" "Col45" "Col46" "Col47" "Col48" "Col49" "Col50" "Col51" "Col52" "Col53" "Col54" "Col55" "Col56" "Col57" "Col58" "Col59" "Col60" "Col61" "Col62" "Col63" "Col64" "Col65" "Col66" "Col67" "Col68" "Col69" "Col70" "Col71" "Col72" "Col73" "Col74" "Col75" "Col76" "Col77" "Col78" "Col79" "Col80" "Col81" "Col82" "Col83" "Col84" "Col85" "Col86" "Col87" "Col88" "Col89" "Col90" "Col91" "Col92" "Col93" "Col94" "Col95" "Col96" "Col97" "Col98" "Col99" "Col100" "Col101" "Col102" "Col103" "Col104" "Col105" "Col106" "Col107" "Col108" "Col109" "Col110" "Col111"
"Market Capitalization" NA NA 17082.69 17879.8 16266.11 17540.1 18214.39 17110.13 18167.87 16700.24 15592.71 14824.06 14455.42 13685.56 12168.31 12550.1 12771.45 11273.2 10284.48 10863.21 10655.99 11750.74 10671.37 10818.32 13288.42 12558.8 12221.79 13213.51 12375.92 11854.12 10942.65 10689.79 11364.1 11887.9 11426.1 10249.34 10609.99 10167.51 9600.1 10001.68 9713.38 9184.3 9730.33 8249.64 9160.61 8586.38 8894.55 8908.81 11887.9 11426.1 10249.34 10609.99 10167.51 9600.1 10001.68 9713.38 9184.3 9730.33 8249.64 9160.61 8586.38 8894.55 8908.81 8566.69 8641.04 8444.84 7867.83 8163.04 7238.2 6279.55 6173.33 7376.47 9048.75 10095.35 10351.52 12311.04 12006.02 10785.58 11009.16 9655.09 7990.1 6918.52 7050.24 6844.2 6520.75 6873.11 7489.61 7459.85 7136.58 6930.38 6401.43 6048.8 5843.01 6224.43 6840.76 7529.23 8452.46 8247.48 8132.72 7632.03 7339.11 6549.2 6165.26 6535.8 5793.52 5621.57 5877.31 5391.98 4792.51 5362.35
"Cash & Equivalents" NA NA 747 132 394 69 1381 769 648 398 492 516 338 198 178 87 260 75 311 651 74 68 1757 144 210 192 186 157 94 234 63 177 81 119 818 477 26 70 487 55 49 49 60 62 117.86 83.4 59.2 108.34 119 818 477 26 70 487 55 49 49 60 62 117.86 83.4 59.2 108.34 271.35 432.14 41.63 59.57 94.83 72.81 37.66 73.6 485.05 188.94 291.14 57.5 102.29 153.82 105.01 198.26 183.46 269.87 12.23 94.9 106.88 117.28 57.37 103.23 342.29 429.89 48.49 111.39 245.22 360.74 80.65 205.1 36.76 203.96 143.32 74.33 282.45 349.66 384.84 238.24 317.86 315.65 291.01 185.21 353.33 160.33 160.31
"Preferred & Other" NA NA 0 0 0 0 0 0 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 257 256 255 255 254 254 254 255 255 255 254 255 255 252 252 253 254 255 221 222 221 221.47 221.13 221.2 220.79 254 255 255 252 252 253 254 255 221 222 221 221.47 221.13 221.2 220.79 222.09 212.56 249.61 212.56 249.61 212.56 212.56 212.56 249.61 212.56 212.56 212.56 249.61 318.02 318.02 318.02 318.02 322.34 322.42 322.54 322.65 322.74 322.77 322.84 639.92 639.98 640.13 640.24 640.31 640.39 640.47 640.54 640.73 640.89 640.95 641.09 641.25 645.87 634.99 635.05 635.18 637.51 637.73 638.05 638.15 640.53 640.77
"Total Debt" NA NA 12379 11982 11309 11111 11873 11073 10675 10676 10678 11144 10683 11526 11020 11027 10599 10773 10366 10699 10094 9751 9480 9363 9282 9213 8653 8943 8815 8968 8487 8162 8205 7687 7868 7498 7219 7245 7336 7432 7094 6968 6682 7000 6841.23 6584.25 6374.14 6264.74 7687 7868 7498 7219 7245 7336 7432 7094 6968 6682 7000 6841.23 6584.25 6374.14 6264.74 6234.03 6249.6 6448.51 6100.6 6011.55 5693.56 5536.13 5276.01 5449.52 4792.08 4881.68 4471.08 4312.4 4410.61 4480.08 4437.33 4758.17 4432.04 4532.28 4466.59 4387.54 4313.86 4316.43 4316.66 4146.02 4175.36 4082.33 4085.09 4089.16 4116.98 3970.11 3972.46 3827.89 3850.12 3927.94 3722.68 3709.36 3804.58 3658.69 3885.52 3667.45 3734.29 3737 3615.16 3492.38 3374.62 3229.81
"Enterprise Value" NA NA 28714.69 29729.8 27181.11 28582.1 28706.39 27414.13 28407.87 27191.24 25991.71 25665.06 25013.42 25226.56 23223.31 23703.1 23323.45 22184.2 20552.48 21124.21 20888.99 21646.74 18607.37 20294.32 22616.42 21834.8 20943.79 22253.51 21350.92 20842.12 19621.65 18929.79 19743.1 19709.9 18731.1 17525.34 18054.99 17594.51 16702.1 17632.68 17013.38 16324.3 16574.33 15408.64 16105.45 15308.35 15430.68 15286 19709.9 18731.1 17525.34 18054.99 17594.51 16702.1 17632.68 17013.38 16324.3 16574.33 15408.64 16105.45 15308.35 15430.68 15286 14751.46 14671.06 15101.34 14121.44 14329.37 13071.51 11990.59 11588.31 12590.55 13864.46 14898.46 14977.66 16770.77 16580.82 15478.67 15566.25 14547.82 12474.62 11760.98 11744.46 11447.51 11040.07 11454.93 12025.88 11903.5 11522.02 11604.35 11015.38 10533.05 10239.65 10754.35 11248.66 11961.09 12739.51 12673.05 12422.15 11700.18 11439.9 10458.04 10447.58 10520.58 9849.67 9705.29 9945.31 9169.17 8647.34 9072.61
"Total Revenue" 2896.75 3461.25 2818 3184 2901 3438 2771 3078 2915 3629 2993 3349 3140 3707 3017 3462 3273 3489 2845 3423 2998 3858 3149 3577 3228 3579 2957 3357 2649 3441 2555 3317 3107 3337 2395 2800 2181 2734 2164 2685 2279 2801 2176 2570 2057.03 2539.49 1848 2056 3337 2395 2800 2181 2734 2164 2685 2279 2801 2176 2570 2057.03 2539.49 1848 2056 1942.6 2627.56 2112.22 2886.26 2250.13 2820.78 2041.89 2318.59 1963.38 2346.24 1479.08 1776.59 1617.34 2061.62 1561.04 1853.05 1720.06 2011.03 1504.01 1886.15 1632.3 1920.34 1539.73 1867.36 1528.38 1879.88 1459.85 1668.79 1461.25 1821.99 1392.09 1697.76 1483.61 1799.69 1396.01 1586.08 1478.81 1717.88 1280.11 1456.11 1342.73 1720.3 1330.65 1479.39 1367.21 1613.83 1263.27
"Growth % YoY" -0.15 0.68 1.7 3.44 -0.48 -5.26 -7.42 -8.09 -7.17 -2.1 -0.8 -3.26 -4.06 6.25 6.05 1.14 9.17 -9.56 -9.65 -4.31 -7.13 7.8 6.49 6.55 21.86 4.01 15.73 1.21 -14.74 3.12 6.68 18.46 42.46 22.06 10.67 4.28 -4.3 -2.39 -0.55 4.47 10.79 10.3 17.75 25 5.89 -3.35 -12.51 -28.77 22.06 10.67 4.28 -4.3 -2.39 -0.55 4.47 10.79 10.3 17.75 25 5.89 -3.35 -12.51 -28.77 -13.67 -6.85 3.44 24.48 14.6 20.23 38.05 30.51 21.4 13.81 -5.25 -4.13 -5.97 2.52 3.79 -1.75 5.38 4.72 -2.32 1.01 6.8 2.15 5.47 11.9 4.59 3.18 4.87 -1.71 -1.51 1.24 -0.28 7.04 0.32 4.76 9.05 8.93 10.13 -0.14 -3.8 -1.57 -1.79 6.6 5.33 -1.02 NA NA NA
"Gross Profit" NA NA 1874 2080 1981 2393 1934 1993 1846 2244 1794 2000 1942 2103 1723 1826 1700 1979 1558 1551 1459 1531 1420 1588 1478 1595 1317 1506 1273 1554 1202 1322 1179 1460 1097 1217 916 1285 980 1169 1066 1349 975 1157 1024.93 1317.57 980 1091 1460 1097 1217 916 1285 980 1169 1066 1349 975 1157 1024.93 1317.57 980 1091 1052.71 1368.8 1091.61 1236.41 991.8 1374.86 1043.29 1236.87 1129.87 1507.31 998.19 1190.69 1151.22 1475.08 1025.84 1170.8 1115.9 1438.56 981.96 1159.37 1094.25 1401.25 1001.2 1198.64 1079.65 1405.45 984.46 1196.22 1086.13 1415.37 998.06 1177.1 1086.53 1381.01 971.41 1118.91 1055.19 1331.37 947.22 1036.88 991.58 1301.1 921.48 994.97 967.89 1217.32 848.39
"Margin %" NA NA 66.5 65.33 68.29 69.6 69.79 64.75 63.33 61.84 59.94 59.72 61.85 56.73 57.11 52.74 51.94 56.72 54.76 45.31 48.67 39.68 45.09 44.39 45.79 44.57 44.54 44.86 48.06 45.16 47.05 39.86 37.95 43.75 45.8 43.46 42 47 45.29 43.54 46.77 48.16 44.81 45.02 49.83 51.88 53.03 53.06 43.75 45.8 43.46 42 47 45.29 43.54 46.77 48.16 44.81 45.02 49.83 51.88 53.03 53.06 54.19 52.09 51.68 42.84 44.08 48.74 51.09 53.35 57.55 64.24 67.49 67.02 71.18 71.55 65.72 63.18 64.88 71.53 65.29 61.47 67.04 72.97 65.02 64.19 70.64 74.76 67.44 71.68 74.33 77.68 71.7 69.33 73.24 76.74 69.58 70.55 71.35 77.5 74 71.21 73.85 75.63 69.25 67.26 70.79 75.43 67.16
"EBITDA" 758 1074 641 777 699 1091 711 794 684 978 617 844 708 916 640 696 625 885 569 611 567 586 520 702 596 715 510 694 547 670 467 564 423 717 411 533 274 624 367 497 458 669 334 485 388.44 693.3 384 487 717 411 533 274 624 367 497 458 669 334 485 388.44 693.3 384 487 445 695.27 439.32 538.75 377.16 666.39 492.65 526.86 446.87 748.34 331.51 492.91 430.87 760.5 313.33 474.78 434.79 751.92 280.96 463.41 390.79 712.97 313.14 490.27 368.26 711.24 307.36 506.85 383.64 721.41 317.3 474.34 363.04 678.27 279.09 400.41 320.03 637.82 281.47 340.21 297.39 610.07 247.48 300.27 305.15 561.67 203.06
"Margin %1" 26.17 31.03 22.75 24.4 24.1 31.73 25.66 25.8 23.46 26.95 20.61 25.2 22.55 24.71 21.21 20.1 19.1 25.37 20 17.85 18.91 15.19 16.51 19.63 18.46 19.98 17.25 20.67 20.65 19.47 18.28 17 13.61 21.49 17.16 19.04 12.56 22.82 16.96 18.51 20.1 23.88 15.35 18.87 18.88 27.3 20.78 23.69 21.49 17.16 19.04 12.56 22.82 16.96 18.51 20.1 23.88 15.35 18.87 18.88 27.3 20.78 23.69 22.91 26.46 20.8 18.67 16.76 23.62 24.13 22.72 22.76 31.9 22.41 27.74 26.64 36.89 20.07 25.62 25.28 37.39 18.68 24.57 23.94 37.13 20.34 26.25 24.09 37.83 21.05 30.37 26.25 39.59 22.79 27.94 24.47 37.69 19.99 25.25 21.64 37.13 21.99 23.36 22.15 35.46 18.6 20.3 22.32 34.8 16.07
"Net Income Before XO" 214.5 410 172 192 207 440 214 280 193 386 168 314 236 353 186 229 205 339 153 183 163 185 283 303 209 313 154 261 205 234 129 183 148 290 121 184 55 253 92 158 50 260 69 157 123.03 286.54 101 169 290 121 184 55 253 92 158 50 260 69 157 123.03 286.54 101 169 128.51 280.74 104.07 182.51 49.48 283.27 72.14 191.53 124.96 339.41 69.8 180.05 135.23 351.55 66.51 176.45 143.61 355.04 47.56 166.61 120.15 327.99 71.42 188.48 113.12 333.3 76.4 201.03 117.88 339.87 87.21 189.31 117.29 324.84 62.45 153.94 100.63 309.44 77.54 116.48 92.2 303.36 64.65 106.7 121.1 263.26 49.06
"Margin %2" 7.4 11.85 6.1 6.03 7.14 12.8 7.72 9.1 6.62 10.64 5.61 9.38 7.52 9.52 6.17 6.61 6.26 9.72 5.38 5.35 5.44 4.8 8.99 8.47 6.47 8.75 5.21 7.77 7.74 6.8 5.05 5.52 4.76 8.69 5.05 6.57 2.52 9.25 4.25 5.88 2.19 9.28 3.17 6.11 5.98 11.28 5.47 8.22 8.69 5.05 6.57 2.52 9.25 4.25 5.88 2.19 9.28 3.17 6.11 5.98 11.28 5.47 8.22 6.62 10.68 4.93 6.32 2.2 10.04 3.53 8.26 6.36 14.47 4.72 10.13 8.36 17.05 4.26 9.52 8.35 17.65 3.16 8.83 7.36 17.08 4.64 10.09 7.4 17.73 5.23 12.05 8.07 18.65 6.26 11.15 7.91 18.05 4.47 9.71 6.8 18.01 6.06 8 6.87 17.63 4.86 7.21 8.86 16.31 3.88
"Adjusted EPS" 0.7 1.42 0.59 1.07 0.69 1.44 0.61 1.01 0.74 1.33 0.57 0.99 0.69 1.32 0.51 0.93 0.67 1.16 0.48 0.78 0.72 0.98 0.42 0.87 0.71 1.2 0.58 1.03 0.78 0.92 0.51 0.86 0.59 1.17 0.48 0.75 0.49 1.08 0.38 0.69 0.65 1.16 0.29 0.72 0.56 1.33 0.46 0.78 1.17 0.48 0.75 0.49 1.08 0.38 0.69 0.65 1.16 0.29 0.72 0.56 1.33 0.46 0.78 0.59 1.3 0.48 0.84 0.52 1.4 0.33 0.88 0.57 1.5 0.3 0.76 0.56 1.49 0.26 0.73 0.59 1.49 0.18 0.69 0.49 1.38 0.28 0.78 0.44 1.38 0.29 0.82 0.47 1.41 0.33 0.77 0.46 1.35 0.23 0.62 0.39 1.3 0.3 0.47 0.36 1.29 0.24 0.43 0.49 1.11 0.18
"Growth % YoY1" 0.72 -1.67 -3.28 5.94 -6.76 8.27 7.02 2.02 7.25 0.76 11.76 6.45 2.99 13.79 6.25 19.23 -6.94 18.37 14.29 -10.34 1.41 -18.33 -27.59 -15.53 -8.97 30.43 13.73 19.77 32.2 -21.37 6.25 14.67 20.41 8.33 26.32 8.7 -24.62 -6.9 31.03 -4.17 16.07 -12.78 -36.96 -7.69 -5.08 2.31 -4.17 -7.14 8.33 26.32 8.7 -24.62 -6.9 31.03 -4.17 16.07 -12.78 -36.96 -7.69 -5.08 2.31 -4.17 -7.14 13.46 -7.14 45.45 -4.55 -8.77 -6.67 10 15.79 1.79 0.67 13.64 4.11 -5.08 -0.07 44.44 5.89 20.41 8.05 -34.72 -11.62 11.36 0 -3.45 -4.88 -6.38 -2.13 -12.12 6.49 2.17 4.44 43.48 24.19 17.95 3.85 -23.33 31.91 8.33 0.78 25 9.3 -26.53 16.22 33.33 -23.21 NA NA NA
"Cash from Operations" 375.79 812.21 991 -84 961 391 845 402 976 572 1227 362 1407 179 794 1 997 26 798 645 581 -1237 733 563 630 109 346 481 710 -162 224 593 177 581 -346 389 525 164 490 152 766 218 492 -58 735.49 285 369 146 581 -346 389 525 164 490 152 766 218 492 -58 735.49 285 369 146 490.18 387.73 254.59 141.41 215.82 279.84 489.5 199.17 -325.31 -66.66 280.22 256.65 718.82 438.66 302.05 244.37 -52.38 647.78 53.19 258.9 294.29 359.1 267.8 184.51 310.07 585.52 233.75 145.31 426.63 480.57 187.86 270.34 236.08 472.92 243.13 69.8 261.19 291.41 285.57 77.33 283.64 328.4 309.68 11.95 357.21 141.59 357.15
"Capital Expenditures" NA NA -660 -676 -608 -478 -635 -523 -542 -503 -629 -460 -599 -548 -551 -465 -719 -531 -595 -529 -785 -584 -608 -547 -638 -519 -485 -482 -583 -480 -537 -420 -619 -385 -426 -390 -431 -439 -308 -373 -448 -356 -404 -317 -593.69 -310 -392 -340 -385 -426 -390 -431 -439 -308 -373 -448 -356 -404 -317 -593.69 -310 -392 -340 -302.22 -394.08 -274.8 -228.02 -75.57 -274.36 -684.94 -207.41 -211.95 -218.98 -157.07 -127.56 -210.59 -156.81 -150.58 -127.3 -226.32 -145.55 -171.37 -140.37 -244.12 -167.92 -185.35 -142.94 -239.55 -165.98 -166.25 -147.38 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
"Free Cash Flow" NA NA 331 -760 353 -87 210 -121 434 69 598 -98 808 -369 243 -464 278 -505 203 116 -204 -1821 125 16 -8 -410 -139 -1 127 -642 -313 173 -442 196 -772 -1 94 -275 182 -221 318 -138 88 -375 141.79 -25 -23 -194 196 -772 -1 94 -275 182 -221 318 -138 88 -375 141.79 -25 -23 -194 187.96 -6.35 -20.21 -86.61 140.26 5.47 -195.45 -8.24 -537.26 -285.64 123.15 129.09 508.23 281.85 151.46 117.07 -278.7 502.23 -118.18 118.53 50.17 191.18 82.45 41.57 70.51 419.54 67.49 -2.08 426.63 480.57 187.86 270.34 236.08 472.92 243.13 69.8 261.19 291.41 285.57 77.33 283.64 328.4 309.68 11.95 357.21 141.59 357.15
"Adjusted Price" 2094.66 3689.2 3805.62 3588.42 3582.4 3885.75 3523.13 3554.9 3420.27 3141.36 2984.19 2838.81 2760.09 2517.44 2447.56 2403.89 2188.98 1960.8 1952.2 2033.87 2099.97 1993.98 2043.36 2296.42 2201.73 2277.15 2301.5 2203.47 2086.87 1938.95 2019.34 2002.47 2048.12 1881.97 1817.17 1807.02 1664.57 1659.78 1717.25 1585.27 1589.9 1506.13 1534.98 1531.24 1498.21 1528.96 1418.46 1431.1 1343.43 1244.04 1194.62 1076.93 1058.66 960.76 1112.69 1322.69 1414.59 1442.28 1545.6 1364.27 1305.46 1231.15 1022.23 869.37 796.9 820.22 762.84 715.9 756.11 816.37 731.97 705.73 657.84 628.55 571.47 624.67 651.89 676.63 759.77 742.27 734.39 657.44 619.61 569.84 524.2 510.26 475.43 449.8 441.27 409.34 383 413.34 441.72 435.71 419.07 385.87 356.85 346.15 326.97 318.45 323.72 314.18 313.22 300.88 329.3 315.1 312.34 279.11 163.47 NA
The larger chunk works perfectly, but I need to be able to check the correlation on the smaller sections. I'm really new to R so it might be easy, but I've read the boards here and the r manuals and can't find it.
In your example above, your code fails on the first (smaller) data set because row 3 consists only of 0's and NA's, so it has a standard deviation of 0 and so its correlation with any other row will return NA, since computing correlation involves dividing the sample covariance by the sample standard deviation of each vector. It doesn't happen in the larget example because row 3 has sufficient variation to have a non-zero standard deviation.
However, your approach seems a bit convoluted. If you want to compute the correlation between a single row in the matrix and all other rows, sorted by correlation, then you can use cor() on the transposed matrix and sort the result, for example:
mycor <- function(dataset, relative.to=19) {
mat <- t(dataset)
cors <- cor(mat, mat[, relative.to], use="na.or.complete")
cors[order(drop(cors)), ]
}
mycor(dataset)

Quantmod, empty dates in getSymbols from Google

Quantmod version 0.4.0
Function getSymbols returns empty dates when using Google as source, not using Yahoo.
Google data seems fine, checking http://www.google.com/finance/historical?cid=700196&startdate=Sep+30%2C+2010&enddate=Nov+1%2C+2010&num=30&ei=6sqlUoieA5SLsgfq5gE&output=csv
Reproducable with followong code:
library(quantmod)
Sys.setenv(TZ="UTC")
DataG <- getSymbols('XLF',src="google",auto.assign=FALSE, from = '2010-09-30', to = '2010-11-01')
DataG
DataY <- getSymbols('XLF',src="yahoo",auto.assign=FALSE, from = '2010-09-30', to = '2010-11-01')
DataY
Retested in version 0.4.1, still NA's in date!
DataG <- getSymbols('XLF',src="google",auto.assign=FALSE, from = '2010-09-30', to = '2010-11-01')
DataG
XLF.Open XLF.High XLF.Low XLF.Close XLF.Volume
2010-09-30 14.46 14.63 14.34 14.34 107539828
<NA> 14.49 14.55 14.34 14.50 132131830
<NA> 14.48 14.59 14.33 14.40 85547602
<NA> 14.53 14.78 14.42 14.73 133006599
<NA> 14.76 14.80 14.67 14.72 64754368
<NA> 14.79 14.84 14.58 14.66 71795649
<NA> 14.68 14.73 14.62 14.70 62422677
<NA> 14.71 14.73 14.62 14.68 41265794
<NA> 14.60 14.87 14.57 14.84 65831033
<NA> 14.94 15.00 14.82 14.86 112666954
<NA> 14.72 14.75 14.46 14.60 169232668
<NA> 14.64 14.72 14.25 14.34 132860239
<NA> 14.30 14.69 14.30 14.67 78307701
<NA> 14.56 14.77 14.41 14.47 146739470
<NA> 14.48 14.69 14.35 14.61 96600861
<NA> 14.67 14.78 14.49 14.61 73596983
<NA> 14.64 14.69 14.56 14.60 41264255
<NA> 14.73 14.75 14.53 14.55 45766940
<NA> 14.49 14.60 14.46 14.57 47408863
<NA> 14.52 14.63 14.47 14.58 62701109
<NA> 14.66 14.70 14.49 14.58 57911184
<NA> 14.54 14.59 14.48 14.56 39827062
2010-11-01 14.59 14.69 14.42 14.56 65746592
DataY <- getSymbols('XLF',src="yahoo",auto.assign=FALSE, from = '2010-09-30', to = '2010-11-01')
DataY
XLF.Open XLF.High XLF.Low XLF.Close XLF.Volume XLF.Adjusted
2010-09-30 14.46 14.63 14.34 14.35 107532900 13.65
2010-10-01 14.49 14.55 14.34 14.50 132129000 13.80
2010-10-04 14.48 14.59 14.33 14.40 85547600 13.70
2010-10-05 14.53 14.79 14.42 14.73 133006600 14.01
2010-10-06 14.76 14.80 14.67 14.72 64754400 14.01
2010-10-07 14.79 14.84 14.58 14.66 71794600 13.95
2010-10-08 14.68 14.73 14.62 14.70 62412700 13.99
2010-10-11 14.71 14.73 14.62 14.68 41265800 13.97
2010-10-12 14.60 14.87 14.57 14.85 65831100 14.13
2010-10-13 14.94 15.00 14.82 14.86 112667000 14.14
2010-10-14 14.72 14.75 14.46 14.60 169232700 13.89
2010-10-15 14.65 14.72 14.25 14.35 132854700 13.65
2010-10-18 14.30 14.69 14.30 14.67 78305300 13.96
2010-10-19 14.56 14.77 14.41 14.47 146739500 13.77
2010-10-20 14.48 14.69 14.35 14.61 96600900 13.90
2010-10-21 14.67 14.78 14.49 14.61 73588900 13.90
2010-10-22 14.64 14.69 14.56 14.60 41264300 13.89
2010-10-25 14.73 14.75 14.53 14.55 45766800 13.84
2010-10-26 14.49 14.60 14.46 14.57 47400100 13.86
2010-10-27 14.52 14.63 14.47 14.58 62701200 13.87
2010-10-28 14.66 14.70 14.49 14.58 57907600 13.87
2010-10-29 14.54 14.59 14.48 14.56 39826600 13.85
2010-11-01 14.59 14.69 14.42 14.56 65743100 13.85
installed.packages()["quantmod", "Version"]
[1] "0.4-1"
There is a problem with localization and date format. This works for me.
invisible(Sys.setlocale("LC_MESSAGES", "C"))
invisible(Sys.setlocale("LC_TIME", "C"))
Now try getSymbols with google as data source.

Error while producing an ARMA model using the TSA package in R

Had anyone else had this problem, or even better, does anyone know why this is giving me an error?
I'm attempting to create an ARMA model of order 3, 3. I'm using the TSA package.
stocks_arma <- arma(stocks$close, order = c(3,3))
I'm getting this warning:
Warning message:
In arma(VIXts, order = c(3, 3)) : Hessian negative-semidefinite
I understand that a Hessian negative-semidefinite matrix is a bad thing because we usually want global mins/maxes. However, I don't understand why this is happening. I am unsure is this is a mathematical issue or a syntactial issue.
My data is a very modest vector of 1000 entries. Here is one-tenth of it:
15.14 15.31 15.08 15.24 16.41 17.99 17.92 16.65 16.68 18.61 18.49 19.08 17.58 18.42 17.59 16.69 18.60 17.81 18.12 18.33 18.83 16.62 16.97 15.03 15.07 15.22 15.27 16.14 15.59 16.29 16.37 15.11 14.33 14.55 15.43 15.71 16.32 15.73 14.84 16.81 15.43 14.15 13.98 14.07 13.88 14.18 14.59 14.51 14.05 15.80 16.41 16.28 14.38 15.63 17.74 17.98 17.47 17.83 17.06 16.49 16.35 15.18 15.96 15.11 15.02 14.02 13.45 14.29 14.63 14.85 13.70 14.74 15.28 15.32 15.99 15.95 15.64 17.57 18.96 18.93 18.03 16.70 17.53 19.34 20.47 18.62 16.27 15.45 16.16 16.48 17.11 16.74 18.36 17.95 18.72 18.05 17.10 17.50 16.66 16.80 17.08 19.71 19.45 19.72 20.38
There is nothing overtly fishy about the values at all.
Any insight is very much appreciated.

Resources