set.seed(123)
##Días a la germinación
dag = sort(runif(35, min=6, max=12))
##Profundidad de siembra
pds = rep(seq(0,12,2),each=5)

##Data frame
dfc = data.frame(pds=pds, dag=dag)

Regresión lineal simple

\[y = \beta_{0} + \beta_{1}x +\epsilon \]

##Modelo
mod1 = lm(dag~pds, data=dfc)
summary(mod1)
## 
## Call:
## lm(formula = dag ~ pds, data = dfc)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.76517 -0.27175  0.03578  0.25264  0.67111 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.91286    0.11771   58.73   <2e-16 ***
## pds          0.43495    0.01632   26.64   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3863 on 33 degrees of freedom
## Multiple R-squared:  0.9556, Adjusted R-squared:  0.9542 
## F-statistic: 709.9 on 1 and 33 DF,  p-value: < 2.2e-16
##Gráfico
plot(pds, dag, xlab="Profundidad de siembra", ylab="Días en germinar", main="Observed data", ylim=c(0,12))
abline(mod1, col="green", lwd=3)

##Estimar dag para pds de 9mm
funpred=function(pds){
  if(0<=pds & pds<=12){
    dag=6.91+0.43*pds
    return(dag)}
  else{"no está en el rango"}}

funpred(9)
## [1] 10.78

Regresión polinomial

\[y = \beta_{0} + \beta_{1}x + \beta_{2}x^2 + \epsilon \]

##Modelo
mod2=lm(dag~pds+I(pds^2))
summary(mod2)
## 
## Call:
## lm(formula = dag ~ pds + I(pds^2))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.4768 -0.2099  0.0269  0.1516  0.6711 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.53189    0.10880  60.033  < 2e-16 ***
## pds          0.66353    0.04247  15.624  < 2e-16 ***
## I(pds^2)    -0.01905    0.00340  -5.602 3.44e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2787 on 32 degrees of freedom
## Multiple R-squared:  0.9776, Adjusted R-squared:  0.9762 
## F-statistic: 697.5 on 2 and 32 DF,  p-value: < 2.2e-16
##Gráfico
plot(pds, dag, xlab ="Profundidad de siembra", ylab="Días en germinar", main ="Observed data", ylim=c(0,12))
lines(pds, fitted(mod2),col = "green", lwd=3)

## Estimar dag para pds = 9mm
funpred2=function(pds){
  if(0<=pds & pds<=12){
    dag= 6.53+(0.66*pds)+(-0.019*(pds^2))
    return(dag)}
  else{"no está en el rango"}}

funpred(9)
## [1] 10.78

Métricas para seleccionar el mejor modelo

library(Metrics)
Metrics::rmse(dag, mod1$fitted.values)
## [1] 0.3750941
Metrics::rmse(dag, mod2$fitted.values)
## [1] 0.2665169
AIC(mod1)
## [1] 36.68521
AIC(mod2)
## [1] 14.76346
## AIC = criterio de información de Akaike

Modelos de diseño

Diseño factorial simple en areglo completamente al azar

\[y_{ij}=\mu +\tau_{i}+ \epsilon_{ij}\] \(y_{ij}\) = variable respuesta, \(i\) = tratamientos, \(j\) = repeticiones, \(\mu\) = intercepto, \(\tau_{i}\) = efecto del i-esimo tratamiento, \(\epsilon_{ij}\) = residuales.

AF=rnorm(n=60, mean=10, sd=0.8)
fert=gl(3,20,60,c("D0","D5", "D10"))
dm1=data.frame(AF, fert=fert)