R 具有特定值的循环不起作用

R 具有特定值的循环不起作用,r,R,我只需要第一个循环的帮助!我想为每一个特定的m值运行循环,请参见代码中的第一行,但它只运行1:10?结果应该存储在最后一行msediff1到msediff100!我还需要每一个m值的图形!提前谢谢 m = c(1,2,3,4,5,6,7,8,9,10,25,50,100) for (m in 1:length(unique(m))){ n <- 150 x1 <- rnorm(n = n, me

我只需要第一个循环的帮助!我想为每一个特定的m值运行循环,请参见代码中的第一行,但它只运行1:10?结果应该存储在最后一行msediff1到msediff100!我还需要每一个m值的图形!提前谢谢

m = c(1,2,3,4,5,6,7,8,9,10,25,50,100)

for (m in 1:length(unique(m))){ 
n <- 150                                           
x1 <- rnorm(n = n, mean = 10, sd = 4)


R <- 100               # Number of reps

results.true <- matrix(NA , ncol = 2, nrow = R)
colnames(results.true) <- c("beta0.hat", "beta1.hat")


results.diff <- matrix(NA, ncol = 2, nrow = R)
colnames(results.diff) <- c("beta0.hat", "betadiff.hat")



sigma <- 1.2                                                                                     
beta <- c(1.2)
X <- cbind(x1)


if (m==1){d0 <- .7071; d  <- c(-.7071)}
if (m==2){d0 = .8090;  d  = c(-.5,-.309)}
if (m==3){d0 = .8582;  d = c(-.3832,-.2809,-.1942) }
if (m==4){d0 = .8873;  d = c(-.3090,-.2464,-.1901,-.1409)}
if (m==5){d0 <- .9064; d <- c(-.2600,-.2167,-.1774,-.1420,-.1103)}
if (m==6){d0 = .92;  d = c(-.2238,-.1925,-.1635,-.1369,-.1126,-.0906)}
if (m==7){d0 = .9302;  d = c(-.1965,-.1728,-.1506,-.1299,-.1107,-.093,-.0768)}
if (m==8){d0 = .9380;  d = c(-.1751,-.1565,-.1389,-.1224,-.1069,-.0925,-.0791,-.0666)}
if (m==9){d0 = .9443;  d = c(-.1578,-.1429,-.1287,-.1152,-.1025,-.0905,-.0792,-.0687,-.0538)}

if (m==10){d0 <-  .9494;
d  <- c(-.1437, -.1314, -.1197, -.1085, -.0978, -.0877, -.0782, -.0691, -.0606, -.0527)}

if (m==25){d0 <-  0.97873;
d  <- c(-0.06128, -0.05915, -0.05705, -0.05500, -0.05298, -0.05100, -0.04906, -0.04715, -0.04528, -0.04345, -0.04166, -0.03990, -0.03818, -0.03650, -0.03486, -0.03325, -0.03168, -0.03015, -0.02865, -0.02719, 
        -0.02577, -0.02438, -0.02303, -0.02171, -0.02043)  }

if (m==50) {d0 <-  0.98918;
d <-  c(-0.03132, -0.03077, -0.03023, -0.02969, -0.02916, -0.02863, -0.02811, -0.02759, -0.02708, -0.02657, -0.02606, -0.02556, -0.02507, -0.02458, -0.02409, -0.02361, -0.02314, -0.02266, -0.02220, -0.02174, -0.02128, -0.02083, -0.02038, -0.01994, -0.01950, -0.01907, -0.01864, -0.01822, -0.01780, -0.01739,-0.01698,-0.01658,-0.01618,-0.01578,-0.01539,-0.01501,-0.01463,-0.01425,-0.01388,-0.01352,
        -0.01316,-0.01280,-0.01245,-0.01210,-0.01176,-0.01142,-0.01108,-0.01075,-0.01043,-0.01011)  }

if (m==100) { d0 <-  0.99454083;
d <- c(-0.01583636,-0.01569757,-0.01555936,-0.01542178,-0.01528478,-0.01514841,-0.01501262,-0.01487745,-0.01474289,-0.01460892,
       -0.01447556,-0.01434282,-0.01421067,-0.01407914,-0.01394819,-0.01381786,-0.01368816,-0.01355903,-0.01343053,-0.01330264,
       -0.01317535,-0.01304868,-0.01292260,-0.01279714,-0.01267228,-0.01254803,-0.01242439,-0.01230136,-0.01217894,-0.01205713,
       -0.01193592,-0.01181533,-0.01169534,-0.01157596,-0.01145719,-0.01133903,-0.01122148,-0.01110453,-0.01098819,-0.01087247,
       -0.01075735,-0.01064283,-0.01052892,-0.01041563,-0.01030293,-0.01019085,-0.01007937,-0.00996850,-0.00985823,-0.00974857,
       -0.00963952,-0.00953107,-0.00942322,-0.00931598,-0.00920935,-0.00910332,-0.00899789,-0.00889306,-0.00878884,-0.00868522,
       -0.00858220,-0.00847978,-0.00837797,-0.00827675,-0.00817614,-0.00807612,-0.00797670,-0.00787788,-0.00777966,-0.00768203,
       -0.00758500,-0.00748857,-0.00739273,-0.00729749,-0.00720284,-0.00710878,-0.00701532,-0.00692245,-0.00683017,-0.00673848,
       -0.00664738,-0.00655687,-0.00646694,-0.00637761,-0.00628886,-0.00620070,-0.00611312,-0.00602612,-0.00593971,-0.00585389,
       -0.00576864,-0.00568397,-0.00559989,-0.00551638,-0.00543345,-0.00535110,-0.00526933,-0.00518813,-0.00510750,-0.00502745) }





for(r in 1:R){
  u  <- rnorm(n = n, mean = 0, sd = sigma)                      
  y <- X%*%beta + u                                                                              

  yy = d0* y[(m+1):n];  Xd <- d0* x1[(m+1):n];  
  for (i in 1:m) { yy <- yy + d[i]* y[(m+1-i):(n-i) ]
  Xd = Xd + d[i]* x1[(m+1-i):(n-i)]  }

  reg.true <- lm(y ~ x1)
  reg.diff <- lm(yy ~ Xd)

  results.true[r, ] <- coef(reg.true)
  results.diff[r, ] <- coef(reg.diff)
}

results.true
results.diff

beta
apply(results.true, MARGIN = 2, FUN = mean)
apply(results.diff, MARGIN = 2, FUN = mean)

co <- 2        
dens.true <- density(results.true[, co])
dens.diff <- density(results.diff[, co])
win.graph()
plot(dens.true, 
     xlim = range(c(results.true[, co], results.diff[, co])), 
     ylim = range(c(dens.true$y, dens.diff$yy)), 
     main = "beta estimation true vs. diff", lwd = 2,)
lines(density(results.diff[, co]), col = "red", lwd = 2)
abline(v = beta, col = "blue", lwd = 2)
legend(x=1.24,y=12,c("outcome true","outcome diff"),lty=c(1,1),col =c("black","red") )
legend(x=1.12,y=12,c("m=",m))

#Mean Squared Error

mse=mean(reg.true$residuals^2)

if (m==1) {msediff1=mean(reg.diff$residuals^2)}
if (m==2) {msediff2=mean(reg.diff$residuals^2)}
if (m==3) {msediff3=mean(reg.diff$residuals^2)}
if (m==4) {msediff4=mean(reg.diff$residuals^2)}
if (m==5) {msediff5=mean(reg.diff$residuals^2)}
if (m==6) {msediff6=mean(reg.diff$residuals^2)}
if (m==7) {msediff7=mean(reg.diff$residuals^2)}
if (m==8) {msediff8=mean(reg.diff$residuals^2)}
if (m==9) {msediff9=mean(reg.diff$residuals^2)}
if (m==10) {msediff10=mean(reg.diff$residuals^2)}
if (m==25) {msediff25=mean(reg.diff$residuals^2)}
if (m==50) {msediff50=mean(reg.diff$residuals^2)}
if (m==100) {msediff100=mean(reg.diff$residuals^2)}
}

我可以在代码中看到一个错误

m = c(1,2,3,4,5,6,7,8,9,10,25,50,100)
for (m in 1:length(unique(m))){
循环一开始,m就改变了。这不再是第一行的内容了。。。 试试看,对于ind在1:lengthuniquem{如果这不是目的的话