[R] How to generate a matrix of Beta or Binomial distribution

2012-08-27 Thread JeffND
Hi folks,

I have a question about how to efficiently produce random numbers from Beta
and Binomial distributions. 

For Beta distribution, suppose we have two shape vectors shape1 and shape2.
I hope to generate a 1 x 2 matrix X whose i th rwo is a sample from
reta(2,shape1[i]mshape2[i]). Of course this can be done via loops:

for(i in 1:1)
{
X[i,]=rbeta(2,shape1[i],shape2[i])
}

However, the above code is time consuming. It would be better to directly
generate X without any loops. I tried the following code

X- rbeta(2,shape1,shape2)

but it looks not right. So I was wondering if there is an R code doing this.

For Binomial distribution, I have a similar question.  I hope to generate a
1 x n matrix Y whose i th row is a sample from rbinom(n,2,p[i]), where p
is a vector of binomial probabilities. We can also do it by loops

for(i in 1:1)
{
Y[i,]=rbinom(n,2,p[i])
}

But it would be nice to do it without using any loops. Is this possible in
R?

Many thanks for great help and suggestions!
Jeff









--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-generate-a-matrix-of-Beta-or-Binomial-distribution-tp4641422.html
Sent from the R help mailing list archive at Nabble.com.

__
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] How to generate a matrix of Beta or Binomial distribution

2012-08-27 Thread JeffND
Dear Michael,

Thanks very much for your explicit explanation! That makes much sense.  

Best wishes,
Jeff



--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-generate-a-matrix-of-Beta-or-Binomial-distribution-tp4641422p4641472.html
Sent from the R help mailing list archive at Nabble.com.

__
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] how to add a variable to a graph title

2012-07-13 Thread JeffND
Thank you for this useful code!

--
View this message in context: 
http://r.789695.n4.nabble.com/how-to-add-a-variable-to-a-graph-title-tp4636364p4636405.html
Sent from the R help mailing list archive at Nabble.com.

__
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 to add a variable to a graph title

2012-07-12 Thread JeffND
Hi folks,

To simplify my question, consider an example:

x=runif(1,0,1)
plot(1:5,1:5)

Now I want to add a title to the above plot showing the vaue of x, so if the
generated x is 0.3, graphically the tile should be like Figure 1: x=0.3.
How do I achieve this in R?

Thanks.
Jeff


--
View this message in context: 
http://r.789695.n4.nabble.com/how-to-add-a-variable-to-a-graph-title-tp4636364.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Local linear regression

2012-06-21 Thread JeffND
locpoly() might be useful

--
View this message in context: 
http://r.789695.n4.nabble.com/Local-linear-regression-tp4512566p4634166.html
Sent from the R help mailing list archive at Nabble.com.

__
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 to find eigenfunctions and eigenvalues of a fourth order ODE

2012-04-27 Thread JeffND
Dear all,

I am having trouble with the following problem. Suppose we have the fourth
order ODE with boundary conditions:

http://r.789695.n4.nabble.com/file/n4591748/problem.jpg problem.jpg 

where q(t) is a known function.

Note here the lambda parameter is changing, so essentially we have a series
of ODEs. lambda is called an eigenvalue, the solution y is called an
eigenfunction associated with lambda. The question is to find the
eigenvalues lambda and eigenfunctions y. 

In matlab, this can be done. I am wondering if R has similar functions to
achieve this goal. Any suggestions will be highly appreciated. 

Best regards,
Jeff

--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-find-eigenfunctions-and-eigenvalues-of-a-fourth-order-ODE-tp4591748p4591748.html
Sent from the R help mailing list archive at Nabble.com.

__
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] efficiently finding the integrals of a sequence of functions

2011-12-11 Thread JeffND
Thanks,Hans!

I agree that this is a good way of solving this problem. 

Here is another way. Instead of defining a vector of uni-dimensional
functions and trying to integrating
each component (a uni-dimensional function), we can do something below

my.integrand-function(x,k)
{
return(f[x,k]) ## use matrix to represent the sequence of uni-dimensional
functions
}

my.integral-function(k) ## k=1,...,5000 denotes the function labels
{
return(integrate(my.integrand,lower=...,upper=...,k)$value)
}

When calculating the integrals, just perform

sapply(1:5000, my.integral)

This is a way of avoiding loops but the computing time needs to be carefully
examined.

Jeff



--
View this message in context: 
http://r.789695.n4.nabble.com/efficiently-finding-the-integrals-of-a-sequence-of-functions-tp4179452p4183323.html
Sent from the R help mailing list archive at Nabble.com.

__
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] efficiently finding the integrals of a sequence of functions

2011-12-10 Thread JeffND
Hi folks,

I am having a question about efficiently finding the integrals of a list of
functions. To be specific,
here is a simple example showing my question.

Suppose we have a function f defined by

f-function(x,y,z) c(x,y^2,z^3) 

Thus, f is actually corresponding to three uni-dimensional functions
f_1(x)=x, f_2(y)=y^2 and f_3(z)=z^3.
What I am looking for are the integrals of these three functions f_1,f_2,f_3
over some interval, say, (0,1).
More specifically, the integrals \int_0^1 f_1(x) dx, \int_0^1 f_2(y) dy and
\int_0^1 f_3(z) dz.

For this simple example, of course we can do these three integrals one by
one using

integrate (f_1, lower=0, upper=1)

