| 1 | # FitLMEForDataframe.r
|
|---|
| 2 | #
|
|---|
| 3 | # Copyright (C) 2011 Jason J. Roberts
|
|---|
| 4 | #
|
|---|
| 5 | # This program is free software; you can redistribute it and/or
|
|---|
| 6 | # modify it under the terms of the GNU General Public License
|
|---|
| 7 | # as published by the Free Software Foundation; either version 2
|
|---|
| 8 | # of the License, or (at your option) any later version.
|
|---|
| 9 | #
|
|---|
| 10 | # This program is distributed in the hope that it will be useful,
|
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | # GNU General Public License (available in the file LICENSE.TXT)
|
|---|
| 14 | # for more details.
|
|---|
| 15 | #
|
|---|
| 16 | # You should have received a copy of the GNU General Public License
|
|---|
| 17 | # along with this program; if not, write to the Free Software
|
|---|
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|---|
| 19 |
|
|---|
| 20 | FitLMEForDataframe <- function(fixed, data, outputModelFile, random, correlation="NULL", method=NULL, xVar=NULL, yVar=NULL, zVar=NULL, mVar=NULL, coordinateSystem=NULL, writeSummaryFile=TRUE, writeDiagnosticPlots=TRUE, plotFileFormat="png", res=1000.0, width=3000.0, height=3000.0, pointSize=10.0, bg="white")
|
|---|
| 21 | {
|
|---|
| 22 | library(stats)
|
|---|
| 23 |
|
|---|
| 24 | # Fit the model.
|
|---|
| 25 |
|
|---|
| 26 | rPackage <- "nlme"
|
|---|
| 27 | library(rPackage, character.only=TRUE)
|
|---|
| 28 | message("Fitting the linear mixed model...")
|
|---|
| 29 | model <- lme(fixed, data=na.omit(data), random=random, correlation=correlation, method=method, keep.data=TRUE)
|
|---|
| 30 |
|
|---|
| 31 | # Print a summary of the model.
|
|---|
| 32 |
|
|---|
| 33 | modelSummary <- paste("\n",
|
|---|
| 34 | "MODEL SUMMARY:\n",
|
|---|
| 35 | "==============\n",
|
|---|
| 36 | SummarizeModel(model), "\n",
|
|---|
| 37 | sep="")
|
|---|
| 38 |
|
|---|
| 39 | writeLines(modelSummary)
|
|---|
| 40 | message("")
|
|---|
| 41 |
|
|---|
| 42 | # Write the output file.
|
|---|
| 43 |
|
|---|
| 44 | save(model, rPackage, fixed, random, xVar, yVar, zVar, mVar, coordinateSystem, file=outputModelFile, compress=FALSE)
|
|---|
| 45 |
|
|---|
| 46 | # Strip the extension, if one exists, off the output file path.
|
|---|
| 47 |
|
|---|
| 48 | outputFilePrefix <- file.path(dirname(outputModelFile), strsplit(basename(outputModelFile), ".", fixed=TRUE)[[1]])[1]
|
|---|
| 49 |
|
|---|
| 50 | # Write the summary file, if requested.
|
|---|
| 51 |
|
|---|
| 52 | if (writeSummaryFile)
|
|---|
| 53 | {
|
|---|
| 54 | f <- file(sprintf("%s_summary.txt", outputFilePrefix), "wt")
|
|---|
| 55 | tryCatch(writeLines(modelSummary, f), finally=close(f))
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | # Do all of the plotting in a tryCatch block, so that we can turn
|
|---|
| 59 | # off open graphics devices in the event of an error.
|
|---|
| 60 |
|
|---|
| 61 | tryCatch(
|
|---|
| 62 | {
|
|---|
| 63 | if (writeDiagnosticPlots)
|
|---|
| 64 | {
|
|---|
| 65 | # Plot the standardized residuals vs. fitted values.
|
|---|
| 66 |
|
|---|
| 67 | ## if (plotFileFormat == "emf")
|
|---|
| 68 | ## win.metafile(sprintf("%s_residuals_vs_fitted.emf", outputFilePrefix), width=width, height=height, pointsize=pointSize)
|
|---|
| 69 | ## else
|
|---|
| 70 | ## png(sprintf("%s_residuals_vs_fitted.png", outputFilePrefix), res=res, width=width, height=height, pointsize=pointSize, bg=bg)
|
|---|
| 71 |
|
|---|
| 72 | ## model <<- model
|
|---|
| 73 | ## plot.lme(model, resid(., type = "p") ~ fitted(.))
|
|---|
| 74 |
|
|---|
| 75 | ## dev.off()
|
|---|
| 76 | }
|
|---|
| 77 | }, finally = {
|
|---|
| 78 | if (dev.cur() != 1)
|
|---|
| 79 | dev.off()
|
|---|
| 80 | })
|
|---|
| 81 | }
|
|---|