# Library of useful functions # to load the library: R> source("library.R") write.data.file <- function(file="data.csv", n=100, min=0, max=1) { # function to generate a set of data and save them to a file x <- runif(n, min, max) write(x, file, 10, FALSE, ", ") } read.data.file <- function(file1="data.csv", sep1=",") { # function to read a set of data from a file x <- scan(file=file1, sep=sep1) return(x) } convergence <- function(n=1e3, opt="mean") { if (opt=="mean") y = mean ( runif(n, 0, 1) ) if (opt=="sd") y = sd ( runif(n, 0, 1) ) return(y) } conv.plot <- function(nmax=1e3, step=1, opt="mean") { n <- seq(1,nmax,step) y <- n for (i in n) { y[i] = convergence(i, opt) } quartz(height=4) plot(n,y, type="l", xlab="tries", ylab="frequency", log="x") } gauss <-function(n=1e3, sigma=1) { quartz(height=4) x<-rnorm(n,sd=sigma) length(x[x>=0]) length(x[x>=-1 & x<=1]) length(x[abs(x)<=1]) mean(x) sd(x) hist(x, nc=50, prob=TRUE) d <- function(z) dnorm(z, 0, sigma) plot(d, -4*sigma, 4*sigma, ty='l', add=TRUE, col='red') fx <- function(z) dnorm(z, mean(x), sd(x)) plot(fx, -4*sigma, 4*sigma, ty='l', add=TRUE, col='brown') }