Reputation: 1480
I am posting this question which refers to a statistical problem however the issue I have is with the code.
I have the below vector and autocorrelation computation of significant values for a confidence intervals of 95%. (There is only one significant value at t+1) Package Quantmod.
x<-c(1,1,3,8,5,2,4,3,1,1,0,5,1,1,3,4,6,7,8,9,4,2,1)
a<-acf(x)
b<-a[[1]]
c<-(b[2:length(b)])
posssignificance_level<-qnorm((1+0.95)/2)/sqrt(sum(!is.na(x)))
posssignificance_level
negsignificance_level<- -posssignificance_level
negsignificance_level
poscorr<-which(posssignificance_level<c)
negcorr<-which(negsignificance_level>c)
poscorr
negcorr
I would like to instruct to lag if there is any significant value above/below 95% confident interval either in poscorr or negcorr or both. I atempted to use the below expresions without sucess. (I use length(poscorr==0 and length(negcorr==0) as the lengh of the resulting vector is 0 when there is no autocorrelation. The result for negcorr is "integer(0)").
posautorrelation <- if(length(poscorr==0)) Lag(x,0) else Lag(x,poscorr)
negautorrelation <- if(length(negcorr==0)) Lag(x,0) else Lag(x,negcorr)
Error en `colnames<-`(`*tmp*`, value = "Lag.") :
la longitud de 'dimnames' [2] no es igual a la extensión del arreglo
Error durante el wrapup: no se puede abrir la conexión
I also try
posautorrelation <- if((poscorr==integer(0)) Lag(x,0) else Lag(x,poscorr)
Error: inesperado símbolo in "posautorrelation <- if(length(poscorr==integer(0)) Lag"
Error durante el wrapup: no se puede abrir la conexión
negautorrelation <- if((negcorr==integer(0)) Lag(x,0) else Lag(x,negcorr)
Error: inesperado símbolo in "negautorrelation <- if(length(negcorr==integer(0)) Lag"
Error durante el wrapup: no se puede abrir la conexión
I would like to know how could I instruct the last two expressions as to obtain two versions of x. One with a lag and the one without any autocorrelation value with a 0 lag using the result of negcorr integer(0).
Upvotes: 2
Views: 245
Reputation: 7714
The first part explains why the which(...)
expression returns integer(0)
print(negsignificance_level)
# [1] -0.4086807
min(c)
# [1] -0.3432622
which(negsignificance_level > c)
# integer(0)
This is i believe the answer to your question:
if length(object) == then expression1 else expression2
posautorrelation <- if(length(poscorr) == 0) Lag(x,0) else Lag(x, poscorr)
posautorrelation
# [1] NA 1 1 3 8 5 2 4 3 1 1 0 5 1 1 3 4 6 7 8 9 4 2
negautorrelation <- if(length(negcorr) == 0) Lag(x,0) else Lag(x, negcorr)
# [1] 1 1 3 8 5 2 4 3 1 1 0 5 1 1 3 4 6 7 8 9 4 2 1
Upvotes: 2