########################################################## ## ## ## Simulation study for "Estimating the retransformed ## ## mean in a heteroscedastic two-part model" by Welsh ## ## and Zhou. ## ## ## ## Developed by Hao Cheng for Andrew Zhou ## ## Date: December 10, 2004 ## ## ## ########################################################## #----- clear environment ----- rm(list=ls()) print(Sys.time()) ########################################################## ## ## ## simulation setting ## ## ## ########################################################## #----- input data, generate covariates and response ----- inputfile <- "" while(inputfile=="") { cat("Please specify the name of input data file: ") inputfile <- readLines(n=1) inputfile <- gsub(" ", "", inputfile) } if(!file.exists(inputfile)) stop("File ", inputfile, " does not exist. Stop!") dataset <- as.matrix(read.table(inputfile)) X <- cbind(1, dataset[,-1]) Y <- as.vector(dataset[,1]) #----- sample size ----- nSample <- nrow(X) #----- number of covariates ----- # nCovariate = 1 + (number of actual covariates) # covariates are given in format (1, covariate_1, ..., covariate_n), # where n = nCovariate - 1 nCovariate <- ncol(X) #----- number of data sets ----- nDataset <- 1 #----- option on estimating parameters beta and gamma ----- # option.bg = 0: maximum-likelihood estimator, solve estimating equation # option.bg = 1: maximum-likelihood estimator, maximize log-likelihood function cat("\n") cat("Please choose method for estimating parameters beta and theta\n") cat(" 1: MLE estimator, maximize log-likelihood function (default)\n") cat(" 2: MLE estimator, solve estimating equation\n") option.bg <- readLines(n=1) option.bg <- as.numeric(gsub(" ", "", option.bg)) if(is.na(option.bg) | (option.bg!=1 & option.bg!=2)) { option.bg<-1 cat("Not a valid selection. Use the default method 1.\n") } #----- real parameter values ----- # For real data, they are initial guess of parameters # For simulation, they are real parameter values used to generate data sets cat("\n") cat("Do you want to give initial guess of parameters (default: all zeros)? (Y/N)") answer <- readLines(n=1) bStop <- FALSE while(!bStop) { answer <- gsub(" ", "", answer) answer <- unlist(strsplit(answer, split=NULL))[1] if(is.na(answer)) answer<-' ' if(answer=='n' | answer=='N') { bStop <- TRUE bInitpar <- FALSE } else if(answer=='y' | answer=='Y') { bStop <- TRUE bInitpar <- TRUE } else { cat("Unknown answer. Please input 'y' or 'n': ") answer <- readLines(n=1) } } if(bInitpar) { cat("\n") cat("Please input initial guess of parameter alpha:\n") alpha.0 <- scan(quiet=TRUE, n=nCovariate) temp <- length(alpha.0) if(temp0] Xbeta <- X[, XbetaIndicator>0] Xgamma <- X[, XgammaIndicator>0] ## ## estimate parameters ## alpha.init <- alpha.0[XalphaIndicator>0] beta.init <- beta.0[XbetaIndicator>0] gamma.init <- gamma.0[XgammaIndicator>0] par <- estpar(Y, Xalpha, Xbeta, Xgamma, alpha.init, beta.init, gamma.init) parfile <- paste(unlist(strsplit(inputfile,".",fixed=TRUE))[1], "_par.dat", sep="") sink(parfile) outpar(par) sink() resultfile <- paste(unlist(strsplit(inputfile,".",fixed=TRUE))[1], "_result.dat", sep="") file.remove(resultfile) sink(resultfile) cat("Source data file: ", inputfile, "\n") cat("Number of observations: ", nSample, "\n") cat("Number of covariates: ", nCovariate-1, "\n") cat("----------------------------------------------------------\n\n") sink() #----- covariates value ----- #cat("\n") #cat("Please input covariates values:\n") #incov <- scan(quiet=TRUE, n=nCovariate-1) #cov <- c(1, incov) #estmean(par, result, tscore) #cat("\n") #cat("Do you want to see estimation results for another covariate? (Y/N) ") answer <- 'Y' bStop <- FALSE while(!bStop) { answer <- gsub(" ", "", answer) answer <- unlist(strsplit(answer, split=NULL))[1] if(is.na(answer)) answer<-' ' if(answer=='n' | answer=='N') { bStop <- TRUE } else if(answer=='y' | answer=='Y') { cat("\n") cat("Please input covariates values:\n") incov <- scan(quiet=TRUE, n=nCovariate-1) temp <- length(incov) if(temp