[Rd] data.frame within a function (PR#9294) (cont'd)
Riyan Cheng
rcheng at purdue.edu
Thu Nov 30 06:43:29 CET 2006
This continues the message "data.frame within a function (PR#9294)" that
was posted on 2006/10/12. Duncan Murdoch kindly replied. I'm using the
current version R 2.4.0, but the same issue exists. Just copy and paste
the following code under R, and compare the output of f1() and f2() and
the output of f3() and f4(). Does anybody have any idea? Thanks.
###################################################
# R code for demonstration only #
###########################
rmvnorm<- function (n, mean = rep(0, nrow(sigma)), sigma =
diag(length(mean))){
if (nrow(sigma) != ncol(sigma)) {
stop("sigma must be a square matrix")
}
if (length(mean) != nrow(sigma)) {
stop("mean and sigma have non-conforming size")
}
ev <- eigen(sigma, sym = TRUE)$values
if (!all(ev >= -sqrt(.Machine$double.eps) * abs(ev[1])))
warning("sigma is numerically not positive definite")
sigsvd <- svd(sigma)
retval <- t(sigsvd$v %*% (t(sigsvd$u) * sqrt(sigsvd$d)))
retval <- matrix(rnorm(n * ncol(sigma)), nrow = n) %*% retval
retval <- sweep(retval, 2, mean, "+")
retval
}
f<- function(obj){
update(obj,~ .+x,evaluate=T); cat("also ok\n")
}
#########################
# compare f1() and f2() #
#########################
f1<- function(){
x<- rnorm(10)
y<- rmvnorm(10,mean=c(1,2)); colnames(y)<- paste("y",1:2,sep="")
#c("y1","y2")
dtf<- data.frame(y,x)
lm1<- lm(cbind(y1,y2)~1,data=dtf); cat("ok\n")
update(lm1,~ .+x,evaluate=T); cat("also ok\n") # only this line is
different
}
f2<- function(){
x<- rnorm(10)
y<- rmvnorm(10,mean=c(1,2)); colnames(y)<- paste("y",1:2,sep="")
#c("y1","y2")
dtf<- data.frame(y,x)
lm1<- lm(cbind(y1,y2)~1,data=dtf); cat("ok\n")
f(lm1) # only this line is different
}
f1()
f2()
#########################
# compare f3() and f4() #
#########################
f3<- function(){
x<- rnorm(10)
y<- rmvnorm(10,mean=c(1,2)); colnames(y)<- paste("y",1:2,sep="")
#c("y1","y2")
lm1<- lm(cbind(y1,y2)~1,data=data.frame(y,x)); cat("ok\n")
update(lm1,~ .+x,evaluate=T); cat("also ok\n") # only this line is
different
}
f4<- function(){
x<- rnorm(10)
y<- rmvnorm(10,mean=c(1,2)); colnames(y)<- paste("y",1:2,sep="")
#c("y1","y2")
lm1<- lm(cbind(y1,y2)~1,data=data.frame(y,x)); cat("ok\n")
f(lm1) # only this line is different
}
f3()
f4()
#########
# the end #
#########
More information about the R-devel
mailing list