However, in practice, I have a sequence of 5000 uni-dimensional functions
and hope to find all of their 
integrals (over some regions), so using loops to do this one by one is not
efficient. 

A possible idea is to convert the sequence of functions to a list of
objects, and use sapply()
which allow us to find the integrals in a fashion of vectorizing. But I
don't know how to convert
the above example function f to a list. In my research problem, I can only
define the 5000 functions
in a vectorizing way like

f-function(x1,x2,...,x5000) c(f1(x1),f2(x2),...,f5000(x5000))

So how to convert it to a list will be a crucial step to efficiently get the
integrals.

Thanks for all the suggestions!
Jeff




--
View this message in context: 
http://r.789695.n4.nabble.com/efficiently-finding-the-integrals-of-a-sequence-of-functions-tp4179452p4179452.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Overlaying density plot on forest plot

2011-12-10 Thread JeffND
Frank,

Have you tried the R function overlay(), it is exactly applying to your
question.

Jeff

--
View this message in context: 
http://r.789695.n4.nabble.com/Overlaying-density-plot-on-forest-plot-tp4179654p4180430.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Regression Models

2011-12-10 Thread JeffND
So your question is about fitting a regression model for all the subsets of
predictors? Then there would be
2^13 submodesl? 

Probably leaps() does what you want. This function does a all-subset
regresion.



--
View this message in context: 
http://r.789695.n4.nabble.com/Regression-Models-tp4173278p4180447.html
Sent from the R help mailing list archive at Nabble.com.

__
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] A question about R image function

2011-11-23 Thread JeffND
Hi Sarah,

Thanks a lot! You are right, my data is not over a regular grid cell
locations.
One advantage of image() is that it can produce continuous color change for
the data value.
When the data value is over a large range, this will make it more
convenient.

Thanks!
Jeff

--
View this message in context: 
http://r.789695.n4.nabble.com/A-question-about-R-image-function-tp3875581p4102588.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Using GIS data in R

2011-11-20 Thread JeffND
Hi Rolf,

I have a similar question. I want to test whether a point with certain
coordinates is inside 
a state, say Texas. It seems that inside.owin() only works for testing if a
point lies in a
regular region, say a polygon. Since Texas has irregular boundary, how do we
achieve this?
Or there is some alternative way we can use.

Thanks!
Jeff  

--
View this message in context: 
http://r.789695.n4.nabble.com/Using-GIS-data-in-R-tp1748266p4089242.html
Sent from the R help mailing list archive at Nabble.com.

__
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 to use quadrature to integrate some complicated functions

2011-11-06 Thread JeffND
Hello to all,

I am having trouble with intregrating a complicated uni-dimensional function
of the following form

Phi(x-a_1)*Phi(x-a_2)*...*Phi(x-a_{n-1})*phi(x-a_n).

Here n is about 5000, Phi is the cumulative distribution function of
standard normal, 
phi is the density function of standard normal, and x ranges over
(-infty,infty).

My idea is to to use quadrature to handle this integral. But since Phi has
not cloaed form,
I don't know how to do this effeciently. I appreciate very much if someone
has any ideas about it.

Thanks!

Jeff

--
View this message in context: 
http://r.789695.n4.nabble.com/how-to-use-quadrature-to-integrate-some-complicated-functions-tp3996765p3996765.html
Sent from the R help mailing list archive at Nabble.com.

__
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] ploting pie charts centered at given points

2011-10-12 Thread JeffND
Hi to all,

Suppose we have a group of points on the plane,
at each point, we want to draw a pie chart centered at that point.

I have found that pie.labels() function might be useful. This function
allows us
to achieve my goal, but before using the function,
we have to use the function plot(), say, the following example code:

pieval-c(2,4,6,8)
plot(1:5,type=n,axes=FALSE)
box()
bisect.angles-floating.pie(3,3,pieval)
pie.labels(3,3,bisect.angles,c(two,four,six,eight))

I am wondering why use 1:5 in plot?
Can I use for loop language to draw many pie charts
on a plane? For this case, using 1:5 may not work.
I also tried other numbers such like 1:10, 1:3,
all failed. 

I stronglly appreciate your time and help!

Jeff

--
View this message in context: 
http://r.789695.n4.nabble.com/ploting-pie-charts-centered-at-given-points-tp3896771p3896771.html
Sent from the R help mailing list archive at Nabble.com.

__
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] A question about R image function

2011-10-05 Thread JeffND
Dear folks,

I have a question about the image() function in R. I found the following
link talking about this
but the replies didn't help with my situations.

http://r.789695.n4.nabble.com/question-on-image-function-td839275.html#a839276

To be simple, I will keep using the example in the above link.

Suppose the data are like 

 xy  mcpvalue
0.4603578 0.6247629   1.001
0.4603715 0.62477881.001
0.4603852 0.6247948   1.001
0.4110561 0.5664841   0.995 

So we have four points with coordinates given by the four pairs of (x,y)
values.
Each point is associated with a mcpvalue.

How do we use image() to plot mcpvalue as a two-dimensional plot?
I hope that the points are positioned by the coordinates and the color of
each point 
in that plot is changing with the value of mcpvalue.

Using image() does not work as the coordinates of the points
are not ascending.

Thanks a lot!
Jeff


--
View this message in context: 
http://r.789695.n4.nabble.com/A-question-about-R-image-function-tp3875581p3875581.html
Sent from the R help mailing list archive at Nabble.com.

__
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.