2. El vínculo analógico y la ciudadanía no afectada
2.4. Ejes de la categoría de la acción común: discursos de la ciudadanía no afectada sobre
2.4.3. Eje 3: papel de la ciudadanía no afectada en los procesos de rememoración en el
In this appendix we show Matlab functions Nglmf it and Sglmf it , coded by the au thor, for fitting a generalized linear model with canonical link using the new algorithm and the scoring method respectively.
function [b , f it , iter , flop] =Nglmf it (x , y , distr)
Nglmf it fits a generalized linear model with canonical link using the new algorithm.
[b , Fit , iter , f lop] =Nglmf it (x , y , distr) fits a generalized linear model using the design matrix x, response y, and distribution distr. The result b is a vector of coefficient estimates. The result f it is a vector of fitted value of the response y. The result iter is the number of iterations needed for convergence. The result f lop is the approximate number of floating point operations when the algorithm converges. Acceptable values for distr are ' normal ' ,
' binomial ' , ' poisson ' , ' gamma ' , and ' inverse gaussian ' . The distribution parameter is fit as a function of the x columns using the canonical link.
Example:
b = Nglmf it (x , [y N] , ' binomial ' )
This example fits a logistic regression model for y on x. Each y (i) is the number of successes in N ( i) trials.
f lops ( O ) ;
if (nargin< 2 ) , error ( ' At least two arguments are required ' ) ; end if (nargin<3 I isempt y ( distr) ) , distr = ' normal ' ; end
xx= [ones ( size (x , 1 ) , 1) x] ; [n p] =size (xx) ;
%Drthonomaliz the des ign matrix x A=orth( [xx eye (n) ] ) ; x=A ( : , 1 : p) ; convcrit = 1 e - 6 ; b = zeros ( p , 1 ) ; bO = b+1 ; iter = 0 ; iterlim = 200 ; switch (distr) case ' normal ' yO y ; eta yO ; case ' binomial '
if ( size (y , 2) =2) , error ( ' Y must have two c olumns . ' ) ; end N y ( : , 2 ) ;
y = y ( : , 1 ) . / N ;
yO = (N . *y + 0 . 5) . / (N + 1 ) ; eta = log (y0 . / ( 1-yO) ) ; case ' poisson '
yO = y + 0 . 25 ; eta = log (yO ) ;
case ' gamma '
i f ( any (y ( : ) <=O) )
delta = min (abs ( y (y =0) ) ) * . 00 1 ; yO = max(delt a , y ) ;
else yO=y ; end
eta = 1 . /yO ; case ' inverse gauss ian '
if ( any (y ( : ) <=O) )
delta = min (abs (y ( y =0) ) ) * . 00 1 ; yO = max (delta, y) ;
else yO=y ; end
eta = 1 . / (y0 . A2) ;
otherwis e , error ( ' Distribut ion name is invalid . ' ) ; end
while ( 1 )
iter = i t er+ 1 ;
% Compute parameter by using inverse link function switch (distr)
cas e ' normal ' t = eta ;
mu= (t+ (x*x ' ) * (y-t) ) ; case ' binomial '
end
p = 1 . / ( 1 + exp ( - eta) ) ; t= ( p . *N+(x*x ' ) * (y . *N-p . *N ) ) ; mu=max ( eps ,min ( 1- eps , t . /N) ) ; case ' poiss on '
t = exp (eta) ;
mu= (t+ ( x*x ' ) * (y-t ) ) ; mu = max (eps ,mu) ; case ' gamma '
t = 1 . /eta ;
mu= (t+ ( x*x ' ) * (y-t ) ) ; mu = max (eps ,mu) ; case ' inverse gaussian '
t = 1 . / ( sqrt (eta) ) ; mu= (t+ (x*x ' ) * (y-t) ) ; mu = max (eps ,mu) ;
% Compute adj usted dependent variable f o r l e ast squares f it switch ( distr)
case ' normal ' z = mu ; case ' binomial '
z=log (mu . / ( 1-mu) ) ; case ' po is s on '
z = l og (mu) ; case ' gamma '
end
case ' invers e gaussian ' z = 1 . / (mu . A2) ; end
% Check stopping c onditions
if (norm (b-bO) < convcrit) , break ; end
if ( i ter>iterlim) , warning ( ' Iteration limit reached . ' ) ; break ; end % Compute weight f unction as the inverse of the variance function switch ( distr) end case ' normal ' w =ones ( s ize (y , 1) , 1 ) ; case ' binomial ' w=N . * (mu · * ( 1 -mu) ) ; case ' poisson ' w =mu ; case ' gamma ' w =mu . /\2 ;
case ' inverse gaussian ' w =mu . /\3 ;
% Compute coef f ic i ent estimates for this iteration bO = b ;
[b , R] = wf it (z , x , w) ;
% Form current linear combination eta = x * b ;
% Calculate the f itted value switch (distr)
case 'normal ' f it = X*b ; case ' binomial ' fit = N . / ( 1 + exp ( -x*b) ) ; c ase ' poisson ' f it = exp (x*b) ; case ' gamma ' f it = 1 . /x*b ; case ' inverse gaussian '
f it = 1 . / sqrt ( x*b) ; end
% Compute coeff i c i ent estimate s based on the unorthonomalized design matrix xx b=wfit (z , xx , w) ;
f l op=flops ;
% Perform a weighted least squares f it function [b , R] =wfit (y , x , w) sw = s qrt (w) ; [ r c] = size (x) ; yw y · * sw ; xw = x · * sw ( : , ones ( 1 , c) ) ; [Q , R] =qr (xw , O) ; b = R\(Q ' *yw) ;
funct ion [b , f it , iter , f lop] =Sglmfit (x , y , distr)
Sglmf it fits a generalized linear model with canonical link using the scoring method.
[b , Fit , iter , flop] =Sglmfit (x , y , distr) fits a generalized linear model using the design matrix x, response y, and distribution distr. The result b is a vector of coefficient estimates. The result f it is a vector of fitted value of the response y. The result iter is the number of iterations needed for convergence. The result flop is the approximate number of floating point operations when the algorithm converges. Acceptable values for distr are ' normal ' ,
' binomial ' , ' poisson ' , ' gamma ' , and ' inverse gauss i an 1 • The distribution parameter is
fit as a function of the x columns using the canonical link.
Example:
b = Sglmfit (x , [y N] , ' binomial ' )
This example fits a logistic regression model for y on x. Each y (i ) is the number of successes in N ( i ) trials.
f lops ( O ) ;
if (nargin<2) , error ( ' At least two arguments are requ ired ' ) ; end if (nargin<3 I isempt y ( distr) ) , distr = ' normal ' ; end
xx= [one s ( size (x , 1 ) , 1) x] ; [n p] =size ( xx) ;
% Orthonormalize the design matrix x A=orth( [xx eye (n) ] ) ; x=A ( : , 1 : p) ; convcrit = 1 e-6 ; b = zero s (p , 1 ) ; bO = b+1 ; iter = 0 ; iterlim = 200 ; switch (distr)
case ' normal ' yO = y ; eta = yO ; case ' binomial '
if ( size (y , 2) =2) , error ( ' Y must have two c olumns . ' ) ; end N y ( : , 2) ;
y y ( : , 1 ) . / N ;
y O = (N . *y + 0 . 5) . / ( N + 1 ) ; eta = log (y0 . / ( 1 -yO ) ) ; case ' poisson '
yO = y + 0 . 25 ; eta = log (yO) ; case ' gamma '
if ( any (y ( : ) <=O) )
delta = min (abs ( y ( y =0) ) ) * . 00 1 ; yO = max (delt a , y) ;
else yO=y ; end
eta = 1 . /yO ; case ' inverse gaussian '
if ( any ( y ( : ) <=O) )
delta = min (abs ( y ( y =0) ) ) * . 00 1 ; yO = max ( delt a , y) ;
else yO=y ;
end
eta = 1 . / (y0 . A2 ) ;
otherwise , error ( ' Distribut ion name is invalid . ' ) ; end
while ( 1 )
iter = iter+1 ;
% Compute parameter by using inverse link f uncti on switch(distr) end case ' normal ' mu=eta ; case ' binomial ' mu 1 . / ( 1 + exp ( -eta) ) ; mu max (eps , min( 1-eps , mu) ) ; case ' poisson ' mu exp (eta) ; mu max (O , mu) ; case ' gamma ' mu 1 . /et a ; mu max (O , mu) ; case ' inverse gaussian '
mu = 1 . /sqrt ( eta) ; mu = max ( O , mu) ;
% Compute adjusted dependent variable f or l east squares f it switch ( distr)
case ' normal ' z = eta ; case ' binomial '
z = eta + (y - mu) . / ( mu · * ( 1-mu) ) ; case ' poisson '
z = eta + ( y - mu) . /mu ; case ' gamma '
z = eta - ( y - mu) . / (mu . A2 ) ; case ' inverse gaussian '
z = eta - 2 * ( (y - mu) . / (mu . A3) ) ; end
% Check stopping conditions
if (norm (b-bO) < convcrit) , break ; end
if ( iter>iterlim) , warning ( ' Iterati on limit reached . ' ) ; break ; end % Compute weight fun ction as the inverse of the variance funct ion switch( di str) case ' normal ' w =ones ( s i ze (y , 1) , 1 ) ; case ' binomial ' w=N . * (mu · * ( 1 -mu) ) ; case ' poisson' w =mu ; case ' gamma ' w =mu . A2 ;
end
w =mu . !\3 ; end
% Compute coef f ic ient estimates for this iteration bO = b ;
[b , R] = wf it (z , x , w) ;
% Form current linear combination eta = x * b ;
% Calculate the f itted value switch (distr) case ' normal ' f it = X*b ; case ' binomial ' f it = N . / ( 1 + exp (-x*b) ) ; case ' po i s son ' f it = exp (x*b) ; case ' gamma ' f it = 1 . /X*b ; case ' inverse gaussian '
f it = 1 . /sqrt (x*b) ; end
% Compute coefficient estimates based on the unorthonomalized design matrix xx b=wf it (z , xx , w) ;
% Perform a weighted least squares f it function [b , R] =wfit ( y , x , w) sw = sqrt (w) ; [r c] = s ize (x) ; yw = y · * sw ; xw = x · * sw ( : , ones ( 1 , c) ) ; [Q , R] =qr ( xw , O) ; b = R\ ( Q ' *yw) ;