Hi everybody, I`m Mark and I do my PhD in biology. I try using R to calculate a population growth rate of animals grown on different types of food. Our workgroup has a R-skript to do so, but sadly nobody, who knows how this works. I`ve never used R before, but got some stuff figured out myself. The skript goes: setwd("c:/Mark") table <- read.table("r-TCO-Scene-Kontrolle.csv") table x <- c(table$x) L <- c(table$lx) m <- c(table$mx) r.range<- c(0, 5) eulerlotka <- function(r) sum(L * m * exp(-r * x)) - 1 res <- uniroot(f = eulerlotka, interval = r.range, tol = 1e-8) res$root I understood that the first 3 lines are simply to load my data and show it in the workspace console. The next 4 lines define variables of my .csv table for R. Thus far, everything works fine. Now comes the Euler-Lotka equation, but somehow, this does not seem to work out as supposed. I tried to find a solution on the internet, but I think this formula is somewhat special so I could not find a suitible solution for my special problem. The console says: > setwd("c:/Mark") > > table <- read.table("r-TCO-Scene-Kontrolle.csv") > > table V1 1 x;lx;mx 2 1;1;0 3 2;1;0 4 3;1;0 5 4;1;0 6 5;1;0 7 6;1;0 8 7;1;0 9 8;1;0 10 9;1;0 11 10;1;0 12 11;0.2;3.5 > > x <- c(table$x) > L <- c(table$lx) > m <- c(table$mx) > r.range<- c(0, 5) > > eulerlotka <- function(r) sum(L * m * exp(-r * x)) - 1 > res <- uniroot(f = eulerlotka, interval = r.range, tol = 1e-8) Error in uniroot(f = eulerlotka, interval = r.range, tol = 1e-08) : f() values at end points not of opposite sign > > res$root Does anybody have an idea how to get this running? Thanks in advance Kind regards Mark
Your script is failing at the first hurdle because you data are not being imported properly. You should include header=TRUE. sep=";" In the call to read.table. Cheers. Simon. Sent from my iPhone Sent from my iPhone> On 30 Jun 2014, at 7:10 pm, "Mark Christjani" <m_christjani at gmx.de> wrote: > > > Hi everybody, > > I`m Mark and I do my PhD in biology. I try using R to calculate a population > growth rate of animals grown on different types of food. Our workgroup has a > R-skript to do so, but sadly nobody, who knows how this works. I`ve never > used R before, but got some stuff figured out myself. The skript goes: > > setwd("c:/Mark") > table <- read.table("r-TCO-Scene-Kontrolle.csv") > table > x <- c(table$x) > L <- c(table$lx) > m <- c(table$mx) > r.range<- c(0, 5) > eulerlotka <- function(r) sum(L * m * exp(-r * x)) - 1 > res <- uniroot(f = eulerlotka, interval = r.range, tol = 1e-8) > res$root > > I understood that the first 3 lines are simply to load my data and show it > in the workspace console. The next 4 lines define variables of my .csv table > for R. Thus far, everything works fine. Now comes the Euler-Lotka equation, > but somehow, this does not seem to work out as supposed. I tried to find a > solution on the internet, but I think this formula is somewhat special so I > could not find a suitible solution for my special problem. The console says: > > >> setwd("c:/Mark") >> >> table <- read.table("r-TCO-Scene-Kontrolle.csv") >> >> table > V1 > 1 x;lx;mx > 2 1;1;0 > 3 2;1;0 > 4 3;1;0 > 5 4;1;0 > 6 5;1;0 > 7 6;1;0 > 8 7;1;0 > 9 8;1;0 > 10 9;1;0 > 11 10;1;0 > 12 11;0.2;3.5 >> >> x <- c(table$x) >> L <- c(table$lx) >> m <- c(table$mx) >> r.range<- c(0, 5) >> >> eulerlotka <- function(r) sum(L * m * exp(-r * x)) - 1 >> res <- uniroot(f = eulerlotka, interval = r.range, tol = 1e-8) > Error in uniroot(f = eulerlotka, interval = r.range, tol = 1e-08) : > f() values at end points not of opposite sign >> >> res$root > > Does anybody have an idea how to get this running? > > Thanks in advance > > Kind regards > Mark > ______________________________________________ > 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.
Mark - good on you for jumping into R with both feet. In addition to Simon's advice, the error message you are getting means that the two endpoints (r.range in your case) do not give function values that are of the opposite sign and so either the function has no roots on the interval or has and even number of roots on the interval (2,4,6,...). Neither situation is good for your case because uniroot finds the single root (where the y-value = 0) on the interval. Once the reading issues are settled, try narrowing or expanding the r.range. A good way to find a proper r.range is to plot the function vs. r and locate the range of r where the function crosses zero. Then use the region of r around that point as your r.range. You also learn a lot about that function by studying that plot. Regards David On 6/30/2014 1:39 AM, Mark Christjani wrote:> Hi everybody, > > I`m Mark and I do my PhD in biology. I try using R to calculate a population > growth rate of animals grown on different types of food. Our workgroup has a > R-skript to do so, but sadly nobody, who knows how this works. I`ve never > used R before, but got some stuff figured out myself. The skript goes: > > setwd("c:/Mark") > table <- read.table("r-TCO-Scene-Kontrolle.csv") > table > x <- c(table$x) > L <- c(table$lx) > m <- c(table$mx) > r.range<- c(0, 5) > eulerlotka <- function(r) sum(L * m * exp(-r * x)) - 1 > res <- uniroot(f = eulerlotka, interval = r.range, tol = 1e-8) > res$root > > I understood that the first 3 lines are simply to load my data and show it > in the workspace console. The next 4 lines define variables of my .csv table > for R. Thus far, everything works fine. Now comes the Euler-Lotka equation, > but somehow, this does not seem to work out as supposed. I tried to find a > solution on the internet, but I think this formula is somewhat special so I > could not find a suitible solution for my special problem. The console says: > > > > setwd("c:/Mark") > > > > table <- read.table("r-TCO-Scene-Kontrolle.csv") > > > > table > V1 > 1 x;lx;mx > 2 1;1;0 > 3 2;1;0 > 4 3;1;0 > 5 4;1;0 > 6 5;1;0 > 7 6;1;0 > 8 7;1;0 > 9 8;1;0 > 10 9;1;0 > 11 10;1;0 > 12 11;0.2;3.5 > > > > x <- c(table$x) > > L <- c(table$lx) > > m <- c(table$mx) > > r.range<- c(0, 5) > > > > eulerlotka <- function(r) sum(L * m * exp(-r * x)) - 1 > > res <- uniroot(f = eulerlotka, interval = r.range, tol = 1e-8) > Error in uniroot(f = eulerlotka, interval = r.range, tol = 1e-08) : > f() values at end points not of opposite sign > > > > res$root > > Does anybody have an idea how to get this running? > > Thanks in advance > > Kind regards > Mark > ______________________________________________ > 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.-- David K Stevens, P.E., Ph.D. Professor and Head, Environmental Engineering Civil and Environmental Engineering Utah Water Research Laboratory 8200 Old Main Hill Logan, UT 84322-8200 435 797 3229 - voice 435 797 1363 - fax david.stevens at usu.edu