[R] Row wise solving an equation

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Tue Mar 20 10:33:23 CET 2007


Dear Gunter,

I've rewritten your functions. But allow me to give you a few tips:
- don't be affraid to use some spacing and indentation. It makes code a
lot more readable
- I prefer to define all variables in a function or I pass them to the
function. In your case would recommend to define the constants p, q, u
and v in the function Af. This might be a problem. 
- Have you considered how the uniroot function would react upon NA
values? I've added a check for it in the output function.
- Avoid using function names as a variable names like you did with "t"
and "q".

Cheers,

Thierry

Af <- function(AB, t0){
  p <- 1
  q0 <- 1
  u <- 1
  v <- 1
  q0 * (1 - exp(u * AB[2] * t0)) + p * (1 - exp(v * AB[2] * t0) - AB[1])
}

output <- function(AB){
  if(is.na(AB) > 0){
    NA
  } else {
    uniroot(Af, c(0, 9999), AB = AB)
  }
}

apply(ddt[, 2:3], 1, output)

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be
www.inbo.be 

Do not put your faith in what statistics say until you have carefully
considered what they do not say.  ~William W. Watt
A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney

 

> -----Oorspronkelijk bericht-----
> Van: r-help-bounces op stat.math.ethz.ch 
> [mailto:r-help-bounces op stat.math.ethz.ch] Namens Eerdekens Gunter
> Verzonden: dinsdag 20 maart 2007 9:31
> Aan: R-help op stat.math.ethz.ch
> Onderwerp: [R] Row wise solving an equation
> 
> 
> Hello R-list, 
> 
> How can I row wise solve a function for which the input 
> variables are written in a matrix or a vector and have the 
> calculated output added to the matrix or written in a vector? 
> 
> To specify my case in particular I continue: I would like R 
> to calculate for me a value for 't' which is function of 
> input parameters A and B, which are comprised in a matrix 
> called 'ddt' and some other constants. q,p,u,v in this example.
> 
> The equation (q*(1-exp(u*B*t))+( p*(1-exp(v*B*t)-A)
> 
> A and B may sometimes be 'missing values' (NA (not a number)) 
> because there isn't always a result for each time step.
> 
> 
> The matrix ddt looks like this
> 	Datetime	B	A	t
> [772,]  12/12/2005 15:27:12  NA         NA            NA          
> [773,]  12/12/2005 16:27:12  5000000    1.24480000    NA          
> [774,]  12/12/2005 17:27:12  NA         NA            NA          
> [775,]  13/12/2005 13:27:12  7012500    2.64038000    NA          
> [776,]  13/12/2005 14:27:12  NA         NA            NA          
> [777,]  13/12/2005 15:31:28  NA         NA            NA   
> 
> I've tried to calculate t as followed
> Af<-function(i,t){ (q*(1-exp(u*ddt[i,2]*t))+( 
> p*(1-exp(v*ddt[i,2]*t)- ddt[i,3])
> 
> Because input B is listed in the second column and input A is 
> listed in the third column of the matrix ddt. The fourth 
> column in ddt is meant for the row wise output values of 't'. 
> 
> To calculate 't' I've tried
> output<-function(i){uniroot(Af,c(0,9999),i=i)}
> 
> Uniroot has been used on single input values for A and B and 
> gives good results and I would like to automate this. 
> Therefore I tried to use mapply
> t<-mapply(output,i=i)
> 
> This seems not work and therefore I'm asking the list: "how 
> can I calculate in R the row wise value for 't'?"
> 
> Do you have a quick solution for it?
> 
> 
> Kind regards,
> Gunter Eerdekens
> 
> 
> --
> ---------------------------------------------------------------------
> Gunter.Eerdekens op ua.ac.be
> ----------------------------------------------------------------------
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
>



More information about the R-help mailing list