[R] y needing more than 2 functions
Dear all, I'm aware if y has two separate functions (depending on the conditions of x) you can use the ifelse function to separate y into two separate functions depending on input. How do you do this if there a multiple different conditions for x? for example, y fits the following between t0 t15-function(t) t^2, y fits the following between t15 t30- function(t) t^3, y fits the following between t30 t45---function(t) t^4 etc Thanks for any help you are able to give, yours sincerely, Aimee __ 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] Trouble with strwrap function
Hi all, I am formatting some graphs and adding text to them. When using the strwrap function in the code below, it properly cuts the main title into sizeable chunks and pastes the second line below that of the first. The subtitle however, is all pasted on the same line, meaning that it reads as gibberish in the graphical output. What can I do to make sure the subtitle posts in consecutive lines (without using /n )? thank you for your assistance, Aimee Jones par(mfrow=c(1,1)) plot(out_rk4$time, out_rk4$N2, col=blue, type=l, lwd=3, cex.lab=0.75, xlim=c(0,3650), ylim=c(0,5000),xlab=Time(days), ylab=Numbers of Population, main=paste(strwrap(Total Cattle population and Infected Aedes and Culex mosquitoes as a function of time, width= 60))) par(new=TRUE) plot(out_rk4$time, out_rk4$I1, col=red, type=l, lwd=3, xlim=c(0,3650), ylim=c(0,5000), xlab=, ylab=) par(new=TRUE) plot(out_rk4$time, out_rk4$I3, col=green, type=l, lwd=3, xlim=c(0,3650), ylim=c(0,5000), xlab=, ylab=, cex.sub=0.5, sub=paste(strwrap(The peaks in infected Aedes and Culex mosquitoes correspond to a fall in cattle population due to a die-off. Infected mosquito populations return to 0 when cattle population falls below a critical level to maintain the virus in circulation. Births increase the cattle population past this critical limit and once again, a mass die-off of cattle and spike in infected mosquitoes occurs., width =360)) ) par(new=FALSE) __ 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] Adding a migration element to a deterministic spatial model
Hi all, I've been working with a friend's model that is a spatial model consisting of 4 patches. She uses the code found below to add migration between the patches for the three species of concern. When I run a script incorporating this code, all four patches run independently without migration occuring. When I type m1[1,2] into the workspace to see if it will return a figure, it returns, error, object m1 not found. However, if I type the code below directly into the workspace, it finds and returns m1[1,2] (or any other value). I have tried to find other examples of code that add a migration element to a spatial model but have not had any success. I need help figuring out why m#[i,j] consistently returns a 0 value (or at least appears to) despite the code instructing R to return different values under set circumstances. thanks for any help you are able to give, yours sincerely, Aimee with(as.list(c(y,p)), { dydt - rep(0,NP*16) # Initialize m1,m2,m3 m1 - matrix(0,NP,NP) m2 - matrix(0,NP,NP) m3 - matrix(0,NP,NP) # Only nearest neighbors have nonzero travel rates between each other for (i in 1:NP) { for (j in 1:NP) { if ((i%%sqrt(NP)) == 0 i!=j) { if (j == i+sqrt(NP) | j == i-1 | j == i-sqrt(NP)) { m1[j,i] - 0.1 m1[i,j] - 0.1 m2[j,i] - 0.25 m2[i,j] - 0.25 m3[j,i] - 0.1 m3[i,j] - 0.1 } } if (i%%sqrt(NP) == 1) { if (j == i+sqrt(NP) | j == i+1 | j == i-sqrt(NP)) { m1[j,i] - 0.1 m1[i,j] - 0.1 m2[j,i] - 0.25 m2[i,j] - 0.25 m3[j,i] - 0.1 m3[i,j] - 0.1 } } if (i%%sqrt(NP)!=0 i%%sqrt(NP)!=1 i!=j) { if (j == i+sqrt(NP) | j == i+1 | j == i-sqrt(NP)| j == i-1) { m1[j,i] - 0.1 m1[i,j] - 0.1 m2[j,i] - 0.25 m2[i,j] - 0.25 m3[j,i] - 0.1 m3[i,j] - 0.1 } } } } # Initialize the summaries of incoming movements to a patch s - rep(0,NP*16) for (i in 1:NP) { z - (i-1)*16 for (k in 3:6) { for (j in 1:NP) { zz - (j-1)*16 s[z+k] - s[z+k] + m1[i,j]*y[zz+k] } } for (k in 7:11) { for (j in 1:NP) { zz - (j-1)*16 s[z+k] - s[z+k] + m2[i,j]*y[zz+k] } } for (k in 13:16) { for (j in 1:NP) { zz - (j-1)*16 s[z+k] - s[z+k] + m3[i,j]*y[zz+k] } } __ 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] How does once import a function from an imported script?
Hi all, I am trying to import some functions into a script and am having difficulty in doing so. I am able to import a series of functions from a .tex file into my script, and call on each function by column name, however R reads them as data rather than as functions and I am struggling with the syntax to make R read them as a function instead. Below is a small set of the things I have tried: parms[B1] returns the character string {2*dnorm(T1(t),mean=32.5,sd=7)} . This is from the imported script parms. when I write B1-function(T1,t) (parms[B1]) and then enter B1 to see what the workspace returns, I get function(T1,t) (parms[B1]) I am trying to get B1 to return as function(T1,t) {2*dnorm(T1(t),mean=32.5,sd=7)}, so that I can plot B1(T1,t). Is there any way to do this? Thank you for your assistance and patience, yours sincerely, Aimee Jones __ 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] Plotting compound functions--help with defining x-axis as f(x)
Thanks for your assistance, this worked perfectly, and sorry for posting in html! I shall check it's plain text for future postings.. Would it be possible for you to explain why this works? I'm unsure as to why redefining t works, when t is perfectly defined within the script previously. Thank you for any further help, Aimee 2011/7/24 Uwe Ligges lig...@statistik.tu-dortmund.de On 23.07.2011 00:50, Aimee Jones wrote: Hi all, I'm having trouble locating a script that will allow to me to create graphs that show compound functions as a function of the simple function, rather than just x (or time as it is in my case). Currently I have the following functions defined in my script: T1-function(t) {27.5-12.5*cos(2*pi*t/365)**} This is not syntactically correct. Perhaps some leftovers of your html message. The posting guide asks you *not* to send html mail! and B1-function(T1,t) {dnorm(T1(t),mean=22.5,sd=**3.3)} plot(function(t) {B1(T1,t)}, 0, 365) plots B1 as a function of time, whereas I am looking for a code that allows me to plot B1 as a function of T1. I tried plot(function(T1(t) {B1(T1,t)}, 0, 365) and also plot(function(T1,t) {B1(T1,t)}, 0, 365), neither of which worked. My coding skills are very limited, and I'm somewhat out of ideas.. So you probably want something along the lines: t - seq(0, 365, length=1000) plot(T1(t), B1(T1, t), type=l) Uwe Ligges Thank you for any assistance you are able to give, yours sincerely, Aimee ps: If it's relevant I'm using R64 (R 2.11.1) on a Mac [[alternative HTML version deleted]] __ 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-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] Plotting compound functions--help with defining x-axis as f(x)
Hi all, I'm having trouble locating a script that will allow to me to create graphs that show compound functions as a function of the simple function, rather than just x (or time as it is in my case). Currently I have the following functions defined in my script: T1-function(t) {27.5-12.5*cos(2*pi*t/365)**} and B1-function(T1,t) {dnorm(T1(t),mean=22.5,sd=**3.3)} plot(function(t) {B1(T1,t)}, 0, 365) plots B1 as a function of time, whereas I am looking for a code that allows me to plot B1 as a function of T1. I tried plot(function(T1(t) {B1(T1,t)}, 0, 365) and also plot(function(T1,t) {B1(T1,t)}, 0, 365), neither of which worked. My coding skills are very limited, and I'm somewhat out of ideas.. Thank you for any assistance you are able to give, yours sincerely, Aimee ps: If it's relevant I'm using R64 (R 2.11.1) on a Mac [[alternative HTML version deleted]] __ 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] Trouble with compound functions---differential equations
Hey, Thank you for your swift responses, both options work great. Aimee On Wed, Jun 15, 2011 at 11:30 PM, Rolf Turner rolf.tur...@xtra.co.nzwrote: On 16/06/11 11:07, Aimee Jones wrote: Hi all, My apologies if this message is incredibly inept but I am very new to both computer programming and to R. I am working with the odesolve add-on and have the following function defined RVF_Single- function(t, x, p) within the script I also have the following functions defined: T1-function(t) {T1-27.5-12.5*cos(2*pi*t/365)} and B1-function(T1,t) {B1-dnorm(T1(t),mean=22.5,sd=3.3)} Actually your code should read: T1-function(t) {27.5-12.5*cos(2*pi*t/365)} and B1-function(T1,t) {dnorm(T1(t),mean=22.5,sd=3.3)} i.e. don't assign the value that you calculate in the code; this is the value ***returned*** by the function. What you is in effect harmless here, but it is confusing and could cause problems in other contexts. When the script is run it doesn't return an error message but the graphs returned are wrong. When I input plot(T1,0,3650) it returns the plot of T1 as expected---a series of waves between 15 and 40, BUT when I input plot(B1,0,3650) I get an error message of Error in 2 * pi * t : 't' is missing. Can anyone advise as to why t registers for function T1 but disappears for function B1? Well, T1() a function of ***t*** only (where t is the variable against which you expect the values of T1() to be plotted. Whereas, B1 is a function of two variables T1 and t, which confuses things. Note that by calling plot() in this way you are in fact calling plot.function() which is in fact a wrapper for curve(). As has been discussed recently on this list, the syntax for curve() is a bit delicate. A workaround for your problem is: plot(function(t){B1(T1,t)},0,3650) HTH cheers, Rolf Turner [[alternative HTML version deleted]] __ 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] Trouble with compound functions---differential equations
Hi all, My apologies if this message is incredibly inept but I am very new to both computer programming and to R. I am working with the odesolve add-on and have the following function defined RVF_Single - function(t, x, p) within the script I also have the following functions defined: T1-function(t) {T1-27.5-12.5*cos(2*pi*t/365)} and B1-function(T1,t) {B1-dnorm(T1(t),mean=22.5,sd=3.3)} When the script is run it doesn't return an error message but the graphs returned are wrong. When I input plot(T1,0,3650) it returns the plot of T1 as expected---a series of waves between 15 and 40, BUT when I input plot(B1,0,3650) I get an error message of Error in 2 * pi * t : 't' is missing. Can anyone advise as to why t registers for function T1 but disappears for function B1? Thank you, Aimee ps: If it's relevant I'm using R64 (R 2.11.1) on a Mac [[alternative HTML version deleted]] __ 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.