library(mblm)
library(Rfit)
## Warning: package 'Rfit' was built under R version 4.1.3
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:Rfit':
## 
##     subsets
setwd("C:/Users/lhomm/OneDrive/Documents/R")

Dat <- read.csv("C:/Users/lhomm/Downloads/BiodiversityData - Sheet1.csv")
attach(Dat)

View(Dat)

Group <- as.factor(Dat$Group)
KS <- kruskal.test(Biodiversity ~ Group)

summary(KS)
##           Length Class  Mode     
## statistic 1      -none- numeric  
## parameter 1      -none- numeric  
## p.value   1      -none- numeric  
## method    1      -none- character
## data.name 1      -none- character
KS
## 
##  Kruskal-Wallis rank sum test
## 
## data:  Biodiversity by Group
## Kruskal-Wallis chi-squared = 8, df = 8, p-value = 0.4335
detach(Dat)
Dat2 <- read.csv("C:/Users/lhomm/Downloads/Biodiversity2 - Sheet1.csv")
## Warning in read.table(file = file, header = header, sep = sep, quote = quote, :
## incomplete final line found by readTableHeader on 'C:/Users/lhomm/Downloads/
## Biodiversity2 - Sheet1.csv'
attach(Dat2)

View(Dat2)
KS2 <- kruskal.test(Biodiversity ~ Average.Distance)

summary(KS)
##           Length Class  Mode     
## statistic 1      -none- numeric  
## parameter 1      -none- numeric  
## p.value   1      -none- numeric  
## method    1      -none- character
## data.name 1      -none- character
KS2
## 
##  Kruskal-Wallis rank sum test
## 
## data:  Biodiversity by Average.Distance
## Kruskal-Wallis chi-squared = 2, df = 2, p-value = 0.3679
detach(Dat2)
attach(Dat)
## The following object is masked _by_ .GlobalEnv:
## 
##     Group
Lm <- lm(Biodiversity ~ Disturbance)

summary(Lm)
## 
## Call:
## lm(formula = Biodiversity ~ Disturbance)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.163 -2.127 -1.290  2.776  4.750 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 14.00374    1.30320  10.746 1.33e-05 ***
## Disturbance  0.01061    0.00521   2.037    0.081 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.172 on 7 degrees of freedom
## Multiple R-squared:  0.3723, Adjusted R-squared:  0.2826 
## F-statistic: 4.151 on 1 and 7 DF,  p-value: 0.08102
Lm
## 
## Call:
## lm(formula = Biodiversity ~ Disturbance)
## 
## Coefficients:
## (Intercept)  Disturbance  
##    14.00374      0.01062
source("https://raw.githubusercontent.com/athienit/STA4210material/main/check.R")
check(Lm,tests=TRUE)
## Loading required package: lawstat
## 
## Attaching package: 'lawstat'
## The following object is masked from 'package:car':
## 
##     levene.test

## $Independence
## $Independence[[1]]
## 
##  Runs Test - Two sided
## 
## data:  re
## Standardized Runs Statistic = 0.40161, p-value = 0.688
## 
## 
## $Independence[[2]]
##  lag Autocorrelation D-W Statistic p-value
##    1      -0.6427093      3.189958    0.08
##  Alternative hypothesis: rho != 0
## 
## 
## $Normality
## 
##  Shapiro-Wilk normality test
## 
## data:  re
## W = 0.88219, p-value = 0.1655
## 
## 
## [[3]]
## [1] "Constant Variance only valid if data are in groups"
## 
## $ConstantVar
## Non-constant Variance Score Test 
## Variance formula: ~ fitted.values 
## Chisquare = 0.2833167, Df = 1, p = 0.59454
Lmr <- rstudent(Lm)

Lmr
##          1          2          3          4          5          6          7 
## -0.3374152  1.4432740 -1.1029345  0.9438862 -0.4141095 -0.4576842 -0.6867345 
##          8          9 
##  2.2062535 -1.3754739
Rfit <- rfit(Biodiversity ~ Disturbance)

summary(Rfit)
## Call:
## rfit.default(formula = Biodiversity ~ Disturbance)
## 
## Coefficients:
##               Estimate Std. Error t.value   p.value    
## (Intercept) 12.7589286  1.9369154  6.5872 0.0003079 ***
## Disturbance  0.0089286  0.0051827  1.7228 0.1285959    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Multiple R-squared (Robust): 0.3854421 
## Reduction in Dispersion Test: 4.3903 p-value: 0.07439
Rfit
## Call:
## rfit.default(formula = Biodiversity ~ Disturbance)
## 
## Coefficients:
##  (Intercept)  Disturbance 
## 12.758928571  0.008928571
BiodiversityV <- as.vector(Biodiversity)

DisturbanceV <- as.vector(Disturbance)

Mblm <- mblm(BiodiversityV ~ DisturbanceV)

summary(Mblm)
## 
## Call:
## mblm(formula = BiodiversityV ~ DisturbanceV)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.9652 -0.0780  0.1009  4.0005  7.7189 
## 
## Coefficients:
##               Estimate       MAD V value Pr(>|V|)   
## (Intercept)  12.875000  0.145321      45  0.00391 **
## DisturbanceV  0.006015  0.004633      31  0.35938   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.919 on 7 degrees of freedom
detach(Dat)