Viability of Willamette River Winter Steelhead: An assessment of the effects of sea lions at Willamette Falls

This website is a companion to an eponymous report . The code and data provided here are intended to facilitate technical review and generate reproducible results.

Contents

North Santiam

[num1,txt1,raw1]=xlsread('North Santiam Basin STW summary.xlsx','1985-2017','A2:D143');
T=cell2table(raw1,'VariableNames',{'Creek','Date','Miles','Redd'})
T2=tabulate(txt1(:,1))

X=[];
for i=1:length(T2)
X=[X;repmat(i, [cell2mat(T2(i,2)) 1]),num1(strcmp(txt1(:,1),T2(i)),1:3)];
end
NS=NaN(length(1985:2017),length(T2),2);
for i=1:length(X)
  NS(X(i,2)-1984,X(i,1),:)=X(i,3:4);
end
NS=cat(2,reshape(repmat(1985:2017,[1,2])',[],1,2),NS)

%But "Mainstem" is actually 6 different surveys (next),cedar and snake less than 4 years
NS(:,8:10,:)=[]; %delete
%now add back individual mainstem surveys
[num1,txt1,raw1]=xlsread('North Santiam Basin STW summary.xlsx','Mainstem','C15:N21');
num1=flip(num1,2);%Time is backwards
NS(:,8:13,:)=NaN;
for i =1:6
NS(18:27,7+i,2)=num1(i+1,1:10);
end
%Add mainstem miles
NS(18:27,8,1)=num1(2,12);
NS(18:27,9,1)=num1(3,12);
NS(18:27,10,1)=num1(4,12);
NS(18:27,11,1)=num1(5,12);
NS(18:27,12,1)=num1(6,12);
NS(18:27,13,1)=num1(7,12);
NS_redd_mi=array2table([(1985:2017)', NS(:,2:end,2)./NS(:,2:end,1)],...
    'VariableNames',{'Year',cell2mat(T2(1)), cell2mat(T2(2)), cell2mat(T2(3)), cell2mat(T2(4)), cell2mat(T2(5)),cell2mat(T2(6)), 'Mint_FB', 'FB_Meh', 'Meh_StayIs','StayIs_Stay','Stay_Green','Green_Mouth' })

%Compare mean of ratio to ratio of mean
MoR= nansum(NS(:,2:end,2)./NS(:,2:end,1),2)./sum(~isnan(NS(:,2:end,2)./NS(:,2:end,1)),2 );
%MoR(all(isnan(NS(:,6:11,2))&isnan(NS(:,6:11,1)),2))=NaN
RoM=nansum(NS(:,2:end,2),2)./nansum(NS(:,2:end,1),2);
%RoM(all(isnan(NS(:,6:11,2))&isnan(NS(:,6:11,1)),2))=NaN
NS_ratios=array2table([(1985:2017)' MoR RoM],'VariableNames',{'Year','MoR','RoM'})
T = 
      Creek       Date    Miles     Redd 
    __________    ____    _____    ______
    'Elkhorn'     1999       1         10
    'L_Rock'      1999    0.25          3
    'Mad'         1999     0.7         15
    'Sinker'      1999     0.7          5
    'Rock'        1999     1.5          8
    'Elkhorn'     2000       1          7
    'LNF'         2000       5          3
    'L_Rock'      2000    0.25          0
    'Mad'         2000     0.7         17
    'cedar'       2000     0.6          2
    'Sinker'      2000     0.7          8
    'Rock'        2000     1.5         18
    'Elkhorn'     2001       1          2
    'Rock'        2001     1.5         22
    'Mad'         2001     0.7         48
    'cedar'       2001     0.6          0
    'Sinker'      2001     0.7         18
    'Elkhorn'     2002       1          9
    'Mainstem'    2002    43.5     295.28
    'cedar'       2002     0.6          0
    'Mad'         2002     0.7         45
    'Rock'        2002     1.5         22
    'Sinker'      2002     0.7         10
    'LNF'         2002     9.3      16.74
    'Mainstem'    2003    43.5      648.5
    'Mad'         2003     0.7         27
    'Elkhorn'     2003     0.9         18
    'Sinker'      2003     0.7         13
    'Rock'        2003     1.5         41
    'Snake'       2003     0.5         19
    'LNF'         2003     9.3      16.74
    'Mainstem'    2004    43.5     312.21
    'Rock'        2004     1.5         49
    'Mad'         2004     0.7         43
    'Elkhorn'     2004     0.9          7
    'Sinker'      2004     0.7          4
    'LNF'         2004     9.3       27.9
    'Mainstem'    2005    43.5     268.35
    'Mad'         2005     0.7         20
    'Rock'        2005     1.5         15
    'Elkhorn'     2005     0.9          5
    'Sinker'      2005     0.7          4
    'LNF'         2005     9.3      33.48
    'Mainstem'    2006    43.5      219.9
    'Rock'        2006     1.5         13
    'Elkhorn'     2006     0.9         10
    'Mad'         2006     0.7         16
    'Sinker'      2006     0.7          8
    'LNF'         2006     9.3       18.6
    'Mainstem'    2007    43.5     429.98
    'Mad'         2007     0.7          8
    'Snake'       2007     0.5          3
    'Rock'        2007     1.5         26
    'Elkhorn'     2007     0.9          2
    'Sinker'      2007     0.7          2
    'LNF'         2007     9.3      40.92
    'Mainstem'    2008    43.5     122.84
    'Mad'         2008     0.7          3
    'LNF'         2008     9.3      56.73
    'Mainstem'    2009    43.5     251.55
    'Mad'         2009     0.7          2
    'Rock'        2009     1.5          0
    'Sinker'      2009     0.7          1
    'Snake'       2009     0.5          0
    'Elkhorn'     2009     0.9          1
    'LNF'         2009     9.3      13.95
    'Mainstem'    2010    43.5     404.33
    'Sinker'      2010     0.7          1
    'Rock'        2010     1.5         11
    'LNF'         2010     9.3      31.62
    'Mainstem'    2011    43.5     545.56
    'LNF'         2011     9.3      53.01
    'Elkhorn'     2013     0.9          9
    'Sinker'      2013     0.7          8
    'Rock'        2013     1.5         11
    'Mad'         2014     0.7         15
    'Rock'        2014     1.7         11
    'Rock'        2015     2.7         19
    'Elkhorn'     2015     0.9         12
    'Mad'         2015     0.7         11
    'Rock'        2016     1.5         27
    'Elkhorn'     2016     0.9         11
    'Mad'         2016     0.7          8
    'Mad'         2017     0.7          2
    'Rock'        2017     1.5          2
    'Elkhorn'     1987     0.9         31
    'Elkhorn'     1989     0.8         11
    'Elkhorn'     1991     0.9         10
    'Elkhorn'     1992     0.9         11
    'Elkhorn'     1993     0.5         12
    'Elkhorn'     1994     0.9         11
    'Elkhorn'     1995     0.9          7
    'Elkhorn'     1996     0.9          0
    'Elkhorn'     1997     0.9         10
    'Elkhorn'     1998     0.9         12
    'L_Rock'      1985    0.25          8
    'L_Rock'      1986    0.25         11
    'L_Rock'      1987    0.25          6
    'L_Rock'      1988    0.25          2
    'L_Rock'      1989    0.25          0
    'L_Rock'      1990    0.25          6
    'L_Rock'      1992    0.25          4
    'L_Rock'      1993    0.25          4
    'L_Rock'      1994    0.25          0
    'L_Rock'      1995    0.25          2
    'L_Rock'      1996    0.25          0
    'L_Rock'      1997    0.25          6
    'L_Rock'      1998    0.25          4
    'Sinker'      1985     0.7         26
    'Sinker'      1987     0.7         23
    'Sinker'      1989     0.7          6
    'Sinker'      1992     0.7         11
    'Sinker'      1993     0.7          9
    'Sinker'      1994     0.7         11
    'Sinker'      1995     0.7          1
    'Sinker'      1996     0.7          1
    'Sinker'      1997     0.7          5
    'Sinker'      1998     0.7          6
    'Mad'         1985     0.7         54
    'Mad'         1986     0.7         41
    'Mad'         1987     0.7         30
    'Mad'         1988     0.7         23
    'Mad'         1989     0.7         22
    'Mad'         1990     0.7         44
    'Mad'         1991     0.7         26
    'Mad'         1992     0.7         23
    'Mad'         1993     0.7         33
    'Mad'         1994     0.7         20
    'Mad'         1995     0.7         22
    'Mad'         1996     0.7          2
    'Mad'         1997     0.7         22
    'Mad'         1998     0.7         24
    'Rock'        1985     1.5         63
    'Rock'        1987     1.5         44
    'Rock'        1988     1.5         14
    'Rock'        1992     1.5         22
    'Rock'        1993     1.5         12
    'Rock'        1994     1.5         33
    'Rock'        1995     1.5         18
    'Rock'        1996     1.5          5
    'Rock'        1997     1.5         18
    'Rock'        1998     1.5         36
T2 =
  9×3 cell array
    'Elkhorn'     [23]    [16.197]
    'L_Rock'      [15]    [10.563]
    'Mad'         [29]    [20.423]
    'Sinker'      [22]    [15.493]
    'Rock'        [26]    [ 18.31]
    'LNF'         [11]    [7.7465]
    'cedar'       [ 3]    [2.1127]
    'Mainstem'    [10]    [7.0423]
    'Snake'       [ 3]    [2.1127]
NS(:,:,1) =
  Columns 1 through 6
         1985          NaN         0.25          0.7          0.7          1.5
         1986          NaN         0.25          0.7          NaN          NaN
         1987          0.9         0.25          0.7          0.7          1.5
         1988          NaN         0.25          0.7          NaN          1.5
         1989          0.8         0.25          0.7          0.7          NaN
         1990          NaN         0.25          0.7          NaN          NaN
         1991          0.9          NaN          0.7          NaN          NaN
         1992          0.9         0.25          0.7          0.7          1.5
         1993          0.5         0.25          0.7          0.7          1.5
         1994          0.9         0.25          0.7          0.7          1.5
         1995          0.9         0.25          0.7          0.7          1.5
         1996          0.9         0.25          0.7          0.7          1.5
         1997          0.9         0.25          0.7          0.7          1.5
         1998          0.9         0.25          0.7          0.7          1.5
         1999            1         0.25          0.7          0.7          1.5
         2000            1         0.25          0.7          0.7          1.5
         2001            1          NaN          0.7          0.7          1.5
         2002            1          NaN          0.7          0.7          1.5
         2003          0.9          NaN          0.7          0.7          1.5
         2004          0.9          NaN          0.7          0.7          1.5
         2005          0.9          NaN          0.7          0.7          1.5
         2006          0.9          NaN          0.7          0.7          1.5
         2007          0.9          NaN          0.7          0.7          1.5
         2008          NaN          NaN          0.7          NaN          NaN
         2009          0.9          NaN          0.7          0.7          1.5
         2010          NaN          NaN          NaN          0.7          1.5
         2011          NaN          NaN          NaN          NaN          NaN
         2012          NaN          NaN          NaN          NaN          NaN
         2013          0.9          NaN          NaN          0.7          1.5
         2014          NaN          NaN          0.7          NaN          1.7
         2015          0.9          NaN          0.7          NaN          2.7
         2016          0.9          NaN          0.7          NaN          1.5
         2017          NaN          NaN          0.7          NaN          1.5
  Columns 7 through 10
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
            5          0.6          NaN          NaN
          NaN          0.6          NaN          NaN
          9.3          0.6         43.5          NaN
          9.3          NaN         43.5          0.5
          9.3          NaN         43.5          NaN
          9.3          NaN         43.5          NaN
          9.3          NaN         43.5          NaN
          9.3          NaN         43.5          0.5
          9.3          NaN         43.5          NaN
          9.3          NaN         43.5          0.5
          9.3          NaN         43.5          NaN
          9.3          NaN         43.5          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
NS(:,:,2) =
  Columns 1 through 6
         1985          NaN            8           54           26           63
         1986          NaN           11           41          NaN          NaN
         1987           31            6           30           23           44
         1988          NaN            2           23          NaN           14
         1989           11            0           22            6          NaN
         1990          NaN            6           44          NaN          NaN
         1991           10          NaN           26          NaN          NaN
         1992           11            4           23           11           22
         1993           12            4           33            9           12
         1994           11            0           20           11           33
         1995            7            2           22            1           18
         1996            0            0            2            1            5
         1997           10            6           22            5           18
         1998           12            4           24            6           36
         1999           10            3           15            5            8
         2000            7            0           17            8           18
         2001            2          NaN           48           18           22
         2002            9          NaN           45           10           22
         2003           18          NaN           27           13           41
         2004            7          NaN           43            4           49
         2005            5          NaN           20            4           15
         2006           10          NaN           16            8           13
         2007            2          NaN            8            2           26
         2008          NaN          NaN            3          NaN          NaN
         2009            1          NaN            2            1            0
         2010          NaN          NaN          NaN            1           11
         2011          NaN          NaN          NaN          NaN          NaN
         2012          NaN          NaN          NaN          NaN          NaN
         2013            9          NaN          NaN            8           11
         2014          NaN          NaN           15          NaN           11
         2015           12          NaN           11          NaN           19
         2016           11          NaN            8          NaN           27
         2017          NaN          NaN            2          NaN            2
  Columns 7 through 10
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
            3            2          NaN          NaN
          NaN            0          NaN          NaN
        16.74            0       295.28          NaN
        16.74          NaN        648.5           19
         27.9          NaN       312.21          NaN
        33.48          NaN       268.35          NaN
         18.6          NaN        219.9          NaN
        40.92          NaN       429.98            3
        56.73          NaN       122.84          NaN
        13.95          NaN       251.55            0
        31.62          NaN       404.33          NaN
        53.01          NaN       545.56          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
          NaN          NaN          NaN          NaN
NS_redd_mi = 
    Year    Elkhorn    L_Rock     Mad      Sinker     Rock     LNF    Mint_FB    FB_Meh    Meh_StayIs    StayIs_Stay    Stay_Green    Green_Mouth
    ____    _______    ______    ______    ______    ______    ___    _______    ______    __________    ___________    __________    ___________
    1985       NaN      32       77.143    37.143        42    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1986       NaN      44       58.571       NaN       NaN    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1987    34.444      24       42.857    32.857    29.333    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1988       NaN       8       32.857       NaN    9.3333    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1989     13.75       0       31.429    8.5714       NaN    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1990       NaN      24       62.857       NaN       NaN    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1991    11.111     NaN       37.143       NaN       NaN    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1992    12.222      16       32.857    15.714    14.667    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1993        24      16       47.143    12.857         8    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1994    12.222       0       28.571    15.714        22    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1995    7.7778       8       31.429    1.4286        12    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1996         0       0       2.8571    1.4286    3.3333    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1997    11.111      24       31.429    7.1429        12    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1998    13.333      16       34.286    8.5714        24    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    1999        10      12       21.429    7.1429    5.3333    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    2000         7       0       24.286    11.429        12    0.6     NaN        NaN       NaN          NaN            NaN           NaN        
    2001         2     NaN       68.571    25.714    14.667    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    2002         9     NaN       64.286    14.286    14.667    1.8    16.2        9.4       6.1            3            0.4           4.7        
    2003        20     NaN       38.571    18.571    27.333    1.8    55.5        6.5       4.7          3.6            0.1           1.7        
    2004    7.7778     NaN       61.429    5.7143    32.667      3    17.7        2.8      12.6          7.9            0.2             0        
    2005    5.5556     NaN       28.571    5.7143        10    3.6    20.6        3.1         2          7.3            0.3             0        
    2006    11.111     NaN       22.857    11.429    8.6667      2    14.8        4.9       3.1          3.9            0.4           NaN        
    2007    2.2222     NaN       11.429    2.8571    17.333    4.4    32.3       11.1       2.1          6.1            NaN           NaN        
    2008       NaN     NaN       4.2857       NaN       NaN    6.1    10.7        1.5       0.6          0.3              0           0.3        
    2009    1.1111     NaN       2.8571    1.4286         0    1.5    18.8        3.8       0.6          0.3            1.8             3        
    2010       NaN     NaN          NaN    1.4286    7.3333    3.4    29.5        7.4       0.9          5.5            2.4           1.3        
    2011       NaN     NaN          NaN       NaN       NaN    5.7    41.4       11.1       4.1          6.4            0.7           NaN        
    2012       NaN     NaN          NaN       NaN       NaN    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    2013        10     NaN          NaN    11.429    7.3333    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    2014       NaN     NaN       21.429       NaN    6.4706    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    2015    13.333     NaN       15.714       NaN     7.037    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    2016    12.222     NaN       11.429       NaN        18    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
    2017       NaN     NaN       2.8571       NaN    1.3333    NaN     NaN        NaN       NaN          NaN            NaN           NaN        
NS_ratios = 
    Year     MoR       RoM  
    ____    ______    ______
    1985    47.071    47.937
    1986    51.286    54.737
    1987    32.698    33.086
    1988     16.73    15.918
    1989    13.438    15.918
    1990    43.429    52.632
    1991    24.127      22.5
    1992    18.292    17.531
    1993      21.6    19.178
    1994    15.702    18.519
    1995    12.127    12.346
    1996    1.5238    1.9753
    1997    17.137    15.062
    1998    19.238    20.247
    1999    11.181    9.8795
    2000     9.219    5.7923
    2001    27.738    23.077
    2002    13.076    7.0198
    2003    16.216    13.502
    2004    13.799    7.8288
    2005    7.8856    6.1101
    2006    8.3163    5.0442
    2007    9.9824    8.9912
    2008    2.9732    3.4125
    2009    3.1997    4.7615
    2010    6.5735    8.1445
    2011    11.567    11.337
    2012       NaN       NaN
    2013    9.5873    9.0323
    2014     13.95    10.833
    2015    12.028    9.7674
    2016    13.884    14.839
    2017    2.0952    1.8182

South Santiam

clearvars -except NS NS_redd_mi NS_ratios
[num1,txt1,raw1]=xlsread('SSantiam.xlsx','CrabTree','A3:I35');
[num2,txt2,raw2]=xlsread('SSantiam.xlsx','Thomas','A3:I35');
[num3,txt3,raw3]=xlsread('SSantiam.xlsx','Wiley','A6:I37');%ignore 1966 and 1980

SS(:,1,1)=num1(:,2);
SS(:,1,2)=num1(:,3);
SS(1:(end-1),2,1)=num2(:,2);
SS(1:(end-1),2,2)=num2(:,3);
SS(:,3,1)=num3(:,2);
SS(:,3,2)=num3(:,3);
SS=cat(2,reshape(repmat(1985:2016,[1,2])',[],1,2),SS);

SS_redd_mi=array2table([(1985:2016)',SS(:,2:end,2)./SS(:,2:end,1)],...
    'VariableNames',{'Year','CrabTree','Thomas','Wiley' })

%Compare mean of ratio to ratio of mean
MoR= nansum(SS(:,2:end,2)./SS(:,2:end,1),2)./sum(~isnan(SS(:,2:end,2)./SS(:,2:end,1)),2 );
RoM=nansum(SS(:,2:end,2),2)./nansum(SS(:,2:end,1),2);
SS_ratios=array2table([(1985:2016)' MoR RoM],'VariableNames',{'Year','MoR','RoM'})
SS_redd_mi = 
    Year    CrabTree    Thomas     Wiley 
    ____    ________    ______    _______
    1985     63.333     24.242     29.804
    1986        NaN        NaN     10.833
    1987         15     23.889     18.889
    1988        NaN         20        NaN
    1989        NaN     7.8788     17.581
    1990        NaN          5     4.5455
    1991         15         25     22.083
    1992     19.167         10      23.75
    1993     8.3333     10.556         10
    1994     21.667     11.111     16.452
    1995     6.6667          5       6.25
    1996    0.83333       3.75       3.75
    1997          0     6.1111     9.1667
    1998          0     8.8889     7.0588
    1999          0       6.25        NaN
    2000          0     7.2222     9.1667
    2001          0     17.778     27.917
    2002          5     12.222     22.083
    2003          6     9.4444     14.583
    2004         12     27.222     26.667
    2005          7     6.8493     9.5833
    2006          8          9          5
    2007          3     14.444     19.167
    2008        NaN        NaN        NaN
    2009          1          0    0.83333
    2010          2     2.2222    0.83333
    2011        NaN        NaN        5.6
    2012      2.907        0.5     16.923
    2013     6.4516      13.75       18.4
    2014     1.7308        NaN     12.157
    2015     10.341     11.463     33.265
    2016     1.9318        NaN     28.163
SS_ratios = 
    Year      MoR        RoM  
    ____    _______    _______
    1985     39.127     32.083
    1986     10.833     10.833
    1987     19.259     19.649
    1988         20         20
    1989      12.73     14.211
    1990     4.7727     4.8276
    1991     20.694     20.682
    1992     17.639     18.148
    1993     9.6296     9.8148
    1994      16.41     15.902
    1995     5.9722     6.0714
    1996     2.7778     2.9545
    1997     5.0926     6.1111
    1998     5.3159     6.4198
    1999      3.125        2.5
    2000      5.463     6.4815
    2001     15.231     18.333
    2002     13.102     12.105
    2003     10.009     11.154
    2004     21.963     24.038
    2005     7.8109     7.4766
    2006     7.3333     6.5909
    2007     12.204     14.423
    2008        NaN        NaN
    2009    0.61111    0.57692
    2010     1.6852     1.5385
    2011        5.6        5.6
    2012     6.7767     4.6711
    2013     12.867     13.164
    2014     6.9438     6.8932
    2015     18.357      16.91
    2016     15.048     11.314

Calapooia

clearvars -except SS SS_redd_mi SS_ratios NS NS_redd_mi NS_ratios

[num1,txt1,raw1]=xlsread('Calapooia.xlsx','Mainstem','A8:C39');
[num2,txt2,raw2]=xlsread('Calapooia.xlsx','NFk','A4:C35');
[num3,txt3,raw3]=xlsread('Calapooia.xlsx','Potts','A2:C33');%ignore 1966 and 1980

Ca(:,1,1)=num1(:,3);
Ca(:,1,2)=num1(:,2);
Ca(:,2,1)=num2(:,3);
Ca(:,2,2)=num2(:,2);
Ca(:,3,1)=num3(:,3);
Ca(:,3,2)=num3(:,2);
Ca=cat(2,reshape(repmat(1985:2016,[1,2])',[],1,2),Ca);
%The sheet titled 'Calapooia StW spawning Summary.xls' notes several years
%when the count is biased low becuase of survey conditions or timing.
%Remove these values and impute them subsequently...
%remove=[1988; 1990; 1993; 1995; 1996; 1999; 2000; 2005];
%[c,ia,ib]=intersect(Ca(:,1,1),remove(:));
%Ca(ia,2,1)=NaN;
%Ca(ia,2,2)=NaN;


Ca_redd_mi=array2table([(1985:2016)',Ca(:,2:end,2)./Ca(:,2:end,1)],...
    'VariableNames',{'Year','CrabTree','Thomas','Wiley' })
Ca_redd_mi=array2table([(1985:2016)',Ca(:,2:end,2)./Ca(:,2:end,1)],...
    'VariableNames',{'Year','Mainstem','NFk','Potts' })

%Compare mean of ratio to ratio of mean
MoR= nansum(Ca(:,2:end,2)./Ca(:,2:end,1),2)./sum(~isnan(Ca(:,2:end,2)./Ca(:,2:end,1)),2 );
RoM=nansum(Ca(:,2:end,2),2)./nansum(Ca(:,2:end,1),2);
Ca_ratios=array2table([(1985:2016)' MoR RoM],'VariableNames',{'Year','MoR','RoM'})
Ca_redd_mi = 
    Year    CrabTree    Thomas    Wiley
    ____    ________    ______    _____
    1985    15.467       25        13  
    1986       NaN      NaN       NaN  
    1987      16.4       11       NaN  
    1988       NaN        8        10  
    1989    3.8667        9         6  
    1990       NaN      NaN         7  
    1991    7.8667        9        16  
    1992       2.4        7         6  
    1993       NaN        2         2  
    1994       5.6        1         1  
    1995       3.2        3       NaN  
    1996       NaN      NaN         1  
    1997    7.3333        7         5  
    1998        12        4         3  
    1999    5.1429        8         2  
    2000    5.7333        5         0  
    2001    13.333       12         9  
    2002    11.867        4         2  
    2003    14.667       11         2  
    2004    20.933       23         5  
    2005    3.7333        6         4  
    2006         6        8         7  
    2007       5.2        6         4  
    2008       NaN      NaN       NaN  
    2009       1.2        1         0  
    2010       4.8        0         0  
    2011    4.0278        2       NaN  
    2012         6      2.5       4.5  
    2013       6.4        1         1  
    2014     4.127        6         7  
    2015    26.667      6.5         3  
    2016    22.679       11       NaN  
Ca_redd_mi = 
    Year    Mainstem    NFk    Potts
    ____    ________    ___    _____
    1985    15.467       25     13  
    1986       NaN      NaN    NaN  
    1987      16.4       11    NaN  
    1988       NaN        8     10  
    1989    3.8667        9      6  
    1990       NaN      NaN      7  
    1991    7.8667        9     16  
    1992       2.4        7      6  
    1993       NaN        2      2  
    1994       5.6        1      1  
    1995       3.2        3    NaN  
    1996       NaN      NaN      1  
    1997    7.3333        7      5  
    1998        12        4      3  
    1999    5.1429        8      2  
    2000    5.7333        5      0  
    2001    13.333       12      9  
    2002    11.867        4      2  
    2003    14.667       11      2  
    2004    20.933       23      5  
    2005    3.7333        6      4  
    2006         6        8      7  
    2007       5.2        6      4  
    2008       NaN      NaN    NaN  
    2009       1.2        1      0  
    2010       4.8        0      0  
    2011    4.0278        2    NaN  
    2012         6      2.5    4.5  
    2013       6.4        1      1  
    2014     4.127        6      7  
    2015    26.667      6.5      3  
    2016    22.679       11    NaN  
Ca_ratios = 
    Year      MoR       RoM  
    ____    _______    ______
    1985     17.822    16.211
    1986        NaN       NaN
    1987       13.7    15.765
    1988          9         9
    1989     6.2889    4.6316
    1990          7         7
    1991     10.956    8.8421
    1992     5.1333    3.2632
    1993          2         2
    1994     2.5333    4.6316
    1995        3.1    3.1429
    1996          1         1
    1997     6.4444    7.0526
    1998     6.3333    10.211
    1999     5.0476    5.0909
    2000     3.5778    5.0526
    2001     11.444    12.737
    2002     5.9556        10
    2003     9.2222    12.947
    2004     16.311    19.474
    2005     4.5778         4
    2006          7    6.3158
    2007     5.0667    5.1579
    2008        NaN       NaN
    2009    0.73333    1.0526
    2010        1.6    3.7895
    2011     3.0139    3.7805
    2012     4.3333    5.4737
    2013        2.8    5.2632
    2014      5.709    4.6988
    2015     12.056    21.386
    2016     16.839    20.909

Mollala

clearvars -except SS SS_redd_mi SS_ratios NS NS_redd_mi NS_ratios Ca Ca_redd_mi Ca_ratios

num1=xlsread('Molalla.xlsx','Butte 12.6 to 13.2','A2:C33');
num2=xlsread('Molalla.xlsx','Butte 15.1 to 15.6','A2:C33');
num3=xlsread('Molalla.xlsx','Mill','A2:C33');
num4=xlsread('Molalla.xlsx','Camp','A2:C33');
num5=xlsread('Molalla.xlsx','Mid Fk','A2:C33');
num6=xlsread('Molalla.xlsx','Lukens','A2:C33');
num7=xlsread('Molalla.xlsx','Dead Horse','A2:C33');
num8=xlsread('Molalla.xlsx','North Fk','A2:C33');
num9=xlsread('Molalla.xlsx','Cooper','A2:C33');
num10=xlsread('Molalla.xlsx','Mainstem 45.8 to 46.2','A2:C33');
num11=xlsread('Molalla.xlsx','Mainstem 44.9 to 45.8','A2:C33');
num12=xlsread('Molalla.xlsx','Mainstem 42.1 to 43.4','A2:C33');

Mo(:,1,1)=num1(:,3);
Mo(:,1,2)=num1(:,2);
Mo(:,2,1)=num2(:,3);
Mo(:,2,2)=num2(:,2);
Mo(:,3,1)=num3(:,3);
Mo(:,3,2)=num3(:,2);
Mo(:,4,1)=num4(:,3);
Mo(:,4,2)=num4(:,2);
Mo(:,5,1)=num5(:,3);
Mo(:,5,2)=num5(:,2);
Mo(:,6,1)=num6(:,3);
Mo(:,6,2)=num6(:,2);
Mo(:,7,1)=num7(:,3);
Mo(:,7,2)=num7(:,2);
Mo(:,8,1)=num8(:,3);
Mo(:,8,2)=num8(:,2);
Mo(:,9,1)=num9(:,3);
Mo(:,9,2)=num9(:,2);
Mo(:,10,1)=num10(:,3);
Mo(:,10,2)=num10(:,2);
Mo(:,11,1)=num11(:,3);
Mo(:,11,2)=num11(:,2);
Mo(:,12,1)=num12(:,3);
Mo(:,12,2)=num12(:,2);
%plotmatrix(Mo(:,:,2))

Mo=cat(2,reshape(repmat(1985:2016,[1,2])',[],1,2),Mo);
Mo_redd_mi=array2table([(1985:2016)',Mo(:,2:end,2)./Mo(:,2:end,1)],...
    'VariableNames',{'Year','Butte_12_6','Butte_15_1',...
    'Mill','Camp','Mid_Fk','Lukens','Dead_Horse','North_Fk','Cooper',...
    'Mainstem_45_8','Mainstem_44_9','Mainstem_42_1'})
%Compare mean of ratio to ratio of mean
MoR= nansum(Mo(:,2:end,2)./Mo(:,2:end,1),2)./sum(~isnan(Mo(:,2:end,2)./Mo(:,2:end,1)),2 );
RoM=nansum(Mo(:,2:end,2),2)./nansum(Mo(:,2:end,1),2);
Mo_ratios=array2table([(1985:2016)' MoR RoM],'VariableNames',{'Year','MoR','RoM'})

clearvars -except SS SS_redd_mi SS_ratios NS NS_redd_mi NS_ratios Ca Ca_redd_mi Ca_ratios Mo Mo_redd_mi Mo_ratios
Mo_redd_mi = 
    Year    Butte_12_6    Butte_15_1     Mill     Camp    Mid_Fk    Lukens    Dead_Horse    North_Fk    Cooper    Mainstem_45_8    Mainstem_44_9    Mainstem_42_1
    ____    __________    __________    ______    ____    ______    ______    __________    ________    ______    _____________    _____________    _____________
    1985    6.6667          4           3.3333     36      24       22.857     22            14         25.333     7.5             24.444           33.077       
    1986         5         16           5.5556     28      18       11.429     20            22             20      15             18.889           24.615       
    1987    8.3333         12           6.6667     36      28       15.714     20            14             28    27.5             11.111           23.077       
    1988    6.6667          6           6.6667     32      19       18.571     22            10             26    12.5             25.556           23.077       
    1989    1.6667          4           2.2222     16       9           10     16             6         20.667      10             15.556           24.615       
    1990    1.6667         10           4.4444     32      14       7.1429     20            10         19.333     2.5             5.5556           20.769       
    1991         0          4           3.3333     32      12       2.8571     10             4         12.667      10             7.7778           16.923       
    1992       NaN        NaN           3.3333      0       6       17.143     24            20         22.667      10             15.556           12.308       
    1993       NaN        NaN              NaN      4       1       2.8571      6             2         5.3333       0             7.7778           7.6923       
    1994       NaN        NaN              NaN     20      27       8.5714     12            10             24      15             13.333           22.308       
    1995       NaN        NaN              NaN      8       2            0     14             8         7.3333      10             7.7778           11.538       
    1996       NaN        NaN              NaN     16       2            0      2             2            NaN       5             3.3333           6.1538       
    1997       NaN        NaN              NaN      8       2            0      8             4            NaN     7.5                NaN           13.077       
    1998       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    1999       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2000       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2001       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2002       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2003       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2004       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2005       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2006       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2007       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2008       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2009       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2010       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2011       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2012       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2013       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2014       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2015       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
    2016       NaN        NaN              NaN    NaN     NaN          NaN    NaN           NaN            NaN     NaN                NaN              NaN       
Mo_ratios = 
    Year     MoR       RoM  
    ____    ______    ______
    1985    18.601     20.11
    1986    17.041    17.127
    1987      19.2    19.337
    1988    17.336    18.343
    1989    11.311    12.928
    1990    12.284    12.597
    1991    9.6298    9.5028
    1992    13.101    13.962
    1993    4.0734    4.6809
    1994    16.912    18.723
    1995    7.6277    7.3759
    1996    4.5609    3.7838
    1997    6.0824    6.4516
    1998       NaN       NaN
    1999       NaN       NaN
    2000       NaN       NaN
    2001       NaN       NaN
    2002       NaN       NaN
    2003       NaN       NaN
    2004       NaN       NaN
    2005       NaN       NaN
    2006       NaN       NaN
    2007       NaN       NaN
    2008       NaN       NaN
    2009       NaN       NaN
    2010       NaN       NaN
    2011       NaN       NaN
    2012       NaN       NaN
    2013       NaN       NaN
    2014       NaN       NaN
    2015       NaN       NaN
    2016       NaN       NaN

Get dam counts

WF=xlsread('Steelhead_WLC - Mapes Edits.xls','AllData&Method','C21:C52');
[Mint1, Mint2, Mint3]=xlsread('Steelhead_WLC - Mapes Edits.xls','AllData&Method','G21:G52');
Mint=cell2mat(Mint3);
[Fost1, Fost2, Fost3]=xlsread('Steelhead_WLC - Mapes Edits.xls','AllData&Method','H21:H52');
Fost=cell2mat(Fost3);

Impute all the missing redd/mi

function imputed=Impute2(PC)
xnew=PC;
isnan(PC)<1;

[row,col] = ind2sub(size(PC),find(isnan(PC))); %indices of missing values

for i = 1:length(row)%missing value
BICs=[];
predy=[];      
%isnan(PC(row(i),:))<1;%ones are useable columns
cs=ind2sub(size(PC,2),find(isnan(PC(row(i),:))<1));%get useable column #, same as below
predcols=find(isnan(PC(row(i),:))<1);%column # of x that contains predictor

xi=PC(:,cs);
yi=PC(:,col(i));

%BEGIN regressions
for j = 1:length(predcols) %univariate regression w/ each survey   
    if sum(~isnan(yi(~isnan(xi(:,j)))))>3 %Make sure there are 4 data points in regression
[b, r, stats] = glmfit(xi(~isnan(xi(:,j)),j),yi(~isnan(xi(:,j))),'normal','link','identity');
[pred lo hi]=glmval(b,xi(~isnan(xi(:,j)),j),'identity',stats);

%3 ways to compute the log likelihood
LL1=nansum(log(normpdf(yi(~isnan(xi(:,j))),pred,nanstd(stats.resid))));
N=sum(~isnan(yi(~isnan(xi(:,j)))));
sig=nanstd(stats.resid);
LL2=-N*.5*log(2*pi) - N*log(sig) - (1/(2*sig.^2))*nansum(stats.resid.^2);
K_matrix =  eye(N) * sig^2;
LL3 =-N*.5*log(2*pi) - sum(log(diag(chol(K_matrix)))) ...
    - .5*stats.resid(~isnan(stats.resid))' / (K_matrix)*stats.resid(~isnan(stats.resid));

%make prediction
predy(j)=pred(row(i)-sum(isnan(xi(1:row(i),j))));
BICs(j)=2*-LL3+3*log(N);
    else
    predy(j)=NaN;
    BICs(j)=NaN;
    end
%AIC=2*-L_generic+2*3;
end 

weights1=exp(-0.5*BICs(~isnan(BICs)))./(sum(exp(-0.5*BICs(~isnan(BICs)))));
%weights2=exp(-0.5*devs+k*log(n))./sum(exp(-0.5*devs+k*log(n)));
yhat=sum((predy(~isnan(predy)).*weights1))/sum(weights1);
xnew(row(i),col(i))=yhat;
end

imputed=xnew;
end

%
X=horzcat(NS(1:(end-1),2:end,2)./NS(1:(end-1),2:end,1), SS(:,2:end,2)./SS(:,2:end,1),...
    Ca(:,2:end,2)./Ca(:,2:end,1),Mo(:,2:end,2)./Mo(:,2:end,1),WF,Mint,Fost);
Z=Impute3(X)
Z =
  Columns 1 through 6
       14.892           32       77.143       37.143           42       2.2694
       26.852           44       58.571       30.714       22.333       2.7023
       34.444           24       42.857       32.857       29.333       1.3602
       32.832            8       32.857       35.476       9.3333       3.0169
        13.75            0       31.429       8.5714       31.333       2.3441
       20.872           24       62.857       25.952       25.333       2.6537
       11.111       11.845       37.143        21.19       28.333       2.7499
       12.222           16       32.857       15.714       14.667       2.3186
           24           16       47.143       12.857            8       1.9835
       12.222            0       28.571       15.714           22       2.2489
       7.7778            8       31.429       1.4286           12       2.6041
            0            0       2.8571       1.4286       3.3333       2.6638
       11.111           24       31.429       7.1429           12       2.3896
       13.333           16       34.286       8.5714           24        2.307
           10           12       21.429       7.1429       5.3333       2.3714
            7            0       24.286       11.429           12          0.6
            2       20.935       68.571       25.714       14.667       2.3401
            9        19.01       64.286       14.286       14.667          1.8
           20       23.512       38.571       18.571       27.333          1.8
       7.7778       32.339       61.429       5.7143       32.667            3
       5.5556       6.9306       28.571       5.7143           10          3.6
       11.111       10.458       22.857       11.429       8.6667            2
       2.2222       8.9708       11.429       2.8571       17.333          4.4
        9.823      -3.0858       4.2857       7.8895       18.513          6.1
       1.1111       3.0338       2.8571       1.4286            0          1.5
       5.6356       8.6427       31.332       1.4286       7.3333          3.4
       8.0302       7.4285       29.026       8.8766       15.198          5.7
       9.8527       10.515        30.28       12.689       7.0814       2.4339
           10       10.998        34.01       11.429       7.3333       2.2892
        9.898       7.6057       21.429       16.258       6.4706       2.6011
       13.333        40.95       15.714       11.722        7.037       2.3343
       12.222       35.042       11.429        11.71           18       2.2739
  Columns 7 through 12
       26.239       4.8283       12.145       6.5037      -2.7967     -0.91951
       18.853       4.6071        7.722       5.8896      0.27152       1.5427
       46.798        4.957       9.0054       5.8932     -0.96801       1.3678
       8.4654       2.2848       7.0335       6.6058      0.15131      0.78841
       31.927       6.9513       3.0974       5.0177      0.28935       1.5229
       29.343       7.0534       3.0243        5.097      0.38464       1.2463
       29.717       6.7844       8.3206        5.367      0.23128      0.65417
       28.552       5.6707       4.1743       5.5454      0.20107       1.2298
       42.878       7.7804       4.4496       4.6748     -0.19314       1.7965
       30.003       6.3167       4.7938       5.3248      0.20977       1.4806
       26.832       7.0283       2.3574       4.9167      0.67285        1.785
       21.529       7.9032        1.061       4.4947       1.1866       2.0217
       29.982       7.2288       2.7544       4.8082      0.55907       1.7756
       31.049       6.7812       4.0381       5.0044      0.43971       1.8446
       27.831       6.6796        2.323       4.9497      0.63824       1.9246
       26.175       7.1514       2.7187       4.7978      0.78533       2.1111
       10.228       1.5862       6.7679       6.9231      0.81696       1.3704
         16.2          9.4          6.1            3          0.4          4.7
         55.5          6.5          4.7          3.6          0.1          1.7
         17.7          2.8         12.6          7.9          0.2            0
         20.6          3.1            2          7.3          0.3            0
         14.8          4.9          3.1          3.9          0.4       1.5019
         32.3         11.1          2.1          6.1       0.9792        1.878
         10.7          1.5          0.6          0.3            0          0.3
         18.8          3.8          0.6          0.3          1.8            3
         29.5          7.4          0.9          5.5          2.4          1.3
         41.4         11.1          4.1          6.4          0.7       2.3341
       25.769       6.6932      0.76911       3.7677       1.0638       1.8122
       29.596       7.4089       4.6325       4.8159      0.54493       1.9113
       30.157        7.285       2.1888       4.9602      0.88046       1.5612
       31.522       6.8185       5.8714       5.0444      0.29637       1.5709
        30.53       6.9868       6.2377       4.9723      0.44752       1.7713
  Columns 13 through 18
       63.333       24.242       29.804       15.467           25           13
       20.836       17.309       10.833       12.747       10.708       5.1211
           15       23.889       18.889         16.4           11       7.2086
       15.005           20       19.682       14.028            8           10
       38.332       7.8788       17.581       3.8667            9            6
       26.664            5       4.5455       8.1133       10.479            7
           15           25       22.083       7.8667            9           16
       19.167           10        23.75          2.4            7            6
       8.3333       10.556           10       2.8116            2            2
       21.667       11.111       16.452          5.6            1            1
       6.6667            5         6.25          3.2            3       6.3346
      0.83333         3.75         3.75       5.8216       1.5255            1
            0       6.1111       9.1667       7.3333            7            5
            0       8.8889       7.0588           12            4            3
            0         6.25       12.401       5.1429            8            2
            0       7.2222       9.1667       5.7333            5            0
            0       17.778       27.917       13.333           12            9
            5       12.222       22.083       11.867            4            2
            6       9.4444       14.583       14.667           11            2
           12       27.222       26.667       20.933           23            5
            7       6.8493       9.5833       3.7333            6            4
            8            9            5            6            8            7
            3       14.444       19.167          5.2            6            4
       7.3117       8.5947       12.536       9.1121       10.911       3.1665
            1            0      0.83333          1.2            1            0
            2       2.2222      0.83333          4.8            0            0
       6.0531       10.336          5.6       4.0278            2       3.0597
        2.907          0.5       16.923            6          2.5          4.5
       6.4516        13.75         18.4          6.4            1            1
       1.7308       7.2137       12.157        4.127            6            7
       10.341       11.463       33.265       26.667          6.5            3
       1.9318       12.918       28.163       22.679           11       4.0401
  Columns 19 through 24
       6.6667            4       3.3333           36           24       22.857
            5           16       5.5556           28           18       11.429
       8.3333           12       6.6667           36           28       15.714
       6.6667            6       6.6667           32           19       18.571
       1.6667            4       2.2222           16            9           10
       1.6667           10       4.4444           32           14       7.1429
            0            4       3.3333           32           12       2.8571
     -0.69984       3.6766       3.3333            0            6       17.143
      -1.4014       8.0264       4.8162            4            1       2.8571
       1.2021       4.9838       3.0734           20           27       8.5714
     -0.40179       3.0868        2.401            8            2            0
      -0.4838       8.4514       1.2687           16            2            0
       2.0222       5.2265        2.918            8            2            0
       4.7875       6.7688       3.2447       28.612       18.726       5.8808
      0.81027       4.7596       2.7579       13.727         9.51       4.3092
       1.1494       5.0799       2.3321       15.035       10.583       5.9459
       5.5778       6.7392       1.6696       31.592       21.111       12.147
       4.7199       6.7912       2.6384       28.349       18.685       8.1439
       6.3456       7.6145       4.2394       34.454       22.749       10.384
       9.9994       9.2591       2.4322       47.967       30.238       6.6722
      -0.0118       4.2095       2.0968       10.666       7.7017       3.9529
       1.3063       4.6854       2.9242       15.613       10.905        6.496
      0.83958       4.6477       1.6039       13.833       9.5469       2.8461
      0.30969       5.9894       3.3381       13.957       7.7278       4.7823
      -1.4927       3.7322       1.4461       5.1414       4.0335        1.574
      0.60232       4.8173       3.7825       12.952       8.8269       2.1705
        0.138       3.3962       2.8946       11.319       8.3885       6.6538
       1.3024       4.8665       4.3933       15.633       11.183        5.525
       1.5326       5.2239       2.7662       16.478        11.39       6.4321
      0.21509       4.1222       4.7359       11.535       8.4207       4.5335
       13.313       11.164       3.3086       60.763       39.586       14.098
       11.046       10.542       3.1299       52.061       34.251       11.461
  Columns 25 through 30
           22           14       25.333          7.5       24.444       33.077
           20           22           20           15       18.889       24.615
           20           14           28         27.5       11.111       23.077
           22           10           26         12.5       25.556       23.077
           16            6       20.667           10       15.556       24.615
           20           10       19.333          2.5       5.5556       20.769
           10            4       12.667           10       7.7778       16.923
           24           20       22.667           10       15.556       12.308
            6            2       5.3333            0       7.7778       7.6923
           12           10           24           15       13.333       22.308
           14            8       7.3333           10       7.7778       11.538
            2            2       11.761            5       3.3333       6.1538
            8            4       16.583          7.5       5.5475       13.077
       16.532       10.177       18.717       14.463       5.5728       20.198
       14.633       8.7017       15.643       10.488        5.536       14.361
       14.849       9.0605       16.735       10.824       5.5404       15.422
       17.035        10.62       21.326         15.2       5.5848       20.039
       16.555       10.128       19.273       14.356       7.1363       19.531
       17.437       11.013       21.506       16.052       7.4717         22.6
       18.964       11.652       21.461       19.547       9.3629       27.014
       14.283       8.6075       15.292       9.6691       7.7139       14.586
       14.976          9.1       16.943       11.016       8.0444       15.842
       14.623       8.8621       15.415       10.493       6.4673       15.855
       11.319       6.5652       15.335       8.9198       9.7215       13.765
       13.472        7.811       13.222       8.1556       5.8205       11.592
       14.521       8.5716       14.714       10.296       6.1404       14.489
       14.672       9.2895       17.448       9.9455       12.332       16.175
       15.092       9.4706       17.794       11.066       6.4288       16.251
       15.058       9.1027       16.946       11.217       7.5616       15.788
        14.53       8.8623        16.29       9.9561         6.05        14.06
       20.726       12.475       26.574       22.927       8.8745       27.521
       19.617       12.069         25.2       20.865       6.2211       25.816
  Columns 31 through 33
        20592          303          355
        21251          523          326
        16765          498          214
        23378          844          656
         9572          187          222
        11107          208          272
         4943          166          139
         5396          355          361
         3568           23          256
         5300          235          234
         4693          164          297
         1801           28          131
         4544          149          336
         3678          231          359
         6904          249          328
         4761          168          331
        12525         1156          783
        16656          436         1002
         9092          173          854
        11842          330         1015
         5963          662          628
         6404          225          419
         5494           77          209
         4830       361.97          253
         2813       333.09          192
         7337          143          426
         7441        321.5          315
         7616       267.09          327
         4944          100          286
         5349          179          215
         4508          186          129
         5778          194          206

Get mean of ratio, MoR=mean(count/mi) and relative density

%Can't back calculate redd counts without a length to get RoM
%C=Z.*horzcat(NS(1:(end-1),2:end,1), SS(:,2:end,1),Ca(:,2:end,1),Mo(:,2:end,1));

%how many sites, i, in each population
i_NS=size(NS,2)-1; %subtract 1 for the year column
i_SS=size(SS,2)-1;
i_Ca=size(Ca,2)-1;
i_Mo=size(Mo,2)-1;

NS_MoR= mean(Z(:,1:i_NS),2);
SS_MoR= mean(Z(:,i_NS+1:(i_NS+i_SS)),2);
Ca_MoR= mean(Z(:,(i_NS+i_SS+1):(i_NS+i_SS+i_Ca)),2);
Mo_MoR= mean(Z(:,(i_NS+i_SS+i_Ca+1):(i_NS+i_SS+i_Ca+i_Mo)),2);

MoR=array2table([(1985:2016)',NS_MoR,SS_MoR,Ca_MoR,Mo_MoR],...
    'VariableNames',{'Year','N_Santiam','S_Santiam','Calapooia','Molalla' })

RelDensity=array2table([(1985:2016)',MoR{:,2:end}./sum(MoR{:,2:end},2)],...
 'VariableNames',{'Year','N_Santiam','S_Santiam','Calapooia','Molalla' })
MoR = 
    Year    N_Santiam    S_Santiam    Calapooia    Molalla
    ____    _________    _________    _________    _______
    1985    20.954        39.127       17.822      18.601 
    1986    18.672        16.326       9.5251      17.041 
    1987    19.325        19.259       11.536        19.2 
    1988    12.237        18.229       10.676      17.336 
    1989    11.353        21.264       6.2889      11.311 
    1990    17.318         12.07       8.5307      12.284 
    1991    13.621        20.694       10.956      9.6298 
    1992    11.596        17.639       5.1333      11.165 
    1993    14.281        9.6296       2.2705      4.0085 
    1994     10.74         16.41       2.5333      13.456 
    1995    8.9026        5.9722       4.1782      6.1446 
    1996    4.0399        2.7778       2.7824      4.7904 
    1997    11.265        5.0926       6.4444      6.2395 
    1998    12.305        5.3159       6.3333      12.807 
    1999    8.5519        6.2171       5.0476      8.7697 
    2000    8.2545         5.463       3.5778      9.3797 
    2001    13.493        15.231       11.444      14.054 
    2002    13.571        13.102       5.9556      13.025 
    2003    16.824        10.009       9.2222      15.156 
    2004    15.344        21.963       16.311      17.881 
    2005     7.806        7.8109       4.5778      8.2306 
    2006    7.9269        7.3333            7      9.8209 
    2007    8.4724        12.204       5.0667      8.7527 
    2008    4.7438        9.4809       7.7298      8.4774 
    2009    3.1859       0.61111      0.73333       6.209 
    2010    8.7311        1.6852          1.6      8.4904 
    2011    11.691        7.3296       3.0292      9.3876 
    2012    9.3939        6.7767       4.3333       9.917 
    2013    10.414        12.867          2.8      9.9581 
    2014    9.2745        7.0338        5.709      8.6093 
    2015    11.851        18.357       12.056      21.777 
    2016    11.802        14.338       12.573      19.357 
RelDensity = 
    Year    N_Santiam    S_Santiam    Calapooia    Molalla
    ____    _________    _________    _________    _______
    1985    0.21713       0.40544      0.18468     0.19275
    1986    0.30329       0.26519      0.15472      0.2768
    1987    0.27878       0.27783      0.16642     0.27697
    1988    0.20926       0.31172      0.18256     0.29646
    1989    0.22608       0.42345      0.12524     0.22524
    1990    0.34496       0.24042      0.16993     0.24469
    1991     0.2481       0.37695      0.19955     0.17541
    1992    0.25467       0.38738      0.11274     0.24521
    1993    0.47304       0.31897      0.07521     0.13278
    1994    0.24897       0.38039     0.058724     0.31192
    1995    0.35331       0.23701      0.16582     0.24386
    1996    0.28074       0.19303      0.19335     0.33289
    1997    0.38789       0.17536       0.2219     0.21485
    1998    0.33472       0.14461      0.17229     0.34838
    1999    0.29916       0.21748      0.17657     0.30678
    2000    0.30945        0.2048      0.13413     0.35163
    2001    0.24885       0.28091      0.21106     0.25918
    2002    0.29725       0.28698      0.13045     0.28531
    2003    0.32852       0.19545      0.18008     0.29594
    2004     0.2146       0.30718      0.22813     0.25008
    2005    0.27461       0.27479      0.16105     0.28955
    2006    0.24709       0.22859       0.2182     0.30613
    2007    0.24561       0.35378      0.14688     0.25373
    2008    0.15588       0.31155        0.254     0.27857
    2009    0.29666      0.056904     0.068285     0.57815
    2010    0.42577      0.082178     0.078024     0.41403
    2011    0.37189       0.23315     0.096354     0.29861
    2012     0.3088       0.22276      0.14245     0.32599
    2013    0.28896       0.35703     0.077693     0.27631
    2014    0.30283       0.22966      0.18641     0.28111
    2015    0.18506       0.28664      0.18825     0.34006
    2016    0.20324       0.24691      0.21652     0.33334

Apportion Willamette Falls counts to the populations

WF1=WF.*(39+29+7+31)/170; %Jepson et al. 2014 radio telemetry of fish at SS NS Ca Mo
WF2=WF1-Z(:,32)-Fost; %subtract Minto(with imputed years) and Foster
miles=[98 130 55 154]; %spawning miles [NS(below Mint), SS(below Foster), Ca, Mo]
%These mileages computed by Erin Gilbert
N=MoR{:,2:5}.*miles./sum(MoR{:,2:5}.*miles,2).*(WF2+[Z(:,32) Fost zeros(length(WF2),2)]); %add Minto and Foster back
N=array2table([(1985:2016)',N], 'VariableNames',{'Year','N_Santiam','S_Santiam','Calapooia','Molalla' })
figure
plot(1985:2016,N{:,2:5},'.-','MarkerSize',12),xlim([1985,2016]),title('Winter Steelhead')
legend('N. Santiam','S. Santiam','Calapooia','Molalla')
clear WF1 WF2 miles NS_MoR SS_MoR Ca_MoR Mo_MoR
N = 
    Year    N_Santiam    S_Santiam    Calapooia    Molalla
    ____    _________    _________    _________    _______
    1985    2333.9       5805.1         1087       3176.7 
    1986    3330.8       3804.5       915.02       4583.6 
    1987    2427.4         3120       773.68       3605.5 
    1988    2445.7       4767.6       1124.9       5114.7 
    1989    1071.9       2679.4       322.39       1623.5 
    1990    2006.7       1873.1       537.42       2166.9 
    1991    642.89       1283.8       273.84       673.98 
    1992    628.45       1270.6       137.68       838.49 
    1993    811.92       812.21       71.602       353.94 
    1994    598.83       1213.3       73.204       1088.7 
    1995    812.02       759.16       200.54       825.78 
    1996    238.34       239.97       89.526       431.58 
    1997    894.73       576.74       270.13        732.3 
    1998    553.06       337.93       140.68       796.54 
    1999    1017.9       1001.1       316.06       1537.5 
    2000    675.13       629.34       153.77       1128.8 
    2001    1524.2       2161.2       606.17       2084.2 
    2002    2325.4       3157.7       546.12       3344.4 
    2003    1370.9       1234.9       406.59       1870.9 
    2004    1195.7       2514.5       676.37       2076.1 
    2005     716.4       940.46       185.27       932.71 
    2006    765.38       990.25       355.44       1396.3 
    2007    660.54       1313.9       216.38       1046.7 
    2008    374.11       952.66       297.23       912.74 
    2009    351.31       81.317       35.705       846.47 
    2010    1437.1       393.04        142.7       2120.3 
    2011    1335.2       1108.8       179.73       1559.6 
    2012    1141.2       1106.8       277.59       1778.8 
    2013    651.53       1138.9       94.798       944.01 
    2014    818.92       833.37       266.68         1126 
    2015    411.76       828.05       218.77       1106.5 
    2016    586.98        949.3       330.91       1426.5 

Get age

age1=xlsread('Age Compilation.xlsx','StW Age Data Compilation','B4:D52');
age2=xlsread('Age Compilation.xlsx','StW Age Data Compilation','F4:H14');
age3=xlsread('Age Compilation.xlsx','StW Age Data Compilation','K4:M18');
A2012=xlsread('StW age structure.xlsx','2012','E2:E97');
A2013=xlsread('StW age structure.xlsx','2013','E2:E118');
A2014=xlsread('StW age structure.xlsx','2013','E2:E112');
A2012=[repmat(2012,[size(A2012),1]),A2012, ones(length(A2012),1)];
A2013=[repmat(2013,[size(A2013),1]),A2013, ones(length(A2013),1)];
A2014=[repmat(2014,[size(A2014),1]),A2014, ones(length(A2014),1)];
age=vertcat(age1,age2,age3,A2012,A2013,A2014);
A=zeros(length(unique(age(:,1))),7);
A(:,1)=unique(age(:,1));

for i =1:size(A,1)
 Ai=age(age(:,1)==A(i,1),2:3);
    for j=1:size(Ai,1)
    A(i,1+Ai(j,1))=A(i,1+Ai(j,1))+Ai(j,2);
    end
end
%A=array2table(A, 'VariableNames',{'Year','age1','age2','age3','age4','age5','age6' });
clear Ai age age1 age2 age3 A2012 A2013 A2014
Age=NaN(length(1985:2016),7);
Age(:,1)=1985:2016;
Age((A(:,1)-1984),2:7)=A(:,2:7);
Age=array2table(Age, 'VariableNames',{'Year','age1','age2','age3','age4','age5','age6' });
Age
Age = 
    Year    age1    age2    age3    age4    age5    age6
    ____    ____    ____    ____    ____    ____    ____
    1985    NaN     NaN     NaN     NaN     NaN     NaN 
    1986      9       0       4      22      17       1 
    1987      4       0       0       2       7       0 
    1988     21       4       0      52      38       5 
    1989      4       0       0       4       8       2 
    1990     10       0       0       7       6       1 
    1991      0       0       4       0       3       1 
    1992      4       0       0      21      11       0 
    1993      0       0       0       0       1       0 
    1994      0       0       0       1       0       0 
    1995    NaN     NaN     NaN     NaN     NaN     NaN 
    1996    NaN     NaN     NaN     NaN     NaN     NaN 
    1997    NaN     NaN     NaN     NaN     NaN     NaN 
    1998    NaN     NaN     NaN     NaN     NaN     NaN 
    1999    NaN     NaN     NaN     NaN     NaN     NaN 
    2000    NaN     NaN     NaN     NaN     NaN     NaN 
    2001      0       0      25      69       8       0 
    2002    NaN     NaN     NaN     NaN     NaN     NaN 
    2003      0       0       0      22       8       0 
    2004      0       0       9      35       3       0 
    2005      0       0       1       5       1       0 
    2006    NaN     NaN     NaN     NaN     NaN     NaN 
    2007    NaN     NaN     NaN     NaN     NaN     NaN 
    2008    NaN     NaN     NaN     NaN     NaN     NaN 
    2009    NaN     NaN     NaN     NaN     NaN     NaN 
    2010    NaN     NaN     NaN     NaN     NaN     NaN 
    2011    NaN     NaN     NaN     NaN     NaN     NaN 
    2012      0       0       2      50      33      11 
    2013      0       0       2      69      36      10 
    2014      0       0       2      69      36       4 
    2015    NaN     NaN     NaN     NaN     NaN     NaN 
    2016    NaN     NaN     NaN     NaN     NaN     NaN 

California sea lion mortality

%Basic pinniped mort data from Wright et al.(2016) report
M=[2014 1703 496 712 780; 2015 4149 899 172 557;2016 2252 650 768 915];
M=array2table(M, 'VariableNames',{'Year','H_ChS','W_ChS','StS','StW' })


%Figure 2 (page14) of Wright et al. (2016) shows the spatial and...
%temporal coverage of the mortality estimates.

%In 2016, only falls stratum was estimated.  Wright estimates the "river"
%stratum proportion is 0.385.  Expand 2016 to include "river"
M{3,5}=M{3,5}/(1-0.385);

%Mean proportion of Pinn mort that is StW
mean_prop=mean(M{:,5}./sum(M{:,2:5},2));

%Remove harvest of StW that aren't in the 4 focal populations
M{:,5}= M{:,5}.*(39+29+7+31)/170 %Jepson et al.

%Start date of the mortality estimate misses a lot of the StW run.
%Get daily counts of StW at Willamette Falls and compare with CSL
%timeseries.  Note that Nov1 is start of "winter" run.
figure('Position',[1050 400 900 800])
N14=xlsread('FallsCounts.xlsx','2013_2014','C2:E213');
%yyaxis left
t=datetime(2013, 11, 1)+ caldays(1:212);
subplot(3,1,1),plot(t,N14(:,2),'.-'),ylim([0 max(N14(:,2))]),title('Willamette Falls'),ylabel('StW Daily Count')
hold on
subplot(3,1,1),plot([t(123),t(123)],[0 max(N14(:,2))],'col',[1 0 0])
%yyaxis right
%plot(t,N(:,3)),ylim([0 max(N(:,3))]),title('Willamette Falls'),ylabel('CSL Max Count')
sum(N14(1:123,2))/sum(N14(:,2))% 23% percent of run not included in mortality study

N15=xlsread('FallsCounts.xlsx','2014_2015','C2:E213');
%yyaxis left
t=datetime(2014, 11, 1)+ caldays(1:212);
subplot(3,1,2),plot(t,N15(:,2),'.-'),ylim([0 max(N15(:,2))]),title('Willamette Falls'),ylabel('StW Daily Count')
hold on
subplot(3,1,2),plot([t(101),t(101)],[0 max(N15(:,2))],'col',[1 0 0])
%yyaxis right
%plot(t,N(:,3)),ylim([0 max(N(:,3))]),title('Willamette Falls'),ylabel('CSL Max Count')
sum(N15(1:101,2))/sum(N15(:,2))% 30% of run not included in mortaltiy study

N16=xlsread('FallsCounts.xlsx','2015_2016','C2:E214');
t=datetime(2015, 11, 1)+ caldays(1:213);
subplot(3,1,3)
yyaxis left
plot(t,N16(:,2),'.-'),ylim([0 max(N16(:,2))]),title('Willamette Falls'),ylabel('StW Daily Count')
yyaxis right
subplot(3,1,3),plot(t,N16(:,3),'.-'),ylim([0 max(N16(:,3))]),title('Willamette Falls'),ylabel('CSL Max Count')
hold on
subplot(3,1,3),plot([t(93),t(93)],[0 max(N16(:,3))],'col',[1 0 0])
sum(N16(1:93,2))/sum(N16(:,2))% 22% of run not included in mortaltiy study

%smooth CSL data
CSL=smooth(93:209,N16(93:209,3),0.4,'loess');
figure('Position',[1050 600 700 300])
plot(t(93:209),N16(93:209,3),'.-r')
hold on
plot(t(93:209),CSL,'-g')
plot(t(1:93),repmat(CSL(1),93,1),'-k')
ylabel('CSL max count')
legend('observed','loess smooth','Assumed','Location','northwest')
CSL=[repmat(CSL(1),92,1); CSL; [0 0 0 0]'];

% For 2016, compute "overlap of the StW and CSL
i=sum(CSL(93:end).*N16(93:end,2));%interaction index for mort estimate
i_star=sum(CSL.*N16(:,2));%complete interaction index
EF16=i_star/i

% For 2015, compute "overlap of the StW and CSL
N15=xlsread('FallsCounts.xlsx','2014_2015','D2:D213');
CSL(121)=[];%delete leap year
%mort study begins Feb9, which is StW day 101
i=sum(CSL(101:end).*N15(101:end));%interaction index for mort estimate
i_star=sum(CSL.*N15);%complete interaction index
EF15=i_star/i

% For 2014, compute "overlap of the StW and CSL
N14=xlsread('FallsCounts.xlsx','2013_2014','D2:D213');
%mort study begins March3, which is StW day 123
i=sum(CSL(123:end).*N14(123:end));%interaction index for mort estimate
i_star=sum(CSL.*N14);%complete interaction index
EF14=i_star/i

M{:,5}= M{:,5}.*[EF14;EF15;EF16]% expand for proportion of run not surveyed for mort

%Assume no mortality prior to 1995
M1985_1994=repmat(0,length(1985:1994),1);
%Wright et al. (2014):  "predation losses were generally a few hundred or less" during late 1990s-2003
%How does 150 salmonid morts scale to these StW?
%start+RiverStratum*ProportionStW*FocalPops*RunTimeAdjustment
M1995_2003(1:length(1995:2003))=150/(1-0.385)*mean_prop*(39+29+7+31)/170*mean([EF14,EF15,EF16]);
%linear increas from 2004 to 2013
Nsteps=2013-2004+2;
step=(M{1,5}-M1995_2003(end))/Nsteps;
M2004_2013(1)=M1995_2003(end)+step;
for i=2:length(2004:2013)
 M2004_2013(i)= M2004_2013(i-1)+step;
end
Mall=horzcat((1985:2016)',vertcat(M1985_1994,M1995_2003',M2004_2013',M{:,5}));
figure
plot(Mall(:,1),Mall(:,2),'.-')
ylabel('CSL Predation on Focal Winter Steelhead')
RelAbund=N{:,2:5}./sum(N{:,2:5},2);
Mort=array2table([Mall(:,1:2),Mall(:,2).*RelAbund],'VariableNames',{'Year','Total_Mort','N_Santiam','S_Santiam','Calapooia','Molalla' })

HR=array2table([Mort{:,1},Mort{:,3:6}./( Mort{:,3:6}+N{:,2:5})],'VariableNames',{'Year','N_Santiam','S_Santiam','Calapooia','Molalla' })

clear A Ca CSL EF14 EF15 Fost Fost1 Fost2 Fost3 i i_CA i_Mo i_NS i_SS
clear i_star j mean_prop Mint Mint1 dMint2 Mint3 Mo N14 N15 N16 NS SS t WF X Z
clear EF16 i_Ca Mint2
M = 
    Year    H_ChS    W_ChS    StS    StW
    ____    _____    _____    ___    ___
    2014    1703     496      712    780
    2015    4149     899      172    557
    2016    2252     650      768    915
M = 
    Year    H_ChS    W_ChS    StS     StW  
    ____    _____    _____    ___    ______
    2014    1703     496      712    486.35
    2015    4149     899      172    347.31
    2016    2252     650      768    927.69
ans =
      0.23257
ans =
      0.30358
ans =
      0.21668
EF16 =
       1.0952
EF15 =
       1.1375
EF14 =
       1.0913
M = 
    Year    H_ChS    W_ChS    StS     StW  
    ____    _____    _____    ___    ______
    2014    1703     496      712    530.74
    2015    4149     899      172    395.05
    2016    2252     650      768      1016
Mort = 
    Year    Total_Mort    N_Santiam    S_Santiam    Calapooia    Molalla
    ____    __________    _________    _________    _________    _______
    1985         0             0            0            0            0 
    1986         0             0            0            0            0 
    1987         0             0            0            0            0 
    1988         0             0            0            0            0 
    1989         0             0            0            0            0 
    1990         0             0            0            0            0 
    1991         0             0            0            0            0 
    1992         0             0            0            0            0 
    1993         0             0            0            0            0 
    1994         0             0            0            0            0 
    1995    33.487        10.469        9.787       2.5854       10.646 
    1996    33.487         7.986       8.0404       2.9997       14.461 
    1997    33.487        12.111       7.8068       3.6565       9.9125 
    1998    33.487         10.13       6.1898       2.5769        14.59 
    1999    33.487        8.8017       8.6567        2.733       13.295 
    2000    33.487        8.7391       8.1464       1.9904       14.611 
    2001    33.487        8.0054       11.351       3.1837       10.947 
    2002    33.487        8.3073       11.281        1.951       11.948 
    2003    33.487        9.4008       8.4684       2.7882        12.83 
    2004    78.691        14.559       30.617       8.2358       25.279 
    2005     123.9        31.987       41.991       8.2724       41.645 
    2006     169.1        36.901       47.743       17.137        67.32 
    2007    214.31        43.724       86.974       14.324       69.283 
    2008    259.51        38.272       97.458       30.407       93.374 
    2009    304.71        81.419       18.846        8.275       196.17 
    2010    349.92        122.85       33.601         12.2       181.26 
    2011    395.12        126.12       104.73       16.975        147.3 
    2012    440.33        116.74       113.23       28.397       181.97 
    2013    485.53        111.81       195.45       16.269          162 
    2014    530.74        142.74       145.25       46.481       196.27 
    2015    395.05        63.414       127.53       33.692       170.42 
    2016      1016        181.07       292.84       102.08       440.03 
HR = 
    Year    N_Santiam    S_Santiam    Calapooia     Molalla 
    ____    _________    _________    _________    _________
    1985            0            0            0            0
    1986            0            0            0            0
    1987            0            0            0            0
    1988            0            0            0            0
    1989            0            0            0            0
    1990            0            0            0            0
    1991            0            0            0            0
    1992            0            0            0            0
    1993            0            0            0            0
    1994            0            0            0            0
    1995     0.012728     0.012728     0.012728     0.012728
    1996      0.03242      0.03242      0.03242      0.03242
    1997     0.013355     0.013355     0.013355     0.013355
    1998     0.017987     0.017987     0.017987     0.017987
    1999    0.0085731    0.0085731    0.0085731    0.0085731
    2000     0.012779     0.012779     0.012779     0.012779
    2001    0.0052248    0.0052248    0.0052248    0.0052248
    2002    0.0035598    0.0035598    0.0035598    0.0035598
    2003    0.0068108    0.0068108    0.0068108    0.0068108
    2004      0.01203      0.01203      0.01203      0.01203
    2005     0.042741     0.042741     0.042741     0.042741
    2006     0.045995     0.045995     0.045995     0.045995
    2007     0.062085     0.062085     0.062085     0.062085
    2008     0.092807     0.092807     0.092807     0.092807
    2009      0.18815      0.18815      0.18815      0.18815
    2010     0.078756     0.078756     0.078756     0.078756
    2011     0.086301     0.086301     0.086301     0.086301
    2012     0.092804     0.092804     0.092804     0.092804
    2013      0.14648      0.14648      0.14648      0.14648
    2014      0.14843      0.14843      0.14843      0.14843
    2015      0.13345      0.13345      0.13345      0.13345
    2016      0.23575      0.23575      0.23575      0.23575

Get proportions of hatchery-origin spawners from previous planning documents

Ph_NS=xlsread('Steelhead_WLC - Mapes Edits.xls','Nsant','B14:B37') ;
Ph_NS=vertcat(Ph_NS,zeros(8,1)); %no hatchery fish 2009-2016;
Ph_SS=xlsread('Steelhead_WLC - Mapes Edits.xls','SSantTotal','N25:N48') ;
Ph_SS=vertcat(Ph_SS,zeros(8,1)); %no hatchery fish 2009-2016;
Ph_Ca=zeros(32,1); %never hatchery fish in Calapooia
Ph_Mo=xlsread('Steelhead_WLC - Mapes Edits.xls','Molalla','D15:D38') ;
Ph_Mo=vertcat(Ph_Mo,zeros(8,1)); %no hatchery fish 2009-2016;
Ph=array2table([(1985:2016)',Ph_NS, Ph_SS, Ph_Ca, Ph_Mo],'VariableNames',{'Year','N_Santiam','S_Santiam','Calapooia','Molalla' } )

clear Ph_NS Ph_SS Ph_Ca Ph_Mo
cd('\\Kalawatseti\home\falcym\docs\Projects\WillamettePinnipedSteelhead')
Ph = 
    Year    N_Santiam    S_Santiam     Calapooia    Molalla
    ____    _________    __________    _________    _______
    1985       0.148        0.15601    0             0.3   
    1986       0.148         0.1541    0             0.3   
    1987       0.148       0.095989    0             0.3   
    1988       0.148        0.13281    0             0.3   
    1989       0.148       0.036578    0             0.3   
    1990       0.148      0.0050463    0             0.3   
    1991       0.148              0    0             0.3   
    1992       0.148     0.00088147    0            0.11   
    1993     0.16279      0.0015505    0            0.11   
    1994     0.13201              0    0            0.11   
    1995       0.111              0    0            0.11   
    1996       0.111              0    0            0.11   
    1997    0.088608      0.0010211    0            0.11   
    1998     0.30519              0    0            0.11   
    1999     0.26849              0    0            0.01   
    2000     0.12437              0    0               0   
    2001           0              0    0               0   
    2002           0      0.0026291    0               0   
    2003           0       0.001968    0               0   
    2004           0              0    0               0   
    2005           0              0    0               0   
    2006           0              0    0               0   
    2007           0              0    0               0   
    2008           0              0    0               0   
    2009           0              0    0               0   
    2010           0              0    0               0   
    2011           0              0    0               0   
    2012           0              0    0               0   
    2013           0              0    0               0   
    2014           0              0    0               0   
    2015           0              0    0               0   
    2016           0              0    0               0   

Calculate recruits

Use same age comp data for all 4 pops on years when data exist. Use
mean composition when data do not exist.
Age2=Age{:,2:7}./sum(Age{:,2:7},2);
mean_age=nanmean(Age2);
Age2(isnan(Age2(:,1)),:)=repmat(mean_age,[sum(isnan(Age2(:,1))),1]);
%matrix of Wild spawner abundance at age
for p=1:4
   Wvec(:,:,p)= (1-Ph{:,p+1}).*N{:,p+1}.*Age2;
end
%Post-mortality Recruits
for p=1:4
    for t=1:(size(Wvec,1)-6)
     R1(t,p)=sum(diag(Wvec(t+(1:6),:,p)));
    end
end
%Pre-mortality Recruits
%Use Conservation Plan's harvest rates of 0.23 through 1992 and 0.07 after.
Harv=vertcat(repmat(0.23,[length(1985:1992),4]),repmat(0.07,[length(1993:2016),4]));
R2=R1./(1-Harv(7:end,:))+Mort{7:end,3:6};
names={'N.Santiam','S.Santiam','Calapooia','Molalla' };
for p=1:4
subplot(2,2,p),scatter(N{1:end-6,1+p},R2(:,p))
title(names(p)),xlabel('Spawners'),ylabel('Recruits')
end

Preliminaries for JAGS

Data = struct('S',N{1:end-6,2:5},'logR',log(R2));
nchains  = 4; % How Many Chains?
nburnin  = 35000; % How Many Burn-in Samples?
nsamples = 10000;  % How Many Recorded Samples?

% To run a different recrutitment model, change the last number in the
% lines in the loop below to match the needed number of paramters.
clear init I
for i=1:nchains
    I.a= 5+2*rand([1,1]);
    %I.b= 1000+2000*rand([1,4]);%BH
    I.b= 0.01*rand([1,4]);%RK
    I.sig_tau =  0+2*rand([1,4]);  %Log scale
    init(i) = I; % init is a structure array that has the initial values for all latent variables for each chain
end

Calling JAGS to fit recruitment model

Here are the JAGS files for the three Ricker recruitment models

Model 1: http://people.oregonstate.edu/~falcym/Model1.txt Model 2: http://people.oregonstate.edu/~falcym/Model2.txt Model 3: http://people.oregonstate.edu/~falcym/Model3.txt

doparallel =1;
%fprintf( 'Running JAGS...\n' );
tic
[samples, stats, structArray] = matjagsParallel( ...
    Data, ...                % Observed data
    fullfile(pwd, 'Model3.txt'), ...   % File that contains model definition
    init, ...                      % Initial values for latent variables
    'doparallel' , doparallel, ... % Parallelization flag
    'nchains', nchains,...         % Number of MCMC chains
    'nburnin', nburnin,...         % Number of burnin steps
    'nsamples', nsamples, ...      % Number of samples to extract
    'thin', 13, ...                 % Thinning parameter
    'dic', 1, ...                  % Do the DIC?
    'monitorparams', {'a','b','sig','density'}, ...    % List of latent variables to monitor
    'savejagsoutput' , 1 , ...     % Save command line output produced by JAGS?
    'verbosity' , 0 , ...          % 0=do not produce any output; 1=minimal text output; 2=maximum text output
    'cleanup' , 0 );               % clean up of temporary files?
delete(gcp)
Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers.
Analyzing and transferring files to the workers ...done.
Parallel pool using the 'local' profile is shutting down.

Compute WAIC

%lppd = log pointwise predictive density
%lppd=sum(sum(log(1/(nsamples*nchains).*sum(sum(samples.density,2),4)),1),3);
%The above lppd is idential to the one below
reshape1=permute(samples.density,[2,4,3,1]);
reshape2=reshape(reshape1,size(reshape1,1)*size(reshape1,2),[]);%rows are mcmc, cols are data
lppd=sum(log((1/(nsamples*nchains))*sum(reshape2)));
pwaic=sum(var(log(reshape2),0,1));
WAIC=-2*(lppd-pwaic)
WAIC =
       217.43

Inspect model fit

stats.Rhat.a
stats.Rhat.b

figure
p=1;
plot(samples.sig(1,:,p))
hold on
plot(samples.sig(2,:,p))
plot(samples.sig(3,:,p))
plot(samples.sig(4,:,p))

%figure
%{
for p=1:4
subplot(4,2,p*2-1),plot(samples.a(1,:,p));
hold on
subplot(4,2,p*2-1),plot(samples.a(2,:,p));
subplot(4,2,p*2-1),plot(samples.a(3,:,p));
subplot(4,2,p*2-1),plot(samples.a(4,:,p));

subplot(4,2,p*2),plot(samples.b(1,:,p));
hold on
subplot(4,2,p*2),plot(samples.b(2,:,p));
subplot(4,2,p*2),plot(samples.b(3,:,p));
subplot(4,2,p*2),plot(samples.b(4,:,p));
end
%}

figure
subplot(5,1,1),plot(samples.a(1,:));
hold on
subplot(5,1,1),plot(samples.a(2,:));
subplot(5,1,1),plot(samples.a(3,:));
subplot(5,1,1),plot(samples.a(4,:));
for p=1:4
subplot(5,1,1+p),plot(samples.b(1,:,p));
hold on
subplot(5,1,1+p),plot(samples.b(2,:,p));
subplot(5,1,1+p),plot(samples.b(3,:,p));
subplot(5,1,1+p),plot(samples.b(4,:,p));
end

figure
for pop=1:4;
clear Ri Si
Si=1:max(N{1:end-6,1+pop});
%Ri=((stats.mean.a.*Si).*exp(-stats.mean.b(pop).*Si))';
%subplot(2,2,pop),plot(Si,Ri)
%hold on
%subplot(2,2,pop),scatter(N{1:end-6,1+pop},R2(:,pop))

bi=(reshape(samples.b(:,:,pop),[],1));
ai=(reshape(samples.a(:,:),[],1));
for rep=1:100
rani=randi(40000);
  for i=1:length(Si)
     Ri(i,rep)=(Si(i).*ai(rani)).*exp(-bi(rani).*Si(i));
  end
end

for i=1:100
subplot(2,2,pop),plot(Si,Ri(:,i),'col',[0.1,0.1,0.1,0.1])
hold on
end
subplot(2,2,pop),scatter(N{1:end-6,1+pop},R2(:,pop),'or')
xlim([0 max(max(N{1:end-6,1+pop}),max(R2(:,pop)))])
ylim([0 max(max(N{1:end-6,1+pop}),max(R2(:,pop)))])
line([0 max(max(N{1:end-6,1+pop}),max(R2(:,pop)))],[0,max(max(N{1:end-6,1+pop},max(R2(:,pop))))],'Color',[0 0 1])
Ri=((stats.mean.a.*Si).*exp(-stats.mean.b(pop).*Si))';
subplot(2,2,pop),plot(Si,Ri,'g')
xlabel('Spawners')
ylabel('Wild Recruits')
title(names(pop))
end
% plot BH
%{
pop=1;
Si=1:max(N{1:end-6,1+pop});
Ri=(stats.mean.a.*Si)./(1+stats.mean.a/stats.mean.b(1).*Si);
plot(Si,Ri)
hold on
scatter(N{1:end-6,1+pop},R2(:,pop))

Si=1:max(N{1:end-6,1+pop});
for rep=1:100
rani=randi(40000);
  for i=1:length(Si)
     Ri(i,rep)=(Si(i)*ai(rani))/(1+ai(rani)/bi(rani)*Si(i));
  end
end
figure
hold on
for i=1:100
plot(Si,Ri(:,i),'col',[0.1,0.1,0.1,0.1])
end
scatter(N{1:end-6,1+pop},R(:,pop),'or')
xlim([0 max(max(S(1:31),max(R)))])
ylim([0 max(max(S(1:31),max(R)))])
line([0 max(max(S(1:31),max(R)))],[0,max(max(S(1:31),max(R)))],'Color',[0 0 1])
xlabel('Spawners')
ylabel('Wild Recruits')
title(names(pop))
%}
ans =
      0.99996
ans =
      0.99996
            1
            1
      0.99996

Do PVA with MCMC samples

This code is set up to run a PVA on the N.Santiam. Change "pop" immediately below to 2, or 3, or 4 for S.santiam, Calapooia, and Molalla respectively. Scenarios 1 through 4 simulate different effects of sea lions. Note that this PVA is capable of incorporating ocean harvest. There is a lot of useless dividing by 1 in this PVA because there it not attempt to incorporate ocean harvest by age.

pop=1;
scenario=2;

tmax=160;% This allows a burn-in period in the PVA
reps=100;%per rand param
randparams=1000;% rand param
out1=[];

switch scenario
    case 1
    %Scenario 1: No pinniped mortality.  Add 0.07 for incidental fishery
    terminal_h=zeros([26,1])+0.07;
    case 2
    %Scenario 2: Lowest empiricical pinniped mortality (2015), 13% Add 0.07 for incidental
    terminal_h =repmat(HR{(end-1),1+pop}+0.07,[26,1]);
    case 3
    %Scenario 3: 2016 mortality repeats in the future.  Add 0.07 for incidental
    terminal_h=repmat(HR{end,1+pop}+0.07,[26,1]);
    case 4
    %Scenario 4: 2017 mortality (33%)  Add 0.07 for incidental fishery
    terminal_h= repmat(0.33+0.07,[26,1]);
end



Coshak=ones(26,6);%No expansion for ocean harvest. Holdover from ChF PVA

QET=100;
RFT=QET;

TermR=R2(:,pop).*Age2(1:26,:);
CK=Coshak;
TotR=Coshak.*TermR;

for i=1:length(Coshak)
TermR_AComp(i,:)=TermR(i,1:6)./sum(TermR(i,1:6),2);%Terminal Recruit Age Comp
TotR_AComp(i,:)=TotR(i,1:6)./sum(TotR(i,1:6),2);%Terminal Recruit Age Comp
end

tic
%Sample param estimates
for randparam = 1:randparams
j=[randi([1,4]),randi(length(samples.a))];
a=samples.a(j(1),j(2));
b=samples.b(j(1),j(2),pop);

for i=1:length(R2)
    z5(i)=log(R2(i))-(log(a)+log(N{i,1+pop})-b*N{i,1+p});%Ricker
end

rho=corr(z5(1:(length(z5)-1))',z5(2:length(z5))');

%QET1=0;
%QET2=0;
%QET3=0;
QET4=0;

for rep=1:reps
%qet1=0;
%qet2=0;
%qet3=0;
qet4=0;

X=zeros(tmax,29);

%BEGIN simulate normally distributed deviates and Coshak Expansion
sig=std(z5);
X(1,9)=0;
for t=2:tmax
   X(t,9)=rho*X(t-1,9)+sig*sqrt(1-rho^2)*randn;
   i=randperm(length(Coshak),1);
   X(t,11:16)=CK(i,:);
   X(t,17:22)=TotR_AComp(i,:);

   X(t,29)=terminal_h(randperm(length(terminal_h),1));%terminal harvest
end
%END simulate normally distributed deviates and Coshak Expansion

N0=mean(N{:,1+pop});
Ni=N0.*mean_age;

X(1,1)=Ni(1);
X(1:2,2)=Ni(2);
X(1:3,3)=Ni(3);
X(1:4,4)=Ni(4);
X(1:5,5)=Ni(5);
X(1:6,6)=Ni(6);


for t =2:(tmax-7)
  St=sum(X(t,1:7));
  X(t,8)=St;
  lnRt=log(a)+log(St)-b*St+X(t,9);%RK
  %Rt=log(a)+log(St)-log(1+a/b*St)+X(t,8);%BH
  if St>RFT
    X(t,10)=exp(lnRt);
  else
    X(t,10)=0; %Implement RFT
  end

   X(t,23:28)=X(t,10).*X(t,17:22)./X(t,11:16) ;%Terminal Recruits by age
  for i=2:6
    X(t+i,i)=nansum([X(t+i,1),X(t,22+i)*(1-X(t+i,29))]);% Make Spawners
  end
     %{
     if isempty(Ph)==1
     X(t+1:t+7,1:7)=X(t+1:t+7,1:7)+diag(floor(X(t,11).*A));%No hatchery fish
     else
     k=randi(length(Ph));
     H=Ph(k)*X(t,11)/(1-Ph(k));%Ph=H/(H+W). Rearranging gives H=(Ph*W)/(1-Ph)
     X(t+1:t+7,1:7)=X(t+1:t+7,1:7)+diag(floor((H+X(t,11)).*A));%hatchery fish
     end
     %}
end%time t

        u=X(51:150,8)<QET;
        %{
        %if min(X(21:120,9))<QET
        %  ext=ext+1;
        %end
        %for i = 1:length(u)
        % qet1=qet1+u(i);
        %end
        %for i = 1:length(u)-1
        % qet2=qet2+u(i)*u(i+1);
        %end
        %}

        %for i = 1:length(u)-2
        % qet3=qet3+u(i)*u(i+1)*u(i+2);
        %end
	    for i = 1:length(u)-3
         qet4=qet4+u(i)*u(i+1)*u(i+2)*u(i+3);
        end

    %QET1=QET1+(qet1>0);
    %QET2=QET2+(qet2>0);
    %QET3=QET3+(qet3>0);
    QET4=QET4+(qet4>0);

    end%rep

  out1=[out1; a b QET4/reps];

end% randperm- samples of params
mins=toc/60

mean(out1(:,3))

% Here is the a single simulation with the actual timeseries superimposed.
% This is intends to demonstrate that the PVA centers abundance
% appropriately, captures stochasticisity and autocorrlation.  In short,
% the simulated timeseries should "look like" the obseved time series.

figure
subplot(2,2,1),plot(X(51:150,8),'.-b')
%ylim([0 4000])
hold on
plot(33:64,N{:,pop+1},'.-r')
%plot(41:66,xlsread('ChF_Revision',population,'B4:B29'),'.-r')%Elk River.
xlabel('Year','Fontsize',14)
ylabel('Spawner Abundance','Fontsize',14)
title(names(pop),'Fontsize',14)
legend('Simulated','Empirical')
mins =
       5.3119
ans =
      0.07971