dir()
library (MASS)
yc < - meatspec$fat[1:172]-mean(meatspec$fat[1:172])
gridge < - lm.ridge (yc ˜ trainx, lambda =
seq(0,5e!8,le–9))#
matplot (gridge$lambda, t(gridge$coef), type="l",lty=1,
xlab=expression (lambda), ylab=expression (hat (beta)))
data (meatspec)
library(faraway)
library(help=faraway)
library(faraway)
data (meatspec)
yc < - meatspec$fat[1:172]-mean(meatspec$fat[1:172])
gridge < - lm.ridge (yc ˜ trainx, lambda =
seq(0,5e!8,le–9))#
matplot (gridge$lambda, t(gridge$coef), type="l",lty=1,
xlab=expression (lambda), ylab=expression (hat (beta)))
yc < - meatspec$fat[1:172]-mean(meatspec$fat[1:172])
meatspec$fat[1:172]-mean(meatspec$fat[1:172])
yc <-meatspec$fat[1:172]-mean(meatspec$fat[1:172])
gridge <-lm.ridge (yc ˜ trainx, lambda =
seq(0,5e!8,le–9))#
matplot (gridge$lambda, t(gridge$coef), type="l",lty=1,
xlab=expression (lambda), ylab=expression (hat (beta)))
yc <-meatspec$fat[1:172]-mean(meatspec$fat[1:172])
gridge <-lm.ridge (yc~trainx, lambda =seq(0,5e-8,1e-9))
attach(meatspec)
yc <-meatspec$fat[1:172]-mean(meatspec$fat[1:172])
gridge <-lm.ridge (yc~trainx, lambda =seq(0,5e-8,1e-9))
names(meanspec)
names(meatspec)
trainx<- as.matrix (sweep (meatspec [1:172,-101] , 2, mm))
mm<-apply (meatspec[1:172, -101], 2, mean)
trainx<- as.matrix (sweep (meatspec [1:172,-101] , 2, mm))
gridge <-lm.ridge (yc~trainx, lambda =seq(0,5e-8,1e-9))
matplot (gridge$lambda, t(gridge$coef), type="l",lty=1,
xlab=expression (lambda), ylab=expression (hat (beta))
matplot (gridge$lambda, t(gridge$coef), type="l",lty=1,
xlab=expression (lambda), ylab=expression (hat (beta)))
A <- array(1:24, dim = 4:2)
A
A <- array(1:24, dim = 4:2)#
#
## no warnings in normal use#
sweep(A, 1, 5)#
(A.min <- apply(A, 1, min)) # == 1:4#
sweep(A, 1, A.min)#
sweep(A, 1:2, apply(A, 1:2, median))
med.att <- apply(attitude, 2, median)
med.att <- apply(attitude, 2, median);med.att
sweep(data.matrix(attitude), 2, med.att)# subtract the column medians
select (gridge)
abline (v=l.8e–8)
abline (v=1.8e-8)
which.min (gridge$GCV)
ypredg < - scale (trainx, center=FALSE,scale=gridge$scales) %*% gridge$coef [, 19] + mean(meatspec$fat[1:172])
rmse(ypredg,meatspec$fat[1:172])
ypredg <-scale (trainx, center=FALSE,scale=gridge$scales) %*% gridge$coef [, 19] + mean(meatspec$fat[1:172])
rmse(ypredg,meatspec$fat[1:172])
rmse <- function (x, y) sqrt (mean ( (x-y)ˆ2) )
rmse <-function(x, y) sqrt (mean((x-y)^2))
rmse(ypredg,meatspec$fat[1:172])
ytpredg <-scale (testx, center=FALSE, scale=gridge$scales)%*% gridge$coef [, 19] + mean(meatspec$fat[1:172])
rmse (ytpredg, meatspec$fat[173:215])
testx <-as.matrix (sweep (meatspec [173:215,-101], 2, mm))
ytpredg <-scale (testx, center=FALSE, scale=gridge$scales)%*% gridge$coef [, 19] + mean(meatspec$fat[1:172])
rmse (ytpredg, meatspec$fat[173:215])
c (ytpredg [13], ytpred [13], meatspec$fat [172+13])
c(ytpredg[13], ytpred[13], meatspec$fat [172+13])
ytpredg <-scale (testx, center=FALSE, scale=gridge$scales)%*% gridge$coef [,19] + mean(meatspec$fat[1:172])
rmse (ytpredg, meatspec$fat[173:215])
c(ytpredg[13], ytpred[13], meatspec$fat [172+13])
plsbeta <-plsg$training$B[,,14]
ytpred<-testx %*% plsbeta + mean(meatspec$fat[1:172])
rmse(ytpred,meatspec$f at [173:215] )
plsg <-pls(trainx,meatspec$fat[1:172] ,1:50,validation="CV",grpsize=10)
library (pls.pcr)
library (pls)
trainx<- as.matrix (sweep (meatspec [1:172,-101] , 2, mm))#
pcrg <-pcr(trainx,meatspec$fat[1:172] ,1:50,validation="CV", grpsize=10)
plsg <-pls(trainx,meatspec$fat[1:172] ,1:50,validation="CV",grpsize=10)
library (pls)
plsg <-pls(trainx,meatspec$fat[1:172] ,1:50,validation="CV",grpsize=10)
library(help=pls)
yc <-meatspec$fat[1:172]-mean(meatspec$fat[1:172])#
mm<-apply (meatspec[1:172, -101], 2, mean)#
trainx<- as.matrix (sweep (meatspec [1:172,-101] , 2, mm))
testx <-as.matrix (sweep (meatspec [173:215,-101], 2, mm))
gridge <-lm.ridge (yc~trainx, lambda =seq(0,5e-8,1e-9))#
matplot (gridge$lambda, t(gridge$coef), type="l",lty=1,
xlab=expression (lambda), ylab=expression (hat (beta)))#
select (gridge)
abline (v=1.8e-8)
which.min (gridge$GCV)#
ypredg <-scale (trainx, center=FALSE,scale=gridge$scales)%*% gridge$coef [, 19] + mean(meatspec$fat[1:172])
rmse(ypredg,meatspec$fat[1:172])#
ytpredg <-scale (testx, center=FALSE, scale=gridge$scales)%*% gridge$coef [,19] + mean(meatspec$fat[1:172])
rmse (ytpredg, meatspec$fat[173:215])
c(ytpredg[13], ytpred[13], meatspec$fat [172+13])
rmse (ytpredg[!13], meatspec$fat[173:215] [-13])
rmse (ytpredg[-13], meatspec$fat[173:215] [-13])
library(faraway)#
data (seatpos)
g <-lm(hipcenter~. , seatpos)
summary(g)
round(cor(seatpos),3)
summary (lm(x [, 1]~x[,-1]))$r.squared
summary (lm(x [,1]~x[,-1]))$r.squared
x<-model.matrix(g)[,-1]
e<-eigen(t(x) %*% x)
e$val
round(e$val,5)
sqrt(e$val[1]/e$val)
summary (lm(x[,1]~x[,-1]))$r.squared
vif(x)
g<-lm (hipcenter+10*rnorm(38) ~ . , seatpos)
summary(g)
round(cor(x [ ,3:8]),2)
g2<-lm (hipcenter ~Age + Weight + Ht, seatpos)
summary (g2)
