Hi,
Looks like the function "theta" takes a variable "data", but
that
variable is not being used in the body of the function (you are using
the global dataX and dataY, which will be the same each time the
function is called).
Martyn
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of ufuk beyaztas
Sent: 17 January 2011 13:21
To: r-help at r-project.org
Subject: [R] Problem about for loop
Hi everyones, my function like;
e <- rnorm(n=50, mean=0, sd=sqrt(0.5625))
x0 <- c(rep(1,50))
x1 <- rnorm(n=50,mean=2,sd=1)
x2 <- rnorm(n=50,mean=2,sd=1)
x3 <- rnorm(n=50,mean=2,sd=1)
x4 <- rnorm(n=50,mean=2,sd=1)
y <- 1+ 2*x1+4*x2+3*x3+2*x4+e
x2[1] = 10 #influential observarion
y[1] = 10 #influential observarion
data.x <- matrix(c(x0,x1,x2,x3,x4),ncol=5)
data.y <- matrix(y,ncol=1)
data.k <- cbind(data.x,data.y)
dataX <- data.k[,1:5]
dataY <- data.k[,6]
theta <- function(data) {
B.cap <- solve(crossprod(dataX)) %*% crossprod(dataX,dataY)
P <- dataX %*% solve(crossprod(dataX)) %*% t(dataX)
Y.cap <- P %*% dataY
e <- dataY - Y.cap
dX <- nrow(dataX) - ncol(dataX)
var.cap <- crossprod(e) / (dX)
ei <- as.vector(dataY - dataX %*% B.cap)
pi <- diag(P)
var.cap.i <- (((dX) * var.cap) / (dX - 1)) -
(ei^2 / ((dX-1) * (1 - pi)))
ti <- ei / sqrt(var.cap * (1 - pi))
Ci <- (ti^2 / (ncol(dataX))) * (pi / (1 - pi))
output.i <- mean(Ci)}
result <- list()
for ( i in 1:5){
data <- replicate(1, data.k[sample(50,50,replace=T),], simplify = FALSE)
output.j <- theta(data)
result <- c(result,(list(output.j))) }
table <- do.call(rbind.data.frame,result)
names(table)=c("cooks")
table
This function give same results each time, the data is changing every
time
but mean(Ci)s are always same.
Does anyone have an idea about how to be? Thanks for any idea
--
View this message in context:
http://r.789695.n4.nabble.com/Problem-about-for-loop-tp3221210p3221210.h
tml
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at 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.
________________________________________________________________________
This e-mail has been scanned for all viruses by Star.\ _...{{dropped:12}}