0

I am trying to use the minpack.lm package in R. Specifically the NLS.LM function. I'm pouring through the manual and help files, but the requirements for setting it up are a little beyond my current capabilities. Any guidance is greatly appreciated. Here is my code, and the error statement I'm getting, below.

R Code:

# Thomas P. Taggart
# ERE445/645
# Spring 2013 - Calibration Presentation

# Lumped parameter rainfall-runoff model for the Susquehanna River at Conklin, NY. 
# Outlined in Haith's (1987) GWLF model. The model uses the SCS curve 
# number runoff technique to determine runoff, with snowpack, unsaturated zone, and 
# saturated zone mass balances. Evapotranspiration is to be determined using Hamon’s 
# method with average monthly values for daylight hours. 
# In this model we assume the following constants, which are determined through calibration:
# Baseflow Recession Coefficient, Kb
# Field capacity, FCAP
# Curve number for average moisture conditions, CN2 
# Initial antecedent moisture conditions, iAMC
# Initial snow accumulation, iSNt
# Initial saturated zone storage, iSATt
# No deep groundwater seepage

# including needed functions
source("Functions.R")
source("distributionFunctions.R")
source("GWLF_Model.R")

require(ggplot2)
require(reshape)
library(minpack.lm)
library(scales)  


###############################################################################################
# USGS Discharge data for Conklin, NY - Gage on the Susquehanna

# Reading in the input file
dischargeInput <- read.csv("USGS_DailyDischarge_ConklinNY_01503000_A.csv", header=TRUE)


###############################################################################################
# Weather Data

# Read in input file
weatherInput = read.csv("Conklin_NY_WeatherData_Edit.csv")


###############################################################################################
# Setting up the model inputs - inital Run

# Baseflow Recession, Kb 
Kb <- 0.90

# Initial unsaturated storage is at field capacity, FCAP (cm)
FCAP <- 10

# Curve number for average moisture conditions, CN 
CN <- 65.7

# Initial antecedent moisture conditions, AMC 
AMC <- 1.5

# Initial saturated zone storage, SATt 
iSATt <- 0.45

# Snowmelt constant, K
K <- 0.45

parameters <- c(Kb, FCAP,CN, AMC, iSATt, K)

# Calling the Model - 1st time to see the initial outputs
# GWLF(parameters, dischargeInput, weatherInput)


###############################################################################################
# Calibrating the model

guess <- c("Kb"=0.1, "FCAP"=1,"CN"=50, "AMC"=0, "iSATt"=0, "K"=0.5)

out <- nls.lm(par = guess, fn = GWLF(parameters, dischargeInput, weatherInput))

Here is the error message:

Error in function (par)  : could not find function "fn"

How do I need to setup par? Or the 1st argument in the function i'm calling within nls.lm? The GWLf function is being passed 6 parameters that are used as constants in the function. These are the 6 parameters i hope to calibrate.

Thanks, Tom

CHP
  • 16,981
  • 4
  • 38
  • 57
traggatmot
  • 1,423
  • 5
  • 26
  • 51

1 Answers1

3

From reading ?nls.lm

You need to pass the function, not a call to the function

out <- nls.lm(par = guess, fn = GWLF, dischargeInput, weatherInput)

Note the extra arguments (which I assume are the data) are passed within ...

It would be safer to name these arguments using whatever argument names you wish these to be within GWLF.

mnel
  • 113,303
  • 27
  • 265
  • 254
  • MNEL, Wow!! I got it working only a couple of minutes ago, and that's exactly how i got it working. Exactly. I am know faced with the problem of wondering why my iterations stops after the second (even thought the number of graphs made indicates it should be higher then two iterations), and why the resulting par output isn't any different AT ALL than the initial guess values. – traggatmot Apr 12 '13 at 06:01
  • @traggatmot, that will be hard to assist with unless you can provide a reproducible example – mnel Apr 12 '13 at 06:12