Statistical models

Table of Contents

1 Required for this session

install packages called 'faraway';

Download regression.txt

2 Regression analysis

Let us consider the example of species of tortoise on Galapagos islands.

data(gala)
head(gala)
gfit=lm(Species~Area+Elevation+Nearest+Scruz+Adjacent,data=gala)
summary(gfit)
x=cbind(1,gala[,-c(1,2)])
y=gala$Species
t(x) %*% x
X=as.matrix(x)
t(x) %*% X
xtxi=solve(t(x) %*% X)
xtxi
xtxi %*% t(x) %*% y
solve(xtxi %*% t(x) %*% y)
sqrt(sum(gfit$res^2)/(30-6))
sqrt(diag(xtxi))*60.975
1-sum(gfit$res^2)/sum((y-mean(y))^2)

example of applicartion of regression analysis

reg.data=read.table('regression.txt',header=T)
attach(reg.data)
names(reg.data)

plot(tannin,growth,pch=16,ylim=c(0,15))
model=lm(growth~tannin)
abline(model)
/*This plot shows uncertainity associated with the estimation of slope*/
coef(model){2}
 summary(model)[[4]][4]
se.lines<-function(model){
b1<-coef(model)[2]+ summary(model)[[4]][4]
b2<-coef(model)[2]- summary(model)[[4]][4]
xm<-mean(model[[12]][2])
ym<-mean(model[[12]][1])
a1<-ym-b1*xm
a2<-ym-b2*xm
abline(a1,b1,lty=2)
abline(a2,b2,lty=2)
}
se.lines(model)

ci.lines<-function(model){
xm<-mean(model[[12]][2])
n<-length(model[[12]][[2]])
ssx<- sum(model[[12]][2].2)- sum(model[[12]][2]).2/n
s.t<- qt(0.975,(n-2))
xv<-seq(min(model[[12]][2]),max(model[[12]][2]),(max(model[[12]][2])-
min(model[[12]][2]))/100)
yv<- coef(model)[1]+coef(model)[2]*xv
se<-sqrt(summary(model)[[6]].2*(1/n+(xv-xm).2/ssx))
ci<-s.t*se
uyv<-yv+ci
lyv<-yv-ci
lines(xv,uyv,lty=2)
lines(xv,lyv,lty=2)
}

plot(tannin,growth,pch=16,ylim=c(0,15))
abline(model)
ci.lines(model)

Boot strap with regression

regdat<-read.table("c:\\temp\\regdat.txt",header=T)
attach(regdat)
names(regdat)

plot(explanatory,response)

model<-lm(response~explanatory)
model

b.boot<-numeric(10000)
for (i in 1:10000){
indices<-sample(1:35,replace=T)
xv<-explanatory[indices]
yv<-response[indices]
model<-lm(yv~xv)
b.boot[i]<-coef(model)[2] }
hist(b.boot,main="")

quantile(b.boot,c(0.025,0.975))

Author: Srihari <goginenisb@titan.sfasu.edu>

Date: 2009-08-07 12:48:13 CDT

HTML generated by org-mode 6.21b in emacs 23