Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1.46
12  99.4  1.55
13  98.7  1.55
14  96.1  1.55
15  93.6  1.4 
16  87.3  1.15
17  95    1.01
18  96.8  0.99
19  85.2  0.95
20  90.6  0.98

###Diagrama de Dispersión###
> plot(Table2_7)
> library(ggplot2)

> ggplot(Table2_7, aes(x=x, y=y)) + geom_point() + theme_light()


###Ajuste del modelo
> modelo1=lm(y~x, data = Table2_7)
> summary(modelo1)

Call:
lm(formula = y ~ x, data = Table2_7)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.35842 -0.11371  0.05373  0.11824  0.29821 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)   
(Intercept) -1.845070   0.895122  -2.061  0.05403 . 
x            0.032974   0.009738   3.386  0.00329 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.1901 on 18 degrees of freedom


Multiple R-squared:  0.3891, Adjusted R-squared:  0.3552 
F-statistic: 11.47 on 1 and 18 DF,  p-value: 0.003291

> anova(modelo1)
Analysis of Variance Table

Response: y
          Df  Sum Sq Mean Sq F value   Pr(>F)   
x          1 0.41441 0.41441  11.466 0.003291 **
Residuals 18 0.65057 0.03614                    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> str(modelo1)
List of 12
 $ coefficients : Named num [1:2] -1.845 0.033
  ..- attr(*, "names")= chr [1:2] "(Intercept)" "x"
 $ residuals    : Named num [1:20] -0.000666 -0.007608 0.298213 0.108129 -0.197626 ...
  ..- attr(*, "names")= chr [1:20] "1" "2" "3" "4" ...
 $ effects      : Named num [1:20] -5.288 0.644 0.299 0.11 -0.198 ...
  ..- attr(*, "names")= chr [1:20] "(Intercept)" "x" "" "" ...
 $ rank         : int 2
 $ fitted.values: Named num [1:20] 1.02 1.12 1.13 1 1.21 ...
  ..- attr(*, "names")= chr [1:20] "1" "2" "3" "4" ...
 $ assign       : int [1:2] 0 1
 $ qr           :List of 5
  ..$ qr   : num [1:20, 1:2] -4.472 0.224 0.224 0.224 0.224 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:20] "1" "2" "3" "4" ...
  .. .. ..$ : chr [1:2] "(Intercept)" "x"
  .. ..- attr(*, "assign")= int [1:2] 0 1
  ..$ qraux: num [1:2] 1.22 1.05
  ..$ pivot: int [1:2] 1 2
  ..$ tol  : num 1e-07
  ..$ rank : int 2
  ..- attr(*, "class")= chr "qr"
 $ df.residual  : int 18
 $ xlevels      : Named list()
 $ call         : language lm(formula = y ~ x, data = Table2_7)
 $ terms        :Classes 'terms', 'formula'  language y ~ x
  .. ..- attr(*, "variables")= language list(y, x)
  .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:2] "y" "x"
  .. .. .. ..$ : chr "x"
  .. ..- attr(*, "term.labels")= chr "x"
  .. ..- attr(*, "order")= int 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(y, x)
  .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. ..- attr(*, "names")= chr [1:2] "y" "x"
 $ model        :'data.frame': 20 obs. of  2 variables:
  ..$ y: num [1:20] 1.02 1.11 1.43 1.11 1.01 0.95 1.11 0.87 1.43 1.02 ...
  ..$ x: num [1:20] 86.9 89.8 90.3 86.3 92.6 ...
  ..- attr(*, "terms")=Classes 'terms', 'formula'  language y ~ x
  .. .. ..- attr(*, "variables")= language list(y, x)
  .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. ..$ : chr [1:2] "y" "x"
  .. .. .. .. ..$ : chr "x"
  .. .. ..- attr(*, "term.labels")= chr "x"
  .. .. ..- attr(*, "order")= int 1
  .. .. ..- attr(*, "intercept")= int 1
  .. .. ..- attr(*, "response")= int 1
  .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. .. ..- attr(*, "predvars")= language list(y, x)
  .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. .. ..- attr(*, "names")= chr [1:2] "y" "x"
 - attr(*, "class")= chr "lm"

###Estimadores
> estimadores=modelo1$coefficients
> estimadores
(Intercept)           x 
 -1.8450702   0.0329736 

###Intervalos de confianza de los parametros


> confint(modelo1)
                  2.5 %     97.5 %
(Intercept) -3.72565183 0.03551147
x            0.01251509 0.05343211

> confint(modelo1, level = 0.95)
                  2.5 %     97.5 %
(Intercept) -3.72565183 0.03551147
x            0.01251509 0.05343211

> residuales=modelo1$residuals
> residuales
            1             2             3             4             5             6 
-0.0006655618 -0.0076079514  0.2982133997  0.1081293913 -0.1976258847 -
0.0845144746 
            7             8             9            10            11            12 
 0.1097780714 -0.3138848913  0.1224641015 -0.0979376875  0.1155336674 
0.1168346783 
           13            14            15            16            17            18 
 0.1418946157  0.2272962447  0.1570923613  0.1161449974 -0.2774220013 -
0.3584231648 
           19            20 
-0.0142807025 -0.1610192088 

#valores ajustados
> yhat=modelo1$fitted.values
> yhat
        1         2         3         4         5         6         7         8 
1.0206656 1.1176080 1.1317866 1.0018706 1.2076259 1.0345145 1.0002219 1.1838849 
        9        10        11        12        13        14        15        16 
1.3075359 1.1179377 1.3444663 1.4331653 1.4081054 1.3227038 1.2429076 1.0338550 
       17        18        19        20 
1.2874220 1.3484232 0.9642807 1.1410192 
> plot(Table2_7$x, Table2_7$y, xlab = `Pureza(x)`, ylab = `Hidrocarburos(y)`)
> abline(modelo1)

> ggplot(Table2_7, aes(x=x, y=y)) + 


geom_point() +  
geom_smooth(method = `lm`, formula = y~x, se=FALSE, col=`dodgerblue1` +
theme_light()

ggplot(datos, aes(x=x, y=y)) +


geom_smooth(method="lm", se=FALSE, color="lightgrey") +
geom_segment(aes(xend=x, yend=yhat), col='red', lty='dashed') +
geom_point()+
geom_point(aes(y=yhat), col='red') +
theme_light()

You might also like