Está en la página 1de 2

library(car)

library(perturb)
library(leaps)

#LEER DATOS EN APC1modif.TXT


datos=read.table(file.choose(),header=T)
#CONSULTA DE NOMBRE DE VARIABLES EN OBJETO datos
names(datos)
#CREANDO UN MARCO DE DATOS EXCLUYENDO LAS OBSERVACIONES 47 Y 112
datos3=datos[-c(47,112),]
attach(datos3)

###REGRESION LINEAL CON VARIABLES INDICADORAS (DE LA VARIABLE REGION)


#DEFINIENDO COMO FACTOR A LA VARIABLE REGION
#Y SELECCIONANDO SU NIVEL 4 COMO EL NIVEL DE REFERENCIA
REGION=as.factor(REGION)
REGION=relevel(REGION,ref="4")
#AJUSTANDO MODELO GENERAL

modeloRINF2=lm(DPERM~REGION+RINF:REGION)

modeloRINF=lm(DPERM~RINF*REGION)
summary(modeloRINF)

modeloRINF2=lm(DPERM~RINF+RINF:REGION)
summary(modeloRINF2)

anova(modeloRINF)

plot(modeloRINF)

par(mfrow=c(1,1))
plot(modeloRINF,pch=1:3,col=1:3,cex=2,cex.lab=1.5)
Legend("topleft",legend=c("A","B","C"),pch=c(1:3),col=c(1:3),cex=2)

par(mfrow=c(1,1))
names(datos3)
plot(RINF,DPERM,pch=1:4,col=1:4,cex=2,cex.lab=1.5)

#recta para la R1
abline(a=7.13,b=0.72,col=1,pch=1,lwd=2)
# Recta para la R2
abline(a=7.13,b=0.56,col=2,pch=2,lwd=2)
#Recta para la R3
abline(a=7,13,b=0,52,col=3,pch=3,lwd=2)
#Recta para la R4
abline(a=7,13,b=0,21,52,col=4,pch=3,lwd=2)

#REALIZACI�ON DEL TEST LINEAL GENERAL PEDIDO EN 7.


library(car)
names(coef(modeloRINF))

#Observe nombre de los t�rminos en modeloRINF a ser


#usados en la especificaci�n del test lineal
linearHypothesis(modeloRINF,c("REGION2=REGION3","RINF:REGION2=RINF:REGION3"))

miscoeficientes=function(modeloreg,datosreg){
coefi=coef(modeloreg)
datos2=as.data.frame(scale(datosreg))
coef.std=c(0,coef(lm(update(formula(modeloreg),~.+0),datos2)))
limites=confint(modeloreg,level=0.95)
vifs=c(0,vif(modeloreg))
resul=data.frame(Estimacin=coefi,Limites=limites,Vif=vifs,Coef.Std=coef.std)
cat("Coeficientes estimados, sus I.C, Vifs y Coeficientes estimados
estandarizados","\n")
resul
}

names(datos)

attach(datos)

datos3=datos[-c(47,112),]
attach(datos3)

m1=lm(DPERM~EDAD+RINF+RRX+PDP)

summary(m1)

anova(m1)

miscoeficientes(m1,datos)

También podría gustarte