iterations <- 100 nvars <- 4 combined <- rbind(scaleMiceTrain, scaleMiceTest) reducedSample <- combined reducedSample <- subset(reducedSample, select = -pID50) reducedSample <- subset(reducedSample, select = -id) for (i in 1:iterations) { miceSample <- sample(combined[,-c(1,2)],nvars, replace=FALSE) miceSample$pID50 <- combined$pID50 miceTestSample <- miceSample[47:55,] miceTrainSample <- miceSample[1:46,] fit.kknn <- kknn(pID50~., miceTrainSample, miceTestSample) table(miceTestSample$pID50, fit.kknn$fit) (fit.train1 <- train.kknn(pID50~., miceTrainSample, kmax=15, kernel=c("rectangular"), distance=1)) predictedTrain <- predict(fit.train1, miceTrainSample, miceTrainSample$pID50) pID50Train <- miceTrainSample$pID50 lmTrain <- lm(predictedTrain~pID50Train) slm <- summary(lmTrain) str(slm) if (i == 1) { previousR2 <-slm$r.squared sink(file="R2outputKKNN.txt", append=TRUE) previousR2 sink() } else if(i!=1) { currentR2 <- slm$r.squared if (previousR2 > currentR2) { currentR2 <- previousR2 } if (previousR2 < currentR2) { sink(file="R2outputKKNN.txt", append=TRUE) currentR2 sink() } } } In my code above, I can't get sink to work. In summary, I'm trying to write the first run's R2, which is called "previousR2" to file, and then anytime "currentR2" > "previousR2", I will write "currentR2" to file. After running the code above, my file R2outputKKNN.txt is empty... However, just running the code below writes / works fine: previousR2 <-slm$r.squared sink(file="R2outputKKNN.txt", append=TRUE) previousR2 sink() -- View this message in context: http://r.789695.n4.nabble.com/Help-with-Sink-Function-tp2291705p2291705.html Sent from the R help mailing list archive at Nabble.com.
This is not reproducible, and does not look minimal. You'll get better answers, and probably solve many issues on your own, if you construct small examples that illustrate the same problem you're having with your real data. Addi Wei wrote:> iterations <- 100 > nvars <- 4 > combined <- rbind(scaleMiceTrain, scaleMiceTest) > reducedSample <- combined > reducedSample <- subset(reducedSample, select = -pID50) > reducedSample <- subset(reducedSample, select = -id) > for (i in 1:iterations) > { > miceSample <- sample(combined[,-c(1,2)],nvars, replace=FALSE) > > miceSample$pID50 <- combined$pID50 > miceTestSample <- miceSample[47:55,] > miceTrainSample <- miceSample[1:46,] > > > fit.kknn <- kknn(pID50~., miceTrainSample, miceTestSample) > table(miceTestSample$pID50, fit.kknn$fit) > (fit.train1 <- train.kknn(pID50~., miceTrainSample, kmax=15, > kernel=c("rectangular"), distance=1)) > > predictedTrain <- predict(fit.train1, miceTrainSample, > miceTrainSample$pID50) > pID50Train <- miceTrainSample$pID50 > lmTrain <- lm(predictedTrain~pID50Train) > slm <- summary(lmTrain) > str(slm) > if (i == 1) > { > previousR2 <-slm$r.squared > sink(file="R2outputKKNN.txt", append=TRUE) > previousR2 > sink() > } > else if(i!=1) > { > currentR2 <- slm$r.squared > if (previousR2 > currentR2) > { > currentR2 <- previousR2 > } > if (previousR2 < currentR2) > { > sink(file="R2outputKKNN.txt", append=TRUE) > currentR2 > sink() > } > } > } > > > In my code above, I can't get sink to work. In summary, I'm trying to write > the first run's R2, which is called "previousR2" to file, and then anytime > "currentR2" > "previousR2", I will write "currentR2" to file. After running > the code above, my file R2outputKKNN.txt is empty... > > However, just running the code below writes / works fine: > previousR2 <-slm$r.squared > sink(file="R2outputKKNN.txt", append=TRUE) > previousR2 > sink()
Sorry about that. Still new to this... The code below should be reproducible. All R2 should just be 1, and I should write 1 to R2outputKKNN.txt 10 times....nothing is happening. Appreciate the efforts to help! for (i in 1:10) { adata = 1:5 bdata = 6:10 lm <- lm(adata~bdata) slm <- summary(lm) str(slm) if (i == 1) { previousR2 <-slm$r.squared sink(file="R2outputKKNN.txt", append=TRUE) previousR2 sink() } else if(i!=1) { currentR2 <- slm$r.squared if (previousR2 > currentR2) { currentR2 <- previousR2 } if (previousR2 < currentR2) { sink(file="R2outputKKNN.txt", append=TRUE) currentR2 sink() } } } -- View this message in context: http://r.789695.n4.nabble.com/Help-with-Sink-Function-tp2291705p2291717.html Sent from the R help mailing list archive at Nabble.com.
Your code between calls to sink() does not generate any output. Hence, nothing will be diverted to the file. To illustrate this point, consider for(i in 1:10) i This produces no output. However, for(i in 1:10) print(i) produces output as expected. -Matt On Fri, 2010-07-16 at 13:34 -0400, Addi Wei wrote:> Sorry about that. Still new to this... The code below should be > reproducible. All R2 should just be 1, and I should write 1 to > R2outputKKNN.txt 10 times....nothing is happening. Appreciate the efforts > to help! > > for (i in 1:10) > { > adata = 1:5 > bdata = 6:10 > lm <- lm(adata~bdata) > slm <- summary(lm) > str(slm) > > if (i == 1) { > previousR2 <-slm$r.squared > sink(file="R2outputKKNN.txt", append=TRUE) > previousR2 > sink() } else if(i!=1) > { > currentR2 <- slm$r.squared > if (previousR2 > currentR2) > { > currentR2 <- previousR2 > } > if (previousR2 < currentR2) { > sink(file="R2outputKKNN.txt", append=TRUE) > currentR2 > sink() > } > } > }-- Matthew S. Shotwell Graduate Student Division of Biostatistics and Epidemiology Medical University of South Carolina http://biostatmatt.com