aula13

Author

ARLAM

carrega pacotes

library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.2.3
Warning: package 'tibble' was built under R version 4.2.3
Warning: package 'dplyr' was built under R version 4.2.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.2     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ purrr     1.0.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl)

estande <- read_excel("C:dados-diversos.xlsx", "estande")

estande |> 
  ggplot(aes(trat, nplants, group = exp))+
  geom_point()+
  #facet_wrap(~exp)+
  ylim(0,max(estande$nplants))+
  geom_smooth(se = F, method = "lm")
`geom_smooth()` using formula = 'y ~ x'

#Modelos mistos

exp1 <- estande |> 
  filter(exp == 1) 
m1 <- lm(nplants ~ trat, data = exp1)
summary(m1)

Call:
lm(formula = nplants ~ trat, data = exp1)

Residuals:
    Min      1Q  Median      3Q     Max 
-25.500  -6.532   1.758   8.573  27.226 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  52.5000     4.2044  12.487 1.84e-11 ***
trat         -0.2419     0.1859  -1.301    0.207    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 15 on 22 degrees of freedom
Multiple R-squared:  0.07148,   Adjusted R-squared:  0.02928 
F-statistic: 1.694 on 1 and 22 DF,  p-value: 0.2066
exp2 <- estande |> 
  filter(exp == 2) 
m2 <- lm(nplants ~ trat, data = exp2)
summary(m2)

Call:
lm(formula = nplants ~ trat, data = exp2)

Residuals:
     Min       1Q   Median       3Q      Max 
-25.7816  -7.7150   0.5653   8.1929  19.2184 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  60.9857     3.6304  16.798 4.93e-14 ***
trat         -0.7007     0.1605  -4.365 0.000247 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 12.95 on 22 degrees of freedom
Multiple R-squared:  0.4641,    Adjusted R-squared:  0.4398 
F-statistic: 19.05 on 1 and 22 DF,  p-value: 0.0002473
exp3 <- estande |> 
  filter(exp == 3) 
m3 <- lm(nplants ~ trat, data = exp3)
summary(m3)

Call:
lm(formula = nplants ~ trat, data = exp3)

Residuals:
     Min       1Q   Median       3Q      Max 
-26.5887  -3.9597   0.7177   5.5806  19.8952 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  95.7500     2.9529  32.425  < 2e-16 ***
trat         -0.7634     0.1306  -5.847 6.97e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 10.53 on 22 degrees of freedom
Multiple R-squared:  0.6085,    Adjusted R-squared:  0.5907 
F-statistic: 34.19 on 1 and 22 DF,  p-value: 6.968e-06
g1 <- exp1 |> 
  ggplot(aes(trat, nplants))+
  geom_point()+
  geom_abline(intercept = 52.5, 
              slope = -0.24) +
    ylim(0, max(estande$nplants))+
 # geom_smooth(method = "lm", se = F)+
  theme_bw()+
  annotate(geom = "text", x = 24,
           y = 70, label = "y = 52.5 - 0.24x")

g2 <- exp3 |> 
  ggplot(aes(trat, nplants))+
  geom_point()+
  ylim(0, max(estande$nplants))+
  geom_smooth(method = "lm", se = F)+
  annotate(geom = "text", x = 24,
           y = 70, label = "y = 52.5 - 0.24x")
library(patchwork)
Warning: package 'patchwork' was built under R version 4.2.3
g1 + g2
`geom_smooth()` using formula = 'y ~ x'

library(lme4)
Warning: package 'lme4' was built under R version 4.2.3
Carregando pacotes exigidos: Matrix
Warning: package 'Matrix' was built under R version 4.2.3

Attaching package: 'Matrix'
The following objects are masked from 'package:tidyr':

    expand, pack, unpack
mix <- lmer(nplants ~ trat + (trat | exp), 
            data = estande)
Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
Model failed to converge with max|grad| = 0.00274249 (tol = 0.002, component 1)
summary(mix)
Linear mixed model fit by REML ['lmerMod']
Formula: nplants ~ trat + (trat | exp)
   Data: estande

REML criterion at convergence: 580.8

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.0988 -0.6091  0.1722  0.6360  1.9963 

Random effects:
 Groups   Name        Variance  Std.Dev. Corr 
 exp      (Intercept) 510.68405 22.5983       
          trat          0.05516  0.2349  -0.82
 Residual             167.91303 12.9581       
Number of obs: 72, groups:  exp, 3

Fixed effects:
            Estimate Std. Error t value
(Intercept)  69.7452    13.2146   5.278
trat         -0.5687     0.1643  -3.462

Correlation of Fixed Effects:
     (Intr)
trat -0.731
optimizer (nloptwrap) convergence code: 0 (OK)
Model failed to converge with max|grad| = 0.00274249 (tol = 0.002, component 1)
library(car)
Warning: package 'car' was built under R version 4.2.3
Carregando pacotes exigidos: carData

Attaching package: 'car'
The following object is masked from 'package:dplyr':

    recode
The following object is masked from 'package:purrr':

    some
Anova(mix)
Analysis of Deviance Table (Type II Wald chisquare tests)

Response: nplants
      Chisq Df Pr(>Chisq)    
trat 11.985  1  0.0005362 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1