-1

I have time series data that appears like below:

data

          Jan      Feb      Mar      Apr      May      Jun      Jul      Aug      Sep      
1960 -10.3000  -7.1000   2.9000  18.0000  18.9000  29.5000  31.4000  28.0000  20.7000  
1961  -1.8000  -4.2000   4.0000  13.2000  22.0000  23.4000  24.0000  23.4000  17.3000  
1962 -12.1000  -3.1000  -0.1000   9.5000  22.9000  24.4000  26.4000  30.0000  21.2000  
1963  -2.9000  -5.9000   5.4000   9.3000  21.0000  25.3000  27.9000  24.6000  21.4000  
1964  -2.8000  -7.4000   2.2000   9.1000  17.6000  25.3000  25.1000  25.3000  18.0000  
1965  -9.5000   1.0000   6.3000  14.3000  18.3000  24.7000  28.4000  26.7000  21.9000  
1966  -5.4000  -2.7000   6.6000  14.3000  17.7000  24.2000  26.2000  25.3000  20.5000   
1967  -8.1000  -0.6000   3.0000  13.8000  23.2000  23.8000  27.6000  24.3000  20.4000     

I want to plot the data on monthly scale, for example, I want to plot data of January month from 1960-1967. I tried using zoo function but it does not work. The problem is that even after installing zoo package, the zoo function seems to be unrecognized by the system. The error message looks like this:

> install.packages("zoo")
Installing package(s) into ‘C:/Users/skhanal2.RUSSELL/Documents/R/win-library/2.15’
(as ‘lib’ is unspecified)
trying URL 'http://streaming.stat.iastate.edu/CRAN/bin/windows/contrib/2.15/zoo_1.7-9.zip'
Content type 'application/zip' length 868277 bytes (847 Kb)
opened URL
downloaded 847 Kb
package ‘zoo’ successfully unpacked and MD5 sums checked
Warning: cannot remove prior installation of package ‘zoo’

The downloaded binary packages are in
        C:\Users\skhanal2.RUSSELL\AppData\Local\Temp\RtmpCSHWVD\downloaded_packages
> test<-zoo(data,by="month")
**Error: could not find function "zoo"**

Has anyone tried to plot time series data by month?If so, I would appreciate if someone could help me find the solution. Also, can anyone suggest what is the problem in zoo function? I am using R version 2.15.2 and I am using Windows 7 in 32 bit machine. Thanks!

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418

1 Answers1

1

It isn't enough to install the package. You also have to load it into memory using the library() command, e.g. library(zoo). Once you get the zoo package loaded correctly...

 test<-zoo(data,order.by=sort(data$year))
 plot(test)
 #or
 plot(test[,c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep")])
russellpierce
  • 4,583
  • 2
  • 32
  • 44
  • Warning: cannot remove prior installation of package ‘zoo’ - doesn't look promising. You might shutdown R and try reinstalling. Failing that if you go to the library folder off of the root of where R is installed you might manually delete the zoo folder, restart R and try installing again. – russellpierce Feb 18 '13 at 17:22
  • However, it seems pretty clear that your problem at this point isn't statistical, it is technical. (Although your call to zoo also looks flawed). – russellpierce Feb 18 '13 at 17:23
  • Thanks. I did work after restarting the R software. Do you have any idea about filtering data by month in the time-series? – Sami khanal Feb 18 '13 at 17:28
  • I edited my answer, the code above should "plot the data on monthly scale" as you requested. – russellpierce Feb 18 '13 at 18:10
  • Sorry if I am being very naive but I could not run the code you suggested: test<-zoo(data,order.by=sort(data$year)) As the data is in time series format, there is no column such as year. My data appears as: structure(c(-10.3, -7.1, 2.9, 18, 18.9, 29.5, 31.4, 28, 20.7, 17.9, 5.9, -2.4, -1.8, -4.2, 4, 13.2, 22, 23.4, 24, 23.4, 17.3, 13, 3.4, -7), .Tsp = c(1960, 1961.91666666667, 12), class = "ts") Thanks for your input! – Sami khanal Feb 18 '13 at 20:14
  • I guess I created year from the rownames in your example dataset. I assume you are showing me the structure of what is in test. Go back to data... and before you run `test<-zoo(data,order.by=sort(data$year))` run `data$year <- rownames(data)` and see if that helps. – russellpierce Feb 18 '13 at 21:43
  • @Sami, Seems you have a `ts` series. Try `monthplot(tser)` where `tser` is your series. – G. Grothendieck Feb 19 '13 at 02:32
  • @G.Grothendieck: I did not quite follow the manual on monthplot in terms of what is being plotted. Are those trend lines across years for each of the months? – russellpierce Feb 19 '13 at 03:40
  • @Sami, Try `tser2 <- ts(1:120, freq = 12); moonthplot(tser2)` to clarify it for yourself. – G. Grothendieck Feb 19 '13 at 06:31
  • Thank you all for your responses. I got the solution to my problem. I should mainly subset the data for the month I want to plot the data. The code looks like this: Aug<-subset(data, cycle(data)==8); plot(Aug) – Sami khanal Feb 19 '13 at 16:30
  • @Samikhanal: Sure that will work, but then you aren't really using the information from the time series and you've lost the relationship between your data and the year it was collected. – russellpierce Feb 19 '13 at 17:39