# [ncl-talk] regression with lag

Kevin Hallock hallock at ucar.edu
Tue Oct 3 16:15:15 MDT 2017

```Hi,

I think there might be an issue with the P1 variable passed as an argument to regline(). The documentation for the regline() function <https://www.ncl.ucar.edu/Document/Functions/Built-in/regline.shtml> indicates that both arguments should be 1-dimensional arrays of the same length, although in your code P1 is being passed as a 3-dimensional array. You may want to try something like this:

dims_P1 = dimsizes(P1)
do a=0,dims_P1(0)-1
do b=0,dims_P1(1)-1
do n=0,nlagr-1
Regdata(a,b,n) = regline(x(0:Nend-n-yLag), P1(a,b,yLag+n:Nend))
end do
end do
end do

Kevin

> On Oct 3, 2017, at 2:58 AM, Sancta Vega <sanctavega at gmail.com> wrote:
>
> Hi Kevin thank you for your suggestion.
> I don't want value of n to increase by 2 every loop iteration but normally from 0 to 20 in order to get 21 as "nlagr".
>
> I removed the "n=-1" and "n=n+1" but still getting same error as before.
> do n=0,nlagr-1
>       Regdata(:,:,n) =regline(x(0:Nend-n-yLag), P1(:,:,yLag+n:Nend))
> end do ;
>
> 2017-10-03 1:44 GMT+02:00 Kevin Hallock <hallock at ucar.edu <mailto:hallock at ucar.edu>>:
> Hi,
>
> I noticed in your code’s do-loop that you’re incrementing n independently of the loop.
>
> If you modify the loop to include “print(n)”:
> do n=0,nlagr-1
>       n = n+1
>       print(n)
>       Regdata(:,:,n) =regline(x(0:Nend-n-yLag), P1(:,:,yLag+n:Nend))
> end do ;
>
> you can see that the only values of “n” used in the “Regdata(:,:,n) =…” line are (/1,3,5,7,9,11,13,15,17,19,21/), because n is being incremented by both the loop structure itself and the “n = n+1” line.
>
> If this is intentional and you want your value of n to increase by 2 every loop iteration, you could cleanly write this by adding a third comma-separated value on the “do n=“ line, for example:
> do n=0,nlagr-1,2
>       print(n)
> end do
>
> Otherwise, you might want to try removing the “n = n+1” line and rerunning your script.
>
> Regarding the subscript error you experienced, I believe that the final 21 value mentioned above is causing the error you’re seeing, as the third dimension of Regdata is “nlagr” elements long — 21 in this case, with valid indices ranging from 0 to 20, inclusive.
>
> I hope this helps,
> Kevin
>
>> On Oct 2, 2017, at 8:25 AM, Sancta Vega <sanctavega at gmail.com <mailto:sanctavega at gmail.com>> wrote:
>>
>> according to this answer  https://www.ncl.ucar.edu/Support/talk_archives/2013/2968.html <https://www.ncl.ucar.edu/Support/talk_archives/2013/2968.html>
>>
>> I need the 10 positive and negative lag regression  of my data x(time), P1(lat,lon,time) but I dont loop trough lat lon .
>>
>> This is my code :
>>
>> dim_s   = dimsizes(P1)  ;;;
>>  Nend = 5000 ; length of  x
>>  nlagr= 21 ; +- 10 lag
>>   yLag = 10
>>
>>  Regdata              = new((/dim_s(0),dim_s(1),nlagr/),"float")
>>
>> n=-1
>> do n=0,nlagr-1
>>       n= n+1
>>        Regdata(:,:,n) =regline(x(0:Nend-n-yLag), P1(:,:,yLag+n:Nend))
>> end do ;
>>
>>
>> This give me an error :
>> fatal:Subscript out of range, error in subscript #2
>> fatal:An error occurred reading P1
>>
>> I try to replace regline by regCoef and get the same error message.
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk <http://mailman.ucar.edu/mailman/listinfo/ncl-talk>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20171003/d94d2994/attachment.html>
```