Re: [R] Multivariate function from univariate functions

2012-03-26 Thread physicistintheory
Petr, thanks!  I've now got a function I can work with.  Regards!

--
View this message in context: 
http://r.789695.n4.nabble.com/Multivariate-function-from-univariate-functions-tp4502670p4504599.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Multivariate function from univariate functions

2012-03-25 Thread physicistintheory
I'm relatively new to R and I'm stuck.

I'm trying to construct a surface to optimize from a multivariate dataset. 
The dataset contains the response of a system to various stimuli.  I am
trying to optimize the mix of stimuli to maximize the response.  To do so,
I've interpolated the various datasets of type response vs. stimuli and I
now have an array of functions interps whose length is the length of the
array of the names of the various stimuli.  I've also created a vector
containing names for the stimuli, vars = x.1, x.2, x.3...

Anyway, each of the functions in interps depends only on one variable
(obviously).  I would like to construct a function, call it, surface which
is essentially: surface(vars) = interps[[1]]vars[[1]] +
interps[[2]]vars[[2]]+...

I've tried constructing surface recursively in a for loop:  
surface - function(vars){0}
for(i in 1:length(vars)){
surface - function(vars){surface(vars) + interps[[i]](vars[[i]])}
}

This results in an infinite recursion error...which I think I understand. 
Essentially, I'm trying to do the analog of something like i = i+1, but
instead of adding integers, I want to add terms to a function.

Any help is appreciated.

--
View this message in context: 
http://r.789695.n4.nabble.com/Multivariate-function-from-univariate-functions-tp4502670p4502670.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Multivariate function from univariate functions

2012-03-25 Thread Mitchell Maltenfort
Naïve question: would a saturated multivariate model work as an
interpolation function?

On 3/24/12, physicistintheory physicistinthe...@gmail.com wrote:
 I'm relatively new to R and I'm stuck.

 I'm trying to construct a surface to optimize from a multivariate dataset.
 The dataset contains the response of a system to various stimuli.  I am
 trying to optimize the mix of stimuli to maximize the response.  To do so,
 I've interpolated the various datasets of type response vs. stimuli and I
 now have an array of functions interps whose length is the length of the
 array of the names of the various stimuli.  I've also created a vector
 containing names for the stimuli, vars = x.1, x.2, x.3...

 Anyway, each of the functions in interps depends only on one variable
 (obviously).  I would like to construct a function, call it, surface which
 is essentially: surface(vars) = interps[[1]]vars[[1]] +
 interps[[2]]vars[[2]]+...

 I've tried constructing surface recursively in a for loop:
 surface - function(vars){0}
 for(i in 1:length(vars)){
 surface - function(vars){surface(vars) + interps[[i]](vars[[i]])}
 }

 This results in an infinite recursion error...which I think I understand.
 Essentially, I'm trying to do the analog of something like i = i+1, but
 instead of adding integers, I want to add terms to a function.

 Any help is appreciated.

 --
 View this message in context:
 http://r.789695.n4.nabble.com/Multivariate-function-from-univariate-functions-tp4502670p4502670.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


-- 
Sent from my mobile device


Ersatzistician and Chutzpahthologist
I can answer any question.  I don't know is an answer. I don't know yet
is a better answer.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Multivariate function from univariate functions

2012-03-25 Thread Petr Savicky
On Sat, Mar 24, 2012 at 08:04:49PM -0700, physicistintheory wrote:
 I'm relatively new to R and I'm stuck.
 
 I'm trying to construct a surface to optimize from a multivariate dataset. 
 The dataset contains the response of a system to various stimuli.  I am
 trying to optimize the mix of stimuli to maximize the response.  To do so,
 I've interpolated the various datasets of type response vs. stimuli and I
 now have an array of functions interps whose length is the length of the
 array of the names of the various stimuli.  I've also created a vector
 containing names for the stimuli, vars = x.1, x.2, x.3...
 
 Anyway, each of the functions in interps depends only on one variable
 (obviously).  I would like to construct a function, call it, surface which
 is essentially: surface(vars) = interps[[1]]vars[[1]] +
 interps[[2]]vars[[2]]+...
 
 I've tried constructing surface recursively in a for loop:  
 surface - function(vars){0}
 for(i in 1:length(vars)){
 surface - function(vars){surface(vars) + interps[[i]](vars[[i]])}
 }

Hi.

Is the following close to what you are asking for?

  f1 - function(x) 3*x^2 + x
  f2 - function(x) 2*x^2 + 2*x
  f3 - function(x) x^2 + 3*x
  lstf - list(f1, f2, f3)
  args - c(x2, x4, x5)

  # input as a matrix or data frame
  surface - function(dat)
  {
  y - rep(0, times=nrow(dat))
  for (i in seq.int(along=lstf)) {
  y - y + lstf[[i]](dat[, args[i]])
  }
  y
  }

  dat - matrix(1:35, ncol=5)
  colnames(dat) - paste(x, 1:5, sep=)

  surface(dat)

  [1] 2140 2346 2564 2794 3036 3290 3556

  # compare with an explicit formula
  f1(dat[, x2]) + f2(dat[, x4]) + f3(dat[, x5])

  [1] 2140 2346 2564 2794 3036 3290 3556

  # input as a named vector
  surface1 - function(x)
  {
  y - 0
  for (i in seq.int(along=lstf)) {
  y - y + lstf[[i]](x[args[i]])
  }
  unname(y)
  }

  vdat - 1:5
  names(vdat) - paste(x, 1:5, sep=)

  surface1(vdat) 

  [1] 94

  # compare again
  unname(f1(vdat[x2]) + f2(vdat[x4]) + f3(vdat[x5]))

  [1] 94

Hope this helps.

Petr Savicky.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.