Varying length of reqression output

The function below works well when the segment length times the number of segments is equal to the length of the time series Cumulative. For the illustration,
a small length of only 12 observations is assumed.

Function LocalForward(Cumulative, Length)
    Wave Cumulative
    Variable Length

    Make/O/N=3 Scale={12,6,4}
    Make/O/N=3 NumbSegments={1,2,3}
   
    Make/O/N=(Length,3) MatrixFWSquaredResids  
   
    Variable i ,j  
   
    for (i=0; i<3; i +=1)  
   
        Variable segmlength = Scale[i] 
        Variable nsegments = NumbSegments[i]   
                                               
   
        Make/O/N=(Length) Xwave = mod(p, segmlength) +1
                                                               
        Make/O/N=(Length) FWResidsSquared
   
            for (j=0; j<nsegments; j += 1) 
           
                CurveFit/Q Line Cumulative[j*segmlength, (j+1)*segmlength-1 ] /X=Xwave /D /R /A=0
                Wave Res_Cumulative
                FWResidsSquared[j*segmlength, (j+1)* segmlength-1] = Res_Cumulative[p]^2
               
            endfor 
           
            MatrixFWSquaredResids[][i] = FWResidsSquared[p]
    endfor
   
End


I run into problems when segment length times the number of segments is not equal to the length of the time series, as does happen when the segment length is 10
and the number of segments is 1 or the segment length is 3 and the number of segments is 3. See the changed code and the attached output (= MatrixSquaredResids).

Function LocalForward(Cumulative, Length)
    Wave Cumulative
    Variable Length
   
    Make/O/N=5 Scale={12,10,6,4,3}
    Make/O/N=5 NumbSegments={1,1,2,3,3}
   
    Make/O/N=(Length,5) MatrixFWSquaredResids  
   
    Variable i ,j  
   
    for (i=0; i<5; i +=1)  
   
        Variable segmlength = Scale[i] 
        Variable nsegments = NumbSegments[i]   
                                               
        Make/O/N=(Length) Xwave = mod(p, segmlength) +1
                                                               
        Make/O/N=(Length) FWResidsSquared
   
            for (j=0; j<nsegments; j += 1) 
           
                CurveFit/Q Line Cumulative[j*segmlength, (j+1)*segmlength-1 ] /X=Xwave /D /R /A=0
                Wave Res_Cumulative
                FWResidsSquared[j*segmlength, (j+1)* segmlength-1] = Res_Cumulative[p]^2
               
            endfor 
           
            MatrixFWSquaredResids[][i] = FWResidsSquared[p]
    endfor
   
End


The matrix in the attachment should show zero values in rows 11 and 12 of the second column as well as in rows 10, 11, and 12 of the fifth column. Instead of that
the columns repeat the values in the same rows of column 1 and 4, respectively. Problems like the one shown above turn up when the the segment length is not a
divisor of the length of the time series.

Does anyone know how I may show the squared residuals correctly also in situations where the segment length fails to be a divisor of the length of the time series?

Any help is highly appreciated.

Eduard



Eduard,

I'm not completely certain this is the source of your problem, but my hunch is that it is. You should change

Make/O/N=(Length) FWResidsSquared

to

Make/O/N=(Length) FWResidsSquared = 0

Overwriting a wave with the /O flag leaves the contents unchanged, unless you change the wave's dimensions. Even then, some elements will retain a non-zero value.

Jeff