//WILDTRACK LOWLAND TAPIR AUTOSCRIPT 22-10-14 // Authors: Sky Alibhai and Zoe Jewell // ROBUST CROSS VALIDATION DISCRIMINANT ANALYSIS // Utility functions // // create distance matrix for clustering _fitdir=getdefaultdirectory(); distance matrix = Function( {dist, nmlst}, {dt, lvl = N Items( nmlst ), tmp = J( lvl, lvl, . ), x = 1 :: lvl ^ 2, i = 1, mid = Index( 1, lvl ^ 2, lvl + 1 )}, tmp[Loc( (1 :: lvl ^ 2) > Shape( Repeat( mid, lvl )`, 1, lvl ^ 2 ) )] = dist; tmp[mid] = 0; dt = As Table( tmp, <sil[i] | abs(sil[i-1]-sil[i])<0.00005)); // << 0.001 seems to be a good threshold x[1 :: N Row( sil ) - 1] = Expr( i++; If( i == 1, 0, (sil[i - 1] > sil[i] & sil[i - 1] > sil[i + 1]) | (sil[i - 1] > sil[i] & Abs( sil[i - 1] - sil[i + 1] ) < 0.01) | Abs( sil[i - 1] - sil[i] ) < 0.00005 ); ); i = 0; x = Loc( Matrix( Eval List( x ) ) ); Show( x ); If( N Row( x ), x[1]/*if(x[1]>1,x[1]-1,x[1])*/, N Row( dt ) - 1 ); ); // progress bar add ProgressBar = Function( {sz}, {bx}; bx = If Box( 1, Graph Box( framesize( sz, 15 ), Y Scale( 0, 0.1 ), X Scale( 1, 11 ), Pen Color( "red" ), Pen Size( 20 ); jmwait( 0 ); If( !Mod(_k, _modval ), window:_progbar_count += 8.5 / 20 ); H Line( 1, window:_progbar_count, 0.05 ); jmwait( 0 ); ) ); bx[axisbox( 1 )] << delete; bx[axisbox( 1 )] << delete; bx[List Box( 1 )] << append( Button Box( "", < m[2] | m[1] < lmt[1] | m[2] > lmt[2] | m[3] <= 0 | m[3] > m[2], 0, 1 ) ); // track analysis track anal2 = Function( {p, categ_x, ex, tmp}, {/*categ_x=arg(arg(ex))*/i,nc2,x1,x2,y, categ_y = Arg( ex, 2 )}, //********Joseph ammend 10 jan 14 Y = Eval( Substitute( Expr( As Column( dt, _x ) << get values ), Expr( _x ), Name Expr( categ_x ) ) ); //nc2=enumnc2(nitems(tmp)); nc2 = [1 2]; holdprs = {}; ki = 1; ncPer = ChiSquare Distribution( ChiSquare Quantile( p, 2 ), 2 ); dt << select excluded << exclude; jmwait( 0 ); dtex = Substitute( Expr( dt << select where( !(As Column( dt, _x ) == tmp[nc2[i, 1]] | As Column( dt, _x ) == tmp[nc2[i, 2]] | As Column( dt, _x ) == "RCV") ) ), Expr( _x ), Name Expr( categ_x ) ); objex = Substitute( Expr( dt << Discriminant( X( _x ), _y, invisible ) ), Expr( _x ), Name Expr( categ_x ), Expr( _y ), Name Expr( categ_y ) ); For( i = 1, i <= N Rows( nc2 ), i++, // select rows dt << Clear Select; Eval( dtex ); dt << exclude; // launch discriminant platform, save canon scores,create overlay plot for normal contours If( _display, nw = New Window( "Custom Analysis - Wildtrack", Lineup Box( N Col( 2 ), obj = Eval( objex ), Outline Box( "Normal Contours", Text Box( "" ), obj << Save Canonical Scores; obj2 = Overlay Plot( X( name( "Canon[1]" ) ), Y( name( "Canon[2]" ) ) );, Outline Box( "Empty", <" ) ); _lst1 = find overlapNormContour( ncPer, meanMat, stdMat ); If( _debug, Write( ", prob=", p, "\!t# olap=", _lst1[1][1] ) ); //holdprs[ki++]=evallist(list(tmp[nc2[i,0]],ncper,_lst1,nitems(_lst1),_slf)); holdprs[ki++] = Eval List( List( tmp[nc2[i, 0]], ncper, _lst1, _lst1[1][1], _slf ) ); // cleanup If( _display, jmwait( 30 ); nw << close window; jmwait( 0 ); ); dt << exclude; //dt< ]; nonself = [=> ]; animal = [=> ]; _debug = 0; p_strt = 0.5; p_stop = 0.5; p_incr = 0.1; v_strt = 18; v_stop = 18; v_incr = 1; totgrid = 1; _display = 0; _restart = 0; Try( If( Length( Trim( Load Text File( wild_home || "restart.jsl" ) ) ), _restart = 1; Include( wild_home || "restart.jsl" ); ) ); jmwait( 0 ); tmpltstr = "//OutlineBox[text()='Contour probability = ^lst2[1][i]^']//TextBox[text()='Variables = ^lst2[2][i]^']"; //ammended 16 oct 12 - sky image1 = new image( _fitdir || "tapir a.jpg" ); image2 = new image( _fitdir || "tapir b.jpg" ); image3 = try(new image( _fitdir || "wildtrack 150.jpg" ),newimage([])); //ammended 23 oct 12 - sky _nw = New Window( "FIT - Lowland Tapir Data Analysis: Data File = " || (dt << get name), //ammended 23 oct 12 - sky < ] ); If( cbx << get & N Items( lbx << get selected ) == 1, Dialog( title( "Alert" ), "You must select at least 2 category levels" ), // ammended 10/13/2012 - SKY !(N Items( cdlgorig_y << GetItems ) & N Items( cdlgorig_x1 << GetItems ) & N Items( cdlgorig_x2 << GetItems )), Dialog( title( "Alert" ), "You must select columns for all roles" ), !(valid( (pbx1 << get) || (pbx2 << get) || (pbx3 << get) ) & valid( (vbx1 << get) || (vbx2 << get) || (vbx3 << get), 1 || N Items( cdlgorig_y << GetItems ) )), Dialog( title( "Alert" ), "Either Contour probability (0-1) or # of variables invalid" ), //ammended 16 oct 12 - sky _tmsec = Tick Seconds(); //anal=if(_restart,_restart_anal,{})||modelMain(cdlgorig_x1<_mypred, 1-tDistribution( val/(stddev(d)/sqrt(nrow(d))),nrow(d)-2 )+tDistribution( -1*val/(stddev(d)/sqrt(nrow(d))),nrow(d)-2 ), tDistribution( val/(stddev(d)/sqrt(nrow(d))),nrow(d)-2 )+1-tDistribution( -1*val/(stddev(d)/sqrt(nrow(d))),nrow(d)-2 ) ); char(round(x,3)*100) ); db = Text Box( Char( Round( _dist[_clusternum], 3 ) ) ), tb = Text Box( Char( Floor( _clusternum ) ) || " Tapirs Predicted",<