% COMANDOS EM R REFERENTES AO EXEMPLO SOBRE MULTICOLINEARIDADE cimento = read.table("cimento.txt",header=TRUE) attach(cimento) require(robustbase) adjbox(calor,xlab="Calor do Cimento") cor(cimento) plot(x1, calor, ylab="Calor do Cimento, pch=16) lines(smooth.spline(x1,cimento,df=3)) plot(x2, calor, ylab="Calor do Cimento, pch=16) lines(smooth.spline(x2,cimento,df=3)) plot(x3, calor, ylab="Calor do Cimento, pch=16) lines(smooth.spline(x3,cimento,df=3)) plot(x4, calor, ylab="Calor do Cimento, pch=16) lines(smooth.spline(x4,cimento,df=3)) % CENTRALIZANDO AS VARIÁVEIS scalor = calor - mean(calor) sx1 = x1 - mean(x1) sx2 = x2 - mean(x2) sx3 = x3 - mean(x3) sx4 = x4 - mean(x4) fit1.cimento = lm(scalor ~ sx1 + sx2 + sx3 + sx4-1) summary(fit1.cimento) fit.model = fit1.cimento source("envel_norm") source("diag_resid_norm") source("diag_cook_norm") fit2.cimento = lm(scalor ~ sx1 + sx2 + sx3 + sx4-1, subset=-8) require(MASS) require(alr4) vif(fit1.cimento) fit3.cimento = lm(scalor ~ sx1 + sx2 -1) summary(fit3.cimento) X = model.matrix(fit1.cimento) M = t(X)%*%X eigen(M) % REGRESSÃO RIDGE fit4.cimento = lm.ridge(scalor ~ sx1 + sx2 + sx3 + sx4-1, lambda=seq(0, 0.20, 0.001)) matplot(fit4.cimento$lambda, coef(fit4.cimento), type = "l", lty=1, xlab = "k", ylab = "Coeficientes",cex=2,cex.lab=1.5, cex.axis=1.5,lwd=4) text(0.19,1.39,expression(hat(beta)[R1]), cex=2) text(0.19,0.39,expression(hat(beta)[R2]), cex=2) text(0.19,-0.06,expression(hat(beta)[R3]), cex=2) text(0.19,-0.28,expression(hat(beta)[R4]), cex=2) select(lm.ridge(scalor ~ sx1 + sx2 + sx3 + sx4-1, lambda=seq(0, 0.20, 0.001))) fit5.cimento = lm.ridge(scalor ~ sx1 + sx2 + sx3 + sx4-1, lambda=0.076) fit5.cimento k = rep(0.076,4) Ik = diag(k) Z = M + Ik s = summary(fit1.cimento)$sigma s2 = s*s var.ridge.cimento = s2*(solve(Z)%*%M%*%solve(Z)) ep.ridge.cimento = sqrt(diag(var.ridge.cimento)) ep.ridge.cimento % DIVIDir OS COEFICIENTES DE fit5.cimento POR ep.ridge.cimento % COMPONENTES PRINCIPAIS T = eigen(M)$vectors % EXTRAINDO APENAS O PRIMEIRO COMPONENTE T0 = T[,1] Z = X%*%T0 fit6.cimento = lm(scalor ~ Z-1) summary(fit6.cimento) b = fit6.cimento$coef b = b*T0 b % EXTRAINDO OS DOIS PRIMEIROS COMPONENTES T2 = cbind(T[,1],T[,2]) Z = X%*%T2 fit7.cimento = lm(scalor ~ Z-1) summary(fit7.cimento) b = fit7.cimento$coef b = b%*%t(T2) b