Home > chronux_1_0 > old > locfit_old > rscb.m

rscb

PURPOSE ^

RSCB Runs locfit scb using R on a passed row array using the given parameters

SYNOPSIS ^

function [smooth,values,upper,lower]=rscb(in,w,nn,pts,lo,hi,pl)

DESCRIPTION ^

RSCB Runs locfit scb using R on a passed row array using the given parameters
 
 RSCB( in, w, nn, pts, lo, hi )
 
 Requires windows since R-(D)COM is windows-specific
  I am working on a platform-independent replacement

 Requires that Matlab-R link Matlab package be installed from
 http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=5051&objectType=file   
 file MATLAB_RLINK.zip

 Requires that R be installed see http://r-project.org first
 file rw1091.exe

 Requires that R locfit package be installed first
 From within R in menu do "Packages" then "Install from CRAN"

 Requires that R-(D)COM be installed first from  
 http://lib.stat.cmu.edu/R/CRAN/contrib/extra/dcom/
 (get latest EXE file approx 3 MB)
 file RSrv135.exe

 The above packages should come bundled with this software for convenience
 with the exception of locfit which is easiest to install from within R



 In values:
 
 in: the input row array to smooth, contains a set of values 
 w: the smoothing width
 nn: nearest neighbor fraction for smoothing (0.1 is good)
 pts: number of evaluation points
 lo: lowest value to consider
 hi: highest value to consider
 pl: optional argument - make a plot if present
 
 Out Values:

 smooth: a smoothed histogram of in, evaluated at the values points below
 values: the evaluation point values
 upper: the upper 95% confidence intervals for each point
 lower: the upper 95% confidence intervals for each point

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [smooth,values,upper,lower]=rscb(in,w,nn,pts,lo,hi,pl)
0002 %RSCB Runs locfit scb using R on a passed row array using the given parameters
0003 %
0004 % RSCB( in, w, nn, pts, lo, hi )
0005 %
0006 % Requires windows since R-(D)COM is windows-specific
0007 %  I am working on a platform-independent replacement
0008 %
0009 % Requires that Matlab-R link Matlab package be installed from
0010 % http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=5051&objectType=file
0011 % file MATLAB_RLINK.zip
0012 %
0013 % Requires that R be installed see http://r-project.org first
0014 % file rw1091.exe
0015 %
0016 % Requires that R locfit package be installed first
0017 % From within R in menu do "Packages" then "Install from CRAN"
0018 %
0019 % Requires that R-(D)COM be installed first from
0020 % http://lib.stat.cmu.edu/R/CRAN/contrib/extra/dcom/
0021 % (get latest EXE file approx 3 MB)
0022 % file RSrv135.exe
0023 %
0024 % The above packages should come bundled with this software for convenience
0025 % with the exception of locfit which is easiest to install from within R
0026 %
0027 %
0028 %
0029 % In values:
0030 %
0031 % in: the input row array to smooth, contains a set of values
0032 % w: the smoothing width
0033 % nn: nearest neighbor fraction for smoothing (0.1 is good)
0034 % pts: number of evaluation points
0035 % lo: lowest value to consider
0036 % hi: highest value to consider
0037 % pl: optional argument - make a plot if present
0038 %
0039 % Out Values:
0040 %
0041 % smooth: a smoothed histogram of in, evaluated at the values points below
0042 % values: the evaluation point values
0043 % upper: the upper 95% confidence intervals for each point
0044 % lower: the upper 95% confidence intervals for each point
0045 %
0046 
0047 % Minimal input validation
0048 if nargin < 6
0049    error( 'Not enough input arguments passed' );
0050 end
0051 
0052 
0053 %
0054 % Connect to R only if not done so already, never disconnect
0055 global RCONNECTED;
0056 if isempty( RCONNECTED )
0057   % Try the open command
0058   [status,msg] = openR;
0059   if status ~= 1
0060     disp(['Problem connecting to R: ' msg]);
0061   end
0062   evalR('library("locfit")') % attach locfit library
0063   RCONNECTED = 1;
0064 end
0065 
0066 % Put fitting parameters into R
0067 putRdata( 'width', w' );
0068 putRdata( 'nn', nn' );
0069 putRdata( 'evalpts', pts );        
0070 evalR( sprintf( 'flim=c(%f,%f)', lo, hi ) );
0071 % bandwidth: variable and constant terms.  0.75*width/2 is how density.lf does it for a gaussian kernel
0072 evalR( 'alpha=c(nn,0.75*width/2)' ); 
0073 
0074 % send the data to R, transposed
0075 putRdata('data',in');
0076 
0077 
0078 % set up for locfit.raw
0079 evalR( 'data <- sort( data )' );
0080 evalR( 'r <- range( data )' );        
0081 %evalR( 'flim=c(r[1.]-width*0.75,r[2]+width*0.75) ); % alternative, data-derived limits - not based upon 'high' above
0082 
0083 % run locfit.raw just like density.lf() and scb does
0084 evalR( 'fit <- scb( data, ev="grid", mg=evalpts, flim=flim, alpha=alpha, kern="gauss", deg=0, link="ident", family="density", type=0 )' );
0085 % pull evaluation points into matlab
0086 evalR( 'values = fit$xev' );
0087 values = getRdata( 'values' );
0088 
0089 % pull smoothed data into matlab
0090 evalR( 'smooth = fit$trans(fit$coef)' );
0091 smooth = getRdata( 'smooth' );
0092 
0093 % pull lower limits into matlab
0094 evalR( 'lower = fit$trans(fit$lower)' );
0095 lower = getRdata( 'lower' );
0096 
0097 % pull upper limits into matlab
0098 evalR( 'upper = fit$trans(fit$upper)' );
0099 upper = getRdata( 'upper' );
0100     
0101 if nargin > 6
0102     % plot up results in matlab
0103     clf % clear figure to prevent overplotting!
0104     plot( values, smooth, 'k-', values, [lower;upper], 'r:' );
0105     title( sprintf( 'Smoothed Results' ) );
0106     xlabel( 'time (ms)' );
0107     ylabel( 'Proportion' );
0108 end

Generated on Fri 09-Jun-2006 23:38:05 by m2html © 2003