## Exercise 1 ## c2f <- function(T, inverse=FALSE){ if (inverse){ tmp <- (T - 32) * (5/9) } else { tmp <- (T * (9/5)) + 32 } return(tmp) } c2f(212, inverse=TRUE) ## Exercise 2 ## myfib <- function(n){ st <- c(1,1) mysum <- st[2] if (n>=3){ for (i in 3:round(n,0)){ mysum <- st[1] + st[2] st <- c(st[2],mysum) } } else { if (n<=0){ stop("Cannot compute Fibonacci series for a negative number") } } return(st[2]) } ## Exercise 3 ## ckprime <- function(n){ n <- abs(round(n,0)) flag <- FALSE i <- 2 while(i < n & !flag){ if (n%%i==0){ print(i) flag <- TRUE } i <- i + 1 } if (flag){ tmp <- paste(n, "is not a prime number.") } else { tmp <- paste(n, "is a prime number.") } return(flag) } # Optimize, do not check for even numbers ckprime2 <- function(n){ n <- abs(round(n,0)) flag <- FALSE if (n%%2==0 & n!=2){ flag <- TRUE } else { i <- 3 while(i < n & !flag){ if (n%%i==0) flag <- TRUE i <- i + 2 } } if (flag){ tmp <- paste(n, "is not a prime number.") } else { tmp <- paste(n, "is a prime number.") } return(flag) } system.time(ckprime(7919)) system.time(ckprime2(7919)) ## Ex 4 ## get.divisors <- function(n){ # Is the input 0? if (n == 0){ mydiv <- 0 # Divisors should be 0 } else { mydiv <- 1 # Initialize the divisors vector for (i in 2:(n-1)){ tmp <- ckprime2(i) # Check if the divisor is a prime number if (!tmp){ if (all.equal(n%%i, 0) == TRUE) mydiv <- c(mydiv,i) # Add only divisors } } } return(mydiv) } ckperfect <- function(n){ if (n%%1!=0){ stop("Given a float number, integer is needed!") } else { N <- n if (n<0) # Check for the sign N <- abs(n) # Get the divisors mydiv <- get.divisors(N) # Check equality on the sum if (abs(sum(mydiv) - N)<=1e-10){ tmp <- TRUE } else { tmp <- FALSE } # Return TRUE if it is a perfect number, otherwise return FALSE return(tmp) } } ckperfect(6) ckperfect(8) ckperfect(0.3) ## Excercise 6 par(mfrow=c(2,1)) ## Varying the mean aa <- sapply(seq(-3,3,length=8),function(x){ x <- rnorm(1000,mean=x,sd=1) return(x) }) bb <- matrix(0,ncol=8,nrow=1000) for (i in seq(-3,3,length=8)){ bb[,i] <- rnorm(1000,mean=i,sd=1) } aa <- sapply(seq(-3,3,length=8),rnorm, mean=x) mycol <- terrain.colors(8) for (i in 1:ncol(aa)){ if (i == 1){ plot(density(aa[,i]),xlim=c(min(aa),max(aa)), col=mycol[i], main="Mean in [-3,3]") abline(v=mean(aa[,i]),lty=2, col=mycol[i]) } else { lines(density(aa[,i]),xlim=c(min(aa),max(aa)), col=mycol[i]) abline(v=mean(aa[,i]),lty=2, col=mycol[i]) } } ## Varying the variance aa <- lapply(seq(0.1,2,length=8),function(x){ x <- density(rnorm(1000,m=0,sd=x)) return(x) }) lim <- sapply(aa,function(x){c(min(x$x), max(x$x),min(x$y), max(x$y))}) lim <- c(min(lim[1,]), max(lim[2,]),min(lim[3,]), max(lim[4,])) for (i in 1:length(aa)){ if (i == 1){ plot(aa[[i]]$x,aa[[i]]$y, col=mycol[i],main="Sd in [0.1,2]",type="l", xlim=lim[1:2], ylim=lim[3:4], xlab="x", ylab="Density") } else { lines(aa[[i]]$x, aa[[i]]$y, col=mycol[i]) } } # Solution 2 ma <- lapply(seq(-3,3,length=8),function(x){ x <- rnorm(1000,m=x,sd=1) return(x) }) sa <- lapply(seq(0.1,2,length=8),function(x){ x <- rnorm(1000,m=0,sd=x) return(x) }) df <- data.frame(X=c(unlist(ma),unlist(sa)), Y=c(unlist(ma),unlist(sa)) , SD=c(rep(1,1000*8), rep(seq(0.1,2,length=8), each=1000)), M=c( rep(seq(-3,3,length=8), each=1000), rep(0,1000*8)), PLFLAG=c(rep("Mean",1000*8), rep("Sd", 1000*8))) library(lattice) densityplot(~X|PLFLAG, groups=SD+M, data=df,plot.points=FALSE, layout=c(1,2), scales=list(relation="free"), as.table=TRUE) ## Exercise 7 mycol <- rev(heat.colors(30)[seq(1,20,2)]) for (i in seq(1,5,length=10)){ n <- 10^i tmp <- rnorm(n) if (i == 1){ plot(density(tmp), ylim=c(0,0.6), col=mycol[i]) } else { lines(density(tmp), col=mycol[i]) } } ## Exercise 8 data(Pima.tr, package="MASS") table(Pima.tr$type) library(lattice) library(latticeExtra) hh <- histogram(~age+bmi+glu|type, data=Pima.tr, scales=list(relation="free")) useOuterStrips(hh)