[R] Strange problem.

Dowkiw, Arnaud Arnaud.Dowkiw at dpi.qld.gov.au
Tue Sep 2 06:47:28 CEST 2003


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}}




More information about the R-help mailing list