################################
### Example 38
################################
### unweighted regression ###
#############################

x<-c(0,2,4,6,8,10)
x
y<-c(0.009,0.158,0.301,0.472,0.577,0.739)
   
M<-lm(y~x)
M
anova(M)
summary(M)

################################
### weighted regression ###
############################# 

Si<-c(0.001,0.004,0.01,0.013,0.017,0.022)
Si
cbind(x,y,Si)

wi<-Si^(-2)/mean(Si^(-2))
wi
sum(wi)

M1<-lm(y~x, weights=wi)
M1

################################
### Example 39
x<-rep(c(0,10,20,30,40,50),each=5)
x
y<-c(4,3,4,5,4,22,20,21,22,21,44,46,45,44,44,60,63,60,63,63,
   75,81,79,78,77,104,109,107,101,105)
   
M<-lm(y~x)
M
anova(M)
summary(M)
################################
### weighted regression ###
############################# 
S<-aggregate(y~x,FUN=sd)
S
Si<-rep(S$y,each=5)
Si
cbind(x,y,Si)
wi<-Si^(-2)/mean(Si^(-2))
wi
sum(wi)
M1<-lm(y~x, weights=wi)
M1
#######################example40######################################
x=c(777,790,759,790,770,758,764)
y=c(782,773,778,765,789,797,782)
mean(x)
mean(y)
sd(x)
sd(y)
t.test(x,y,alternative="two.sided",conf.level=0.95,var.equal = TRUE)
var.test(x, y, alternative = "two.sided")
qt(0.975,12)

#######################example41######################################
x=c(0.48,0.36,0.2,0.55,0.45,0.46,0.47,0.23)
y=c(0.35,0.37,0.27,0.29)
mean(x)
mean(y)
sd(x)
sd(y)
t.test(x,y,alternative="two.sided",conf.level=0.95,var.equal = TRUE)
var.test(x, y, alternative = "two.sided")


#######################example43######################################
x=c(22.2,19.2,15.7,20.4,19.6,15.7)
y=c(25,19.5,16.6,21.3,20.7,16.8)
d=x-y
d
sum(d)
mean(x-y)
sd(x-y)

t.test(x,y,paired=TRUE, alternative="two.sided",conf.level=0.95,var.equal = FALSE)


#######################example45-46-47######################################
x1=c(91.5,93.8,93.4,92.5,94.8,93.2,93.7,91.5,92.7,92.7)
x2=c(56.1,56.3,56.4,56.7,56.5,56.8,57,56.8,55.7,57.7)
x3=c(73.6,74.1,74.5,73.7,73.6,73.9,74.4,73.9,73.9,73.8)
y=c(0.888,0.461,0.453,0.56,0.414,0.438,0.342,0.743,0.751,0.477)
m=lm(y~x1+x2+x3)
anova(m)
summary(m)
qf(0.95,3,6)
betahat<-coef(m)
r=resid(m)
r
sr=rstandard(m)
sr
yhat=fitted(m)
yhat

confint(m)
shapiro.test(m$res)
qqnorm(m$res)
qqline(m$res)

hist(sr)
boxplot(m$res)
#######################example48######################################
x1=c(2,-1,1,2,1)
x2=c(1,2,-3,1,4)
x3=c(4,1,4,2,6)
y=c(8,10,9,6,12)

m=lm(y~-1+x1+x2+x3)
anova(m)
summary(m)
qf(0.95,3,2)

####################### Randomized block design -50##################

y<-c(84,79,83,80,77,78,83,80,80,79,79,78)
A<-factor(rep(1:4,each=3))
A
Block<-factor(rep(1:3,4))
Block
y<-y-80
y
aggregate(y~A,FUN=sum)
aggregate(y~Block,FUN=sum)
sum(y)

M<-aov(y~A+Block)
anova(M)
qf(0.95,3,6)
#########################example49#########
x=matrix(scan(),byrow=T,ncol=3)
1 -2 0
-2 5 0
0 0 2

e=eigen(x)
e
e$values[1]/(e$values[1]+e$values[2]+e$values[3])
e$values[2]/(e$values[1]+e$values[2]+e$values[3])