0

I am trying to correlate a linear and circular variable using the circular package in R: http://cran.r-project.org/web/packages/circular/circular.pdf

#Linear variable
x<-c(221, 223, 256, 212, 217, 217, 200)

#Circular variable, compass direction in degrees
y<-c(2, 213, 356, 188, 202, 221, 191)

#Convert y to a circular variable, to specify type 
y<-circular(y, type=c("angles"),units=c("degrees"),
   template=c("geographics"), zero=0, rotation=c("clock"))

#Correlation
lm.circular(y=y, x=x, init=c(7), type="c-l", verbose=TRUE)

Running this produces the error: Error in while (diff > tol) { : missing value where TRUE/FALSE needed

I am not sure what this means. There are no missing values. I thought it might be due to the fact that I don't understand "init."

Here is the description of the function "init" in the documentation:"a vector with initial values of length equal to the columns of x." I have tried "1" and "7" here, with the same results. The documentation mentions that the appropriate inputs for x and y are vectors, so I am not sure what "columns" means.

Or maybe there is something else I am missing? Thanks!

  • Well, for one thing, `x` in your case is a vector and has no columns, so I don't think `init` will work - leave it as `NULL` and see what happens. BTW I believe there's a typo (or many) - the object returned when calling with `c-l` should be of class `lm.circular.cl` . – Carl Witthoft Sep 03 '14 at 17:16
  • I should have mentioned that I also tried "init=NULL" and tried deleting it altogether, both of which produce the error: "Error in lm.circular.cl(...) : 'init' is missing with no default." –  Sep 03 '14 at 17:20

2 Answers2

0

While the documentation is weak, a little work with debug reveals that a vector input is converted to a single-column matrix, so you should enter a single initial value for init.

However, I ran your dataset (and code) but the algorithm diverges. If you plot y vs x in linear space, it would appear that the ordered pair (221,2) is wildly out of bed with the remainder of the values.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • By "single initial value" do you mean "c(1)"? And what do you mean by "the algorithm diverges"? 2 is not that far away from some of the other values on the compass given as an example here (i.e., 356); direction on a compass is a circular variable which is why a linear correlation does not work. Is there something about circular data that make this algorithm unable to handle outliers (an outlier is not a problem in a linear correlation, it is just a weaker relationship). These data were an example, I get the same results with my actual data, which include a full range of directions. –  Sep 03 '14 at 20:14
  • Rachel, from your questions it's clear you are new to `R`. I think you need to work through `R-intro` and one of those online interactive "R tutorials" before going any further. I also suggest that if you don't understand what "diverges" means, you are not at a mathematical level to understand fitting or regression functions. – Carl Witthoft Sep 03 '14 at 21:31
  • My mathematical knowledge and experience in R aside, were you able to get this function to work? How? –  Sep 03 '14 at 21:55
0

Your example does work with init=0. I agree that the error message is not very informative. You may want to ask the package developers if it can be improved.

pixel
  • 103
  • 3