# 1. Function in R x <- rnorm(100) plot(density(a)) x <- runif(100) x <- rnorm(100,mean=1,sd=2) f1 <-function(z){ return(mean(z)/sd(z)) } f1(x) f2 <-function(z){ mu <- mean(z) va <- var(z) qt <- quantile(z,probs=seq(0.1,0.9,0.1)) return(list(mean = mu, var = va, quantile = qt)) } a <- f2(x) a$mean a$var a$quantile # 2. F-test by an R function D <- read.table("US90.txt",header = T) attach(D) long <-lm(log(gdpgr)~log(invgr)+log(consgr)+log(producgr)+log(unemp)+log(inf)) short <-lm(log(gdpgr)~log(unemp)+log(inf)) # Below, 'u' is the unrestricted model and 'r' is the restricted model F.test<-function(u,r){ k <- length(coef(u)) n <- length(resid(u)) eeu<- sum(resid(u)^2) eer<- sum(resid(r)^2) kr <- length(coef(r)) q <- k-kr #q is the number of restrictions Fstat <- ((eer-eeu)/q)/(eeu/(n-k)) #F-statistic Fprob <- 1-pf(Fstat, q, n-k) #P-value return(list(Fstat=Fstat, Fprob=Fprob)) } F.test(long,short) # 3. Box-Cox data <- read.table("gasnew.dat",header=T) attach(data) y<-ln.y. # per capita consumption p<-ln.p. # real price z<-ln.z. # per capita income p2<-p^2 pz<-p*z f<-lm(y~p+z+p2+pz) summary(f) library(MASS) Y<-exp(y) g<-boxcox(Y~p+z+p2+pz, lambda =c(-1, -0.5,-0.25,0,0.25,0.5,1)) lambda0 <- 1 j0 <- which(g$x == lambda0) j.star<-which.max(g$y) lambda.star<-g$x[j.star] test.stat <- 2*(g$y[j.star]-g$y[j0]) test.stat lambda0 <- 0 j0 <- which.min(abs(g$x - lambda0)) j.star<-which(g$y == max(g$y)) test.stat <- 2*(g$y[j.star] - g$y[j0]) test.stat # alternatively try approx(g$x,g$y,xout=0) test.stat <- 2*(g$y[j.star] - approx(g$x,g$y,xout=0)$y)