sorry confusing title, wasn't sure how title trying do. objective create dataset of 1000 obs each length of run. have created phase1 dataset, set of control limits produced. trying create phase2 dataset using rnorm. im trying create repeat loop continuously create values in phase2 dataset until 1 of values outside of control limits produced phase1 dataset. example if had 3.0 , -3.0 control limits phase2 dataset create bunch of observations until obs 398 when value here happens 3.45, stopping creation of data. objective record number 398. furthermore, trying loop code phase1 dataset/ control limits portion , create new set of control limits , run phase2, until have 1000 run lengths recorded. code have phase1/ control limits works fine , looks this:
nphase1=50 nphase2=1000 varcount=1 meanshift= 0 sigmashift= 1 ##### phase1 dataset/ control limits ##### phase1 <- matrix(rnorm(nphase1*varcount, 0, 1), nrow = nphase1, ncol=varcount) mean_var <- apply(phase1, 2, mean) std_var <- apply(phase1, 2, sd) df_var <- data.frame(mean_var, std_var) upper_spc_limit_method1 <- with(df_var, mean_var + 3 * std_var) lower_spc_limit_method1 <- with(df_var, mean_var - 3 * std_var) df_control_limits<- data.frame(upper_spc_limit_method1, lower_spc_limit_method1) i have created code in sas , looks this. might better reference trying achieve me trying explain it.
%macro phase2_dataset (n=,varcount=, meanshift=, sigmashift=, nphase1=,simid=,); %do z=1 %to &n; %phase1_dataset (n=&nphase1, varcount=&varcount); data phase2; set control_limits n=lastobs; call streaminit(0); until (phase2_var1<lower_spc_limit_method1_var1 or phase2_var1>upper_spc_limit_method1_var1); phase2_var1 = rand("normal", &meanshift, &sigmashift); output; end; run; ods exclude all; proc means data=phase2; var phase2_var1; ods output summary=x; run; ods select all; data run_length; set x; keep phase2_var1_n; run; proc append base= qa.phase2_dataset&simid data=run_length force; run; %end; %mend; also been doing research using while loop in replace of repeat loop. im new r ideas able throw way appreciated. thanks!
using while loop indeed seems way go. here's think you're looking for:
set.seed(10) #making results reproducible replicate(100, { #100 easier display here phase1 <- matrix(rnorm(nphase1*varcount, 0, 1), nrow = nphase1, ncol=varcount) mean_var <- colmeans(phase1) #slightly better apply std_var <- apply(phase1, 2, sd) df_var <- data.frame(mean_var, std_var) upper_spc_limit_method1 <- with(df_var, mean_var + 3 * std_var) lower_spc_limit_method1 <- with(df_var, mean_var - 3 * std_var) df_control_limits<- data.frame(upper_spc_limit_method1, lower_spc_limit_method1) #phase 2 x <- 0 count <- 0 while(x > lower_spc_limit_method1 && x < upper_spc_limit_method1) { x <- rnorm(1) count <- count + 1 } count }) the result is:
[1] 225 91 97 118 304 275 550 58 115 6 218 63 176 100 308 844 90 2758 [19] 161 311 1462 717 2446 74 175 91 331 210 118 1517 420 32 39 201 350 89 [37] 64 385 212 4 72 730 151 7 1159 65 36 333 97 306 531 1502 26 18 [55] 67 329 75 532 64 427 39 352 283 483 19 9 2 1018 137 160 223 98 [73] 15 182 98 41 25 1136 405 474 1025 1331 159 70 84 129 233 2 41 66 [91] 1 23 8 325 10 455 363 351 108 3 if performance becomes problem, perhaps interesting explore improvements, creating more numbers rnorm() @ time , counting how many necessary exceed limits , repeat if necessary.
Comments
Post a Comment