[R] strange differences in vector operation versus manual calculation
William Dunlap
wdunlap at tibco.com
Wed May 2 18:06:42 CEST 2012
Your multistep approach corresponds to the following, which has one
more set of parentheses than you used
yy <- params[4] + (params[3] - params[4])/((1 + 10^(params[1]-xx))^params[2])
In R lingo, both of your approaches are vectorized and you probably won't find
a huge difference in speed between them. I would use the long approach,
as it is more understandable. (I would also give memorable names to the
intermediate results, not t1, t2, ..., unless they really had no meaning.)
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Rajarshi Guha
> Sent: Wednesday, May 02, 2012 8:42 AM
> To: R
> Subject: [R] strange differences in vector operation versus manual calculation
>
> Hi, I'm running a calculation in two ways. The first way is to employ
> vectors and evaluate a function in one go. The second way is to break
> down the function into pieces and combine the pieces to the final
> answer.
>
> Algebraically, they should give me the same result. But the final
> vector differs significantly. I must be missing something very
> obvious, but I just cannot see it
>
> xx <- c(-9.56305825951348, -8.20220288142583, -6.84134750333818,
> -5.48049212525052,
> -4.11963674716287)
> params <- structure(c(-7.9292094394, 4.9549173134, 4.738588416, 101.5743644892
> ), .Names = c("LOG_AC50", "HILL_COEF",
> "INF_ACTIVITY", "ZERO_ACTIVITY"
> ))
> yy <- params[4] + (params[3] - params[4])/(1 + 10^(params[1]-xx)^params[2])
>
> t1 <- 10^(params[1]-xx)
> t2 <- params[3] - params[4]
> t3 <- (1+t1)^params[2]
> t4 <- t2/t3;
> t5 <- params[4] + t4
>
> I would've expected yy and t5 to be the same; yet they are not
>
> --
> Rajarshi Guha | http://blog.rguha.net
> NIH Center for Advancing Translational Science
>
> ______________________________________________
> R-help at 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.
More information about the R-help
mailing list