title: How to get optimum weights that would result btter correlation between Runoff and API in R?
body: <p>I would like to get the best possible correlation between Runoff and API (Anteceedent Precipitation Index) by fitting weights to my before freeze up precipitation months (July-October) within defined range. The sum of the fitted weights of any combination for all the months must never exceed 1.</p>
  
  <p>Here is my partially done code (Sorry I don't know how to proceed further). I am trying to use 100 different combination of weights for each month within a defined range for before freeze up precipitation. </p>
  
  <pre><code>weights=data.frame(July=runif(100, min = 0.01, max = 0.1), August=runif(100, min = 0.11, max = 0.25),
                         Sept=runif(100, min = 0.26, max = 0.40), Oct=runif(100, min = 0.41, max = 0.6))
  
  # Precipitation before freezup
  July_rain=runif(31*20, min = 45, max = 55)
  Aug_rain=runif(31*20, min = 35, max = 45)
  Sep_rain=runif(30*20, min = 30, max = 38)
  Oct_rain=runif(31*20, min = 28, max = 32)
  
  # Winter Precipitation 
  Dec_rain=runif(31*20, min = 20, max = 25)
  Jan_rain=runif(31*20, min = 20, max = 25)
  Feb_rain=runif(31*20, min = 20, max = 25)
  Mar_rain=runif(31*20, min = 35, max = 40)
  
  # Runoff
  Runoff=runif(30*20, min = 10, max = 400)
  
  # placing data into multiple Dataframe
  max.len = max(length(July_rain), length(Aug_rain), length(Sep_rain), length(Oct_rain), length(Dec_rain), length(Jan_rain), length(Feb_rain), length(Mar_rain), length(Runoff))
  July_rain = c(July_rain, rep(NA, max.len - length(July_rain)))
  Aug_rain = c(Aug_rain, rep(NA, max.len - length(Aug_rain))) 
  Sep_rain = c(Sep_rain, rep(NA, max.len - length(Sep_rain)))
  Oct_rain = c(Oct_rain, rep(NA, max.len - length(Oct_rain))) 
  Dec_rain = c(Dec_rain, rep(NA, max.len - length(Dec_rain)))
  Jan_rain = c(Jan_rain, rep(NA, max.len - length(Jan_rain))) 
  Feb_rain = c(Feb_rain, rep(NA, max.len - length(Feb_rain)))
  Mar_rain = c(Mar_rain, rep(NA, max.len - length(Mar_rain)))
  Runoff=c(Runoff, rep(NA, max.len - length(Runoff)))
  
  DF_1=data.frame(July_rain,Aug_rain, Sep_rain, Oct_rain) # before freezeup
  DF_2=data.frame(Dec_rain, Jan_rain, Feb_rain, Mar_rain) # winter precipitation
  </code></pre>
  
  <p>Here is the equation that I am trying to fit in</p>
  
  <pre><code>API_beforeFreezup (or DF_1) = 0.5*(0.01 - 0.10)*July_rain + (0.11 - 0.25)*Aug_rain + (0.26 - 0.40)*Sep_rain + (0.41 - 0.60)*Oct_rain 
  API_Winter= 0.5*(Sum of winter precipitation) 
  API= API_beforeFreezup + API_Winter
  Runoff = API # I should get maximum correlation between API and Runoff.
  </code></pre>