####CLASE REG. LINEAL
#############CARGADO DE DATOS, Crawley, M. J. (2012). The R book. John Wiley & Sons.
growth=c(12,10,8,11,6,7,2,3,3)
tannin=c(0,1,2,3,4,5,6,7,8)
reg.data= as.data.frame (cbind(growth,tannin),header = TRUE)
names(reg.data)
plot(tannin,growth,pch=21,col="blue",bg="red")  
model <- lm(growth~tannin)
abline(model,col="red")
yhat <- predict(model,tannin=tannin)
join <- function(i)
  lines(c(tannin[i],tannin[i]),c(growth[i],yhat[i]),col="green")
sapply(1:9,join)
model
lm(growth~tannin)
#####la ecuación de la recta sería === growth = 11.755 56 – 1.216 667 × tannin.
###### modelo nulo en el que el crecimiento se describe mediante un parámetro
###(intercepto = parámetro 1)
deviance(lm(growth~1))
deviance(lm(growth~tannin))
summary(lm(growth~tannin))           
 summary(lm(growth~tannin))[[8]]
anova(lm(growth~tannin))
summary.aov(lm(growth~tannin))
summary(lm(growth~tannin))
######## estumar valores a partir del modelo
model <- lm(growth~tannin)
predict(model,list(tannin=5.5))
######plots de los residuos. Ajuste del modelo
windows(7,7)
par(mfrow=c(2,2))
plot(model)
#####GRAFICAR EL MODELO CON LOS INTERVALOS DE CONFIANZA
newx <- seq(min(reg.data$tannin), max(reg.data$tannin), length.out=9)
preds <- predict(model, newdata = data.frame(x=newx), interval = 'confidence')
plot(growth~tannin, data = reg.data, type = 'n')
abline(model)
lines(newx, preds[ ,3], lty = 'dashed', col = 'blue')
lines(newx, preds[ ,2], lty = 'dashed', col = 'blue')
###usando ggplot2
library(ggplot2)
library(hrbrthemes)
# linear trend + confidence interval
p3 <- ggplot(reg.data, aes(x=tannin, y=growth)) +
  geom_point() +
  geom_smooth(method=lm , color="red", fill="#69b3a2", se=TRUE) +
  theme_ipsum()
p3

Última atualização: sexta-feira, 5 mai. 2023, 21:55