[R] Problem about for loop

ufuk beyaztas ufukbeyaztas at gmail.com
Mon Jan 17 14:20:45 CET 2011


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.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list