[R] Beginner question on apply()

From: Thomas Mählmann <maehlmann_at_wiso.uni-koeln.de>
Date: Wed 26 Oct 2005 - 01:50:32 EST


Dear all,

I am an beginner with R and I have a question concerning apply(). My problem is as follows:

I have data on four variables (x1,x2,x3,x4) with missing values for x1 and x2.

> data[1:9,]

   x1 x2 x3 x4
1 NA NA 10 1
2 NA NA 8 3
3 NA NA 13 7
4 9 9 9 9
5 11 14 20 10
6 14 14 14 15
7 6 6 6 8
8 4 4 4 19
9 12 12 12 8
.....

I would like to replace the missing values by values sampled from the conditional distribution p(x1,x2|x3,x4) of x1 and x2, given the observed data on x3 and x4.
This conditional distribution is specified as a product of two normals: p1(x1|x2,x3,x4)*p2(x2|x3,x4), where the mean of p1 is given by a1*x2+a2*x3+a4*x4 and for p2 the mean is b1*x3+b2*x4.(Note that the means change for every observation)

I use Adaptive Rejection Metropolis Sampling (function arms() from package HI) to draw values from p(x1,x2|x3,x4). My code is:

### Begin of function
> myfunction <- function() {

### 1. Step: Definition of my log density log(p1(x1|x2,x3,x4)*p2(x2|x3,x4)), assuming values for (a1,a2,a3) and (b1,b2)

> mylogdensity <- function(x){

+ log(prod(dnorm(x[1], mean=0.1*x[2]+0.4*x3+0.9*x4), dnorm(x[2], mean=2*x3-3.1*x4)))}

### 2. Step: Drawing 1000 samples from this density

> y <- arms(c(0,0), mylogdensity, function(x) crossprod(x)<=1e6, 1000)

### 3. Step: replacing the missing values by the sampled values  

> newdata <- data.frame(x1=y[,1], x2=y[,2], x3=rep(x3, 1000), x4=rep(x4,
1000))
}
### End of function

To apply this procedure to every observation (row) with missing data on x1 and x2, I use:

> ifelse(is.na(x1), apply(data, 1, myfunction), newdata <- dataframe(x1=x1,
x2=x2, x3=x3, x4=x4))

After this I get the error message

Error in FUN(newX[, i], ...) : unused argument(s) ( ...)

I have no idea about the meaning of this message and where the error is (contained in the "ifelse"-statement or already in the definiton of "myfunction"?)

Any help is highly appreciated!!

Best wishes,
Thomas



Thomas Mählmann
Department of Economics
University of Cologne
Albertus-Magnus Platz
50923 Koeln
Germany
Tel: (0049) +221-4702628
Fax: (0049) +221-4702305
Email: maehlmann@wiso.uni-koeln.de

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Oct 27 01:53:55 2005

This archive was generated by hypermail 2.1.8 : Thu 27 Oct 2005 - 03:32:21 EST