#setup your working diectory #all the working files should be here #change the path to your computer path #just copy and paste the full path of your working folder #and make suer to change every "\" into "/" setwd('U:/STAT380') #to check where is you current working directory use getwd() #the default working directory is in User/My Documents #generate 1000 samples from binomial distribution with n=100 trials and p of succes =.05 x=rbinom(1000,n=100,prob=0.5) ######################################################################## #save the plot in the file that in the working directory ######################################################################## #start the png device in order to save your plot in a .png image file png('histogram.png') #make a histogram and save the object in a variable h #note: we need prob=TRUE, to get probabiliities not counts in our histogram hist(x,nclass=20,main='Histogram of Standard normal variable',col='purple',prob=T) #get the density of the random variable x d=density(x) #add density estimate of the distribution of the x on the top of the histogram of x lines(d,lwd=2, col='red') abline(v=mean(x),col='green') #shut off the png device, so that you will be able to open the saved .png image file #if the device is not shut properly, you might experience problems with accesing the saved file #and another potential problem might be not showing your plots in your opened R or R Studio instance dev.off() #scatterplot of x plot(x,xlab='x',main='Scatterplot of x') #plot a density of x plot(density(x),main='Distribution of x') ################################################################################## #following is the procedure for installing and #loading an R package. This procedure is general to any R package ################################################################################## #install the package coda using the R command install.packages install.packages('coda') #or you can also go to the 'tools/Install packages' menu in R studio and install the 'coda' package #or package/install packages in R menu #after installing the package, you need to load the package #using the command library library(coda) #make a traceplot of your chain traceplot(as.mcmc(x)) #get quantiles of the distribution of x quantile(x,c(0.25,0.5,0.975)) #save the results in your working directory #if you want to save in directory other than your working directory, giva a full path #for example 'U:/STAT380/x_chain.RData' save(x,file='x_chain.RData') #load the saved results from your woking directory. #give a full path if the file you want to load is in a directory different from your working directory load('x_chain.RData')