#carga de datos

library(wooldridge)
## Warning: package 'wooldridge' was built under R version 4.0.5
data(hprice1)
head(force(hprice1),n=5)
##   price assess bdrms lotsize sqrft colonial   lprice  lassess llotsize   lsqrft
## 1   300  349.1     4    6126  2438        1 5.703783 5.855359 8.720297 7.798934
## 2   370  351.5     3    9903  2076        1 5.913503 5.862210 9.200593 7.638198
## 3   191  217.7     3    5200  1374        0 5.252274 5.383118 8.556414 7.225482
## 4   195  231.8     3    4600  1448        1 5.273000 5.445875 8.433811 7.277938
## 5   373  319.1     4    6095  2514        1 5.921578 5.765504 8.715224 7.829630
modeloPrecio<-lm(formula = price~lotsize+sqrft+bdrms,data = hprice1)
Xmat<-model.matrix(modeloPrecio)
XXmat<-t(Xmat)%*%Xmat

#Prueba de Durwin Watson

library(lmtest)
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.0.5
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
dwtest(modeloPrecio,alternative="two.side",iterations=1000)
## 
##  Durbin-Watson test
## 
## data:  modeloPrecio
## DW = 2.1098, p-value = 0.6218
## alternative hypothesis: true autocorrelation is not 0
#se concluye que se rechaza la presencia de autocorrelacion ya que Pvalue>0.05

#Prueba de multiplicador de Lagrange

library(lmtest)
bgtest(modeloPrecio,order = 1)
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  modeloPrecio
## LM test = 0.39362, df = 1, p-value = 0.5304
#se concluye que Pvalue>0.05, hay evidencia que el modelo no sigue autocorrelacion lienal de orden 1 y no se rechaza la hipotesis nula
bgtest(modeloPrecio,order = 2)
## 
##  Breusch-Godfrey test for serial correlation of order up to 2
## 
## data:  modeloPrecio
## LM test = 3.0334, df = 2, p-value = 0.2194
#en el segundo caso se concluye que el modelo sigue una autocorrelacion de orden 2 y se acepta la la hipotesis nula ya que Pvalue<0.05