这些天,我和R和RStudio有问题。 R脚本无法在RStudio中运行,它经常被中断。但它可以在原始控制台中成功运行。这是脚本:
library(MASS)
simulation <- function(index) {
n = 800 #100 200 400 800 1600 3200
p = 0.5 * n #0.5*n 0.8*n 1.2*n
delta = 0.5 #0.5
k0 = 1
y = array(0, dim = c(p, n))
# simulation1 r=3
# ---------------------------------------------------------
r = 4
A = array(runif(p * r, min = -1, max = 1), dim = c(p, r))
x = array(0, dim = c(r, n)) #x = array(0, dim = c(p, n))
epsilon = array(t(mvrnorm(n, mu = rep(0, p), Sigma = diag(1, p, p))),
dim = c(p, n))
e = array(t(mvrnorm(n, mu = rep(0, r), Sigma = diag(1, r, r))), dim = c(r,
n))
Pi = diag(c(0.6, -0.5, 0.3, 0.6), r, r)
for (i in 2:n) {
x[, i] = Pi %*% x[, i - 1, drop = FALSE] + e[, i]
}
A[, 2] = A[, 2]/p^(delta/2) # weak factor
y1 = A %*% x + epsilon
y = t(scale(t(y1), center = T, scale = FALSE))
Mhat = array(0, dim = c(p, p))
for (i in 1:k0) {
covk = y[, (i + 1):n, drop = FALSE] %*% t(y[, 1:(n - i), drop = FALSE])/(n -
i)
Mhat = Mhat + covk %*% t(covk)
}
R = p/2
ratio = array(0, dim = c(R, 1))
temp = eigen(Mhat)
value = temp$values
vector = temp$vectors
for (i in 1:R) {
ratio[i] = value[i + 1]/value[i]
}
plot(ratio, type = "l")
rhat1 = which.min(ratio)
Ahat1 = vector[, 1:rhat1, drop = FALSE]
# two step
ystar = y1 - Ahat1 %*% t(Ahat1) %*% y1
ystar = t(scale(t(ystar), center = T, scale = FALSE))
Mhatstar = array(0, dim = c(p, p))
for (i in 1:k0) {
covk = ystar[, (i + 1):n, drop = FALSE] %*% t(ystar[, 1:(n - i),
drop = FALSE])/(n - i)
Mhatstar = Mhatstar + covk %*% t(covk)
}
temp1 = eigen(Mhatstar)
valuestar = temp1$values
vector1 = temp1$vectors
ratiostar = array(0, dim = c(R, 1))
for (i in 1:R) {
ratiostar[i] = valuestar[i + 1]/valuestar[i]
}
plot(ratiostar, type = "l")
rhat2 = which.min(ratiostar)
# Ahat2 = vector1[, 1:rhat2, drop = FALSE]
rhat = rhat1 + rhat2
# Ahat = cbind2(Ahat1, Ahat2)
return(rhat)
}
result=replicate(200,simulation())
程序经常被打断。我不知道为什么。
我的桌面是:win7 RStudio版本:0.98.426 日志文件是:
15 Oct 2013 11:02:50 [rsession-Administrator] WARNING Abort requested; LOGGED FROM: bool session::connection::checkForAbort(boost::shared_ptr<session::HttpConnection>, boost::function<void()>) C:\Users\Administrator\rstudio\src\cpp\session\http\SessionHttpConnectionUtils.cpp:146
15 Oct 2013 11:03:00 [rsession-Administrator] ERROR r error 5 (R symbol not found) [symbol (option)=ggvis.renderer]; OCCURRED AT: T r::options::getOption(const std::string&, const T&) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >] C:\Users\Administrator\rstudio\src\cpp\r\include/r/ROptions.hpp:73; LOGGED FROM: T r::options::getOption(const std::string&, const T&) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >] C:\Users\Administrator\rstudio\src\cpp\r\include/r/ROptions.hpp:75