Hi, everyone, I am a new user of R. Recently, I tried the following commands, but couldn't make them work. If any one of you has some ideas about it, please help me. The commands are>std<-1000 >mean<-8000 >prior<-function(n){1/(sqrt(2*pi)*std)*exp(-1.0*(n-mean)^2/(2*std^2))} >plot(prior,1,15000) >post<-function(n){+ if(n < 9543) + 0 + else + prior(n)/n + }>plot(post,1,15000) # This command didn't work. The error message is " x and y lengths differ "I was really confused about it because the first function "prior" can work well. I think I must make something wrong stupidly, but could not find it. If you can help me about it, it would be very helpful. Thanx a lot in advance. Rgds, Yannong Dong
Note also the warning: "the condition has length > 1 and only the first element will be used in: if (n < 9543) 0 else prior(n)/n". The problem is that "if" is not vectorized: "if(n<9543)" is interpreted as "if(n[1]<9543)", which then produces a scalar output, 0, which presumably then generates the error message "x and y lengths differ". Try "ifelse" instead, so "post" becomes: post<-function(n){ ifelse(n < 9543, 0, prior(n)/n) } Then plot(post,1,15000) seemed to work fine for me. hope this helps. spencer graves Yannong.Dong-1 wrote:> Hi, everyone, > I am a new user of R. Recently, I tried the following commands, but couldn't make them work. If any one of you has some ideas about it, please help me. The commands are > > >>std<-1000 >>mean<-8000 >>prior<-function(n){1/(sqrt(2*pi)*std)*exp(-1.0*(n-mean)^2/(2*std^2))} >>plot(prior,1,15000) >>post<-function(n){ > > + if(n < 9543) > + 0 > + else > + prior(n)/n > + } > >>plot(post,1,15000) # This command didn't work. The error message is " x and y lengths differ " > > > I was really confused about it because the first function "prior" can work well. I think I must make something wrong stupidly, but could not find it. If you can help me about it, it would be very helpful. Thanx a lot in advance. > > Rgds, > Yannong Dong > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On Mon, 1 Sep 2003, Yannong.Dong-1 wrote:> >std<-1000 > >mean<-8000 > >prior<-function(n){1/(sqrt(2*pi)*std)*exp(-1.0*(n-mean)^2/(2*std^2))} > >plot(prior,1,15000) > >post<-function(n){ > + if(n < 9543) > + 0 > + else > + prior(n)/n > + } > >plot(post,1,15000) # This command didn't work. The error message is " x and y lengths differ "Try replacing the post() function with something like: post<-function(n){ ifelse(n < 9543, 0, prior(n) / n) } -- Cheers, Kevin ------------------------------------------------------------------------------ "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." -- Charles Babbage (1791-1871) ---- From Computer Stupidities: http://rinkworks.com/stupid/ -- Ko-Kang Kevin Wang Master of Science (MSc) Student SLC Tutor and Lab Demonstrator Department of Statistics University of Auckland New Zealand Homepage: http://www.stat.auckland.ac.nz/~kwan022 Ph: 373-7599 x88475 (City) x88480 (Tamaki)
Yannong.Dong-1 had a mysterious problem with plot() applied to a function. I think the basic problem is not that 'if() else' isn't vectorised, but that 'plot' *is*. The R documentation is fairly clear if you know where to look (like ?plot.function), and I know I've seen it mentioned in one of the R tutorials. Having a vectorised plot() makes a great deal of sense, but it can be a surprise to people who aren't used to the vector way of thinking. It looks as though mention of ?Normal might be useful too.
Hi Dong, The problem comes from the fact that you are trying to apply a function to a set of values. Prior works because it applies the same function to each component of that set. Post does not work because it applies a function which is defined by intervals. No if (bla bla bla) is allowed unless it applies to a given element of that set of values and not to the whole vector. This is why I suggest you do this :> post<-function(n){ post<-c(rep(NA,length(n))) for (i in 1:length(n)) { if(n[i]<9543) post[i]<-0 else post[i]<-prior(n[i])/n[i] } post }>plot(post(9500:15000))try this and tell me if it's what you want. Good luck, Arnaud ************************* Arnaud DOWKIW Department of Primary Industries J. Bjelke-Petersen Research Station KINGAROY, QLD 4610 Australia T : + 61 7 41 600 700 T : + 61 7 41 600 728 (direct) F : + 61 7 41 600 760 ************************** -----Original Message----- From: Yannong.Dong-1 [mailto:Yannong.Dong-1 at ou.edu] Sent: Tuesday, 2 September 2003 11:21 AM To: r-help at stat.math.ethz.ch Subject: [R] Strange problem. Hi, everyone, I am a new user of R. Recently, I tried the following commands, but couldn't make them work. If any one of you has some ideas about it, please help me. The commands are>std<-1000 >mean<-8000 >prior<-function(n){1/(sqrt(2*pi)*std)*exp(-1.0*(n-mean)^2/(2*std^2))} >plot(prior,1,15000) >post<-function(n){+ if(n < 9543) + 0 + else + prior(n)/n + }>plot(post,1,15000) # This command didn't work. The error message is " x and y lengths differ "I was really confused about it because the first function "prior" can work well. I think I must make something wrong stupidly, but could not find it. If you can help me about it, it would be very helpful. Thanx a lot in advance. Rgds, Yannong Dong ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help ********************************DISCLAIMER******************...{{dropped}}
By the way, Dong, you shouldn't use the term "mean" in Splus to designate the mean of your trait (mean<-8000). "mean' is an Splus function, and by doing mean<-8000, you may experience problems when you'll need the argument "mean" to designate the original Splus function (as in the function model.tables.aov(object.aov,type="effects") for example). I tell you that because I had this problem today. Some of the function I had created and that worked before wouldn't work anymore and I realised this was because of that "mean" element that I had generated to help you solve your problem and that I forgot to erase. Arnaud -----Original Message----- From: Yannong.Dong-1 [mailto:Yannong.Dong-1 at ou.edu] Sent: Tuesday, 2 September 2003 11:21 AM To: r-help at stat.math.ethz.ch Subject: [R] Strange problem. Hi, everyone, I am a new user of R. Recently, I tried the following commands, but couldn't make them work. If any one of you has some ideas about it, please help me. The commands are>std<-1000 >mean<-8000 >prior<-function(n){1/(sqrt(2*pi)*std)*exp(-1.0*(n-mean)^2/(2*std^2))} >plot(prior,1,15000) >post<-function(n){+ if(n < 9543) + 0 + else + prior(n)/n + }>plot(post,1,15000) # This command didn't work. The error message is " x and y lengths differ "I was really confused about it because the first function "prior" can work well. I think I must make something wrong stupidly, but could not find it. If you can help me about it, it would be very helpful. Thanx a lot in advance. Rgds, Yannong Dong ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help ********************************DISCLAIMER******************...{{dropped}}