Changeset 825

Show
Ignore:
Timestamp:
08/03/11 17:48:38 (22 months ago)
Author:
jjr8
Message:

Continued development of Linear Mixed Modeling tools.

Location:
MGET/Branches/Jason/PythonPackage/src/GeoEco/Statistics
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • MGET/Branches/Jason/PythonPackage/src/GeoEco/Statistics/FitLMEForDataframe.r

    r815 r825  
    1818# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
    1919 
    20 FitLMEForDataframe <- function(fixed, data, outputModelFile, random=NULL, correlation=NULL, method=NULL, xVar=NULL, yVar=NULL, zVar=NULL, mVar=NULL, coordinateSystem=NULL, writeSummaryFile=TRUE, plotFileFormat="png", res=1000.0, width=3000.0, height=3000.0, pointSize=10.0, bg="white") 
     20FitLMEForDataframe <- function(fixed, data, outputModelFile, random=NULL, correlation="NULL", method=NULL, xVar=NULL, yVar=NULL, zVar=NULL, mVar=NULL, coordinateSystem=NULL, writeSummaryFile=TRUE, plotFileFormat="png", res=1000.0, width=3000.0, height=3000.0, pointSize=10.0, bg="white") 
    2121{ 
    2222    # Fit the model. 
     
    3232                          "==============\n", 
    3333                          "\n", 
    34                           paste(capture.output(summary(model)), sep="", collapse="\n"), "\n", 
     34                          sub("Fixed effects: list\\(fixed\\)", paste("Fixed effects: ", as.character(fixed)[2], as.character(fixed)[1], as.character(fixed)[3]), paste(capture.output(summary(model)), sep="", collapse="\n")), "\n", 
    3535                          "\n", 
    3636                          "ANOVA:\n", 
     
    4343    # Write the output file. 
    4444     
    45     save(model, xVar, yVar, zVar, mVar, coordinateSystem, file=outputModelFile, compress=FALSE) 
     45    save(model, fixed, random, xVar, yVar, zVar, mVar, coordinateSystem, file=outputModelFile, compress=FALSE) 
    4646 
    4747    # Strip the extension, if one exists, off the output file path. 
  • MGET/Branches/Jason/PythonPackage/src/GeoEco/Statistics/Modeling.py

    r824 r825  
    417417                         where=None, method=u'REML', correlationStructure=None, correlationFormula=None, range_=None, nugget=None, metric=None, fixed=False, 
    418418                         xColumnName=None, yColumnName=None, zColumnName=None, mColumnName=None, 
    419                          writeSummaryFile=True, plotFileFormat=u'png', res=1000., width=3000., height=3000., pointSize=10.0, bg=u'white', 
     419                         writeSummaryFile=True, writeDiagnosticPlots=True, plotFileFormat=u'png', res=1000., width=3000., height=3000., pointSize=10.0, bg=u'white', 
    420420                         overwriteExisting=False): 
    421421        cls.__doc__.Obj.ValidateMethodInvocation() 
     422 
     423        # Perform additional validation. 
     424 
     425        if correlationStructure is not None and correlationFormula is None or correlationStructure is None and correlationFormula is not None: 
     426            Logger.RaiseException(ValueError(_('To fit a model that includes for within-group correlation, you must specify both the a correlation structure and a correlation formula. If you do not wish to include within-group correlation, you must omit both of those parameters.'))) 
     427 
     428        if correlationStructure is not None and range_ is None and nugget is not None: 
     429            Logger.RaiseException(ValueError(_('If the Nugget parameter is specified, the Range parameter must also be specified.'))) 
    422430 
    423431        # Load the table into a temporary data frame. 
     
    439447                randomFormula = u'NULL' 
    440448 
    441             r('FitLMEForDataframe(fixed=%s, data=%s, outputModelFile="%s", random=%s, correlation=NULL, method="%s", xVar=%s, yVar=%s, zVar=%s, mVar=%s, coordinateSystem=%s, writeSummaryFile=%s, plotFileFormat="%s", res=%f, width=%f, height=%f, pointSize=%f, bg="%s")' % 
    442               (fixedFormula, dataFrameName, tempOutputFile.replace('\\', '\\\\'), randomFormula, method, 
     449            if correlationStructure is not None: 
     450                if correlationStructure == u'exponential': 
     451                    correlation = u'corExp(' 
     452                elif correlationStructure == u'gaussian': 
     453                    correlation = u'corGaus(' 
     454                elif correlationStructure == u'linear': 
     455                    correlation = u'corLin(' 
     456                elif correlationStructure == u'rational quadratic': 
     457                    correlation = u'corRatio(' 
     458                elif correlationStructure == u'spherical': 
     459                    correlation = u'corSpher(' 
     460                else: 
     461                    Logger.RaiseException(RuntimeError(_('Unknown correlationStructure value \'%(cs)s\'. Please contact the author of this tool for assistance.') % {u'cs': correlationStructure})) 
     462 
     463                if range_ is not None: 
     464                    if nugget is None: 
     465                        correlation += repr(range_) + ', ' 
     466                    else: 
     467                        correlation += u'c(' + repr(range_) + u', ' + repr(nugget) + u'), ' 
     468 
     469                correlation += u'form=' + correlationFormula + u', nugget=' + repr(nugget is not None).upper() 
     470 
     471                if metric is not None: 
     472                    correlation += u', metric="' + metric + u'"' 
     473 
     474                correlation += u', fixed=' + repr(fixed).upper() + u')' 
     475 
     476            else: 
     477                correlation = u'NULL' 
     478 
     479            r('library(nlme)')      # Required here so we can instantiate correlation classes 
     480 
     481            r('FitLMEForDataframe(fixed=%s, data=%s, outputModelFile="%s", random=%s, correlation=%s, method="%s", xVar=%s, yVar=%s, zVar=%s, mVar=%s, coordinateSystem=%s, writeSummaryFile=%s, plotFileFormat="%s", res=%f, width=%f, height=%f, pointSize=%f, bg="%s")' % 
     482              (fixedFormula, dataFrameName, tempOutputFile.replace('\\', '\\\\'), randomFormula, correlation, method, 
    443483               xColumnParam, yColumnParam, zColumnParam, mColumnParam, coordinateSystemParam, 
    444484               str(writeSummaryFile).upper(), plotFileFormat, res, width, height, pointSize, bg)) 
     
    35843624 
    35853625AddArgumentMetadata(LinearMixedModel.FitToArcGISTable, u'correlationStructure', 
    3586     typeMetadata=UnicodeStringTypeMetadata(canBeNone=True, allowedValues=[u'Exponential', u'Gaussian', u'Linear', u'Rational quadratic', u'Spherical']), 
     3626    typeMetadata=UnicodeStringTypeMetadata(canBeNone=True, makeLowercase=True, allowedValues=[u'Exponential', u'Gaussian', u'Linear', u'Rational quadratic', u'Spherical']), 
    35873627    description=_( 
    35883628u"""TODO: Write documentation for this parameter."""),