This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
plot(cars)
mo=lm(dist~speed,data=cars)
summary(mo)
##
## Call:
## lm(formula = dist ~ speed, data = cars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -29.069 -9.525 -2.272 9.215 43.201
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -17.5791 6.7584 -2.601 0.0123 *
## speed 3.9324 0.4155 9.464 1.49e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 15.38 on 48 degrees of freedom
## Multiple R-squared: 0.6511, Adjusted R-squared: 0.6438
## F-statistic: 89.57 on 1 and 48 DF, p-value: 1.49e-12
anova(mo)
## Analysis of Variance Table
##
## Response: dist
## Df Sum Sq Mean Sq F value Pr(>F)
## speed 1 21186 21185.5 89.567 1.49e-12 ***
## Residuals 48 11354 236.5
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
If we take a random sampling of n=25 with uniform distribution to split the data in class we shold not found any difference in group except 5% of the time (Well for the reader is it really true with an uniform sampling?)
set.seed(999)
A=sample(50,25,replace=F)
hist(A)#not looking Unif but it is n=50!!!
ds1=cars[A,]
ds1$cat="class1"
ds2=cars[-A,]
ds2$cat="class2"
new=rbind(ds1,ds2)
new
## speed dist cat
## 27 16 32 class1
## 4 7 22 class1
## 7 10 18 class1
## 41 20 52 class1
## 14 12 24 class1
## 1 4 2 class1
## 10 11 17 class1
## 22 14 60 class1
## 35 18 84 class1
## 23 14 80 class1
## 15 12 28 class1
## 5 8 16 class1
## 48 24 93 class1
## 19 13 46 class1
## 18 13 34 class1
## 45 23 54 class1
## 37 19 46 class1
## 12 12 14 class1
## 9 10 34 class1
## 31 17 50 class1
## 21 14 36 class1
## 47 24 92 class1
## 39 20 32 class1
## 25 15 26 class1
## 26 15 54 class1
## 2 4 10 class2
## 3 7 4 class2
## 6 9 10 class2
## 8 10 26 class2
## 11 11 28 class2
## 13 12 20 class2
## 16 13 26 class2
## 17 13 34 class2
## 20 14 26 class2
## 24 15 20 class2
## 28 16 40 class2
## 29 17 32 class2
## 30 17 40 class2
## 32 18 42 class2
## 33 18 56 class2
## 34 18 76 class2
## 36 19 36 class2
## 38 19 68 class2
## 40 20 48 class2
## 42 20 56 class2
## 43 20 64 class2
## 44 22 66 class2
## 46 24 70 class2
## 49 24 120 class2
## 50 25 85 class2
#check summary
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
summary(new)
## speed dist cat
## Min. : 4.0 Min. : 2.00 Length:50
## 1st Qu.:12.0 1st Qu.: 26.00 Class :character
## Median :15.0 Median : 36.00 Mode :character
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
new$cat=factor(new$cat)
boxplot(new$dist~new$cat)
boxplot(new$speed~new$cat)#num balanced not diff
interaction.plot(cut(new$dist,3),new$cat,new$speed)##Interaction dissmissed
t.test(new$dist~new$cat)
##
## Welch Two Sample t-test
##
## data: new$dist by new$cat
## t = -0.30991, df = 47.685, p-value = 0.758
## alternative hypothesis: true difference in means between group class1 and group class2 is not equal to 0
## 95 percent confidence interval:
## -17.07448 12.51448
## sample estimates:
## mean in group class1 mean in group class2
## 41.84 44.12
t.test(new$speed~new$cat)
##
## Welch Two Sample t-test
##
## data: new$speed by new$cat
## t = -1.0714, df = 47.91, p-value = 0.2893
## alternative hypothesis: true difference in means between group class1 and group class2 is not equal to 0
## 95 percent confidence interval:
## -4.602664 1.402664
## sample estimates:
## mean in group class1 mean in group class2
## 14.6 16.2
mo1=lm(new$speed~new$cat+new$dist)#set factor in 2pos
summary(mo1)
##
## Call:
## lm(formula = new$speed ~ new$cat + new$dist)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.8771 -2.3332 0.3476 2.1304 7.0186
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.71754 0.95797 8.056 2.10e-10 ***
## new$catclass2 1.22495 0.88507 1.384 0.173
## new$dist 0.16449 0.01735 9.482 1.73e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.126 on 47 degrees of freedom
## Multiple R-squared: 0.6647, Adjusted R-squared: 0.6505
## F-statistic: 46.6 on 2 and 47 DF, p-value: 7.021e-12
summary(aov((mo1)))
## Df Sum Sq Mean Sq F value Pr(>F)
## new$cat 1 32.0 32.0 3.275 0.0768 .
## new$dist 1 878.7 878.7 89.916 1.73e-12 ***
## Residuals 47 459.3 9.8
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
as exepcted selecting a SRS and split equally the data n=50 into 2 groups show no change of effect in mean diff
Lets explore how nbalancing class affect model preformance
new10=new[new$cat=="class1",]
AA=sample(10,10,replace=FALSE)
new100=new10[AA,]
new200=new[new$cat=="class2", ]
df1=rbind(new100,new200)
df1
## speed dist cat
## 23 14 80 class1
## 41 20 52 class1
## 22 14 60 class1
## 27 16 32 class1
## 14 12 24 class1
## 7 10 18 class1
## 4 7 22 class1
## 10 11 17 class1
## 1 4 2 class1
## 35 18 84 class1
## 2 4 10 class2
## 3 7 4 class2
## 6 9 10 class2
## 8 10 26 class2
## 11 11 28 class2
## 13 12 20 class2
## 16 13 26 class2
## 17 13 34 class2
## 20 14 26 class2
## 24 15 20 class2
## 28 16 40 class2
## 29 17 32 class2
## 30 17 40 class2
## 32 18 42 class2
## 33 18 56 class2
## 34 18 76 class2
## 36 19 36 class2
## 38 19 68 class2
## 40 20 48 class2
## 42 20 56 class2
## 43 20 64 class2
## 44 22 66 class2
## 46 24 70 class2
## 49 24 120 class2
## 50 25 85 class2
dim(df1)
## [1] 35 3
t.test(df1$dist~df1$cat)
##
## Welch Two Sample t-test
##
## data: df1$dist by df1$cat
## t = -0.48092, df = 16.012, p-value = 0.6371
## alternative hypothesis: true difference in means between group class1 and group class2 is not equal to 0
## 95 percent confidence interval:
## -27.14701 17.10701
## sample estimates:
## mean in group class1 mean in group class2
## 39.10 44.12
mo2=lm(speed~dist+cat,data=df1)#set factor in 2pos
summary(mo2)
##
## Call:
## lm(formula = speed ~ dist + cat, data = df1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.7848 -2.2793 0.4539 2.2242 5.3527
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.39448 1.20995 5.285 8.67e-06 ***
## dist 0.15871 0.01913 8.298 1.76e-09 ***
## catclass2 2.80328 1.12950 2.482 0.0185 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.008 on 32 degrees of freedom
## Multiple R-squared: 0.7119, Adjusted R-squared: 0.6939
## F-statistic: 39.54 on 2 and 32 DF, p-value: 2.249e-09
head(model.matrix(mo1),7)
## (Intercept) new$catclass2 new$dist
## 1 1 0 32
## 2 1 0 22
## 3 1 0 18
## 4 1 0 52
## 5 1 0 24
## 6 1 0 2
## 7 1 0 17
anova(mo2)
## Analysis of Variance Table
##
## Response: speed
## Df Sum Sq Mean Sq F value Pr(>F)
## dist 1 659.75 659.75 72.9256 9.286e-10 ***
## cat 1 55.73 55.73 6.1597 0.01851 *
## Residuals 32 289.50 9.05
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(mo1)
## Analysis of Variance Table
##
## Response: new$speed
## Df Sum Sq Mean Sq F value Pr(>F)
## new$cat 1 32.0 32.00 3.2745 0.07676 .
## new$dist 1 878.7 878.70 89.9163 1.73e-12 ***
## Residuals 47 459.3 9.77
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
contrasts(df1$cat)##attention sum
## class2
## class1 0
## class2 1
df1
## speed dist cat
## 23 14 80 class1
## 41 20 52 class1
## 22 14 60 class1
## 27 16 32 class1
## 14 12 24 class1
## 7 10 18 class1
## 4 7 22 class1
## 10 11 17 class1
## 1 4 2 class1
## 35 18 84 class1
## 2 4 10 class2
## 3 7 4 class2
## 6 9 10 class2
## 8 10 26 class2
## 11 11 28 class2
## 13 12 20 class2
## 16 13 26 class2
## 17 13 34 class2
## 20 14 26 class2
## 24 15 20 class2
## 28 16 40 class2
## 29 17 32 class2
## 30 17 40 class2
## 32 18 42 class2
## 33 18 56 class2
## 34 18 76 class2
## 36 19 36 class2
## 38 19 68 class2
## 40 20 48 class2
## 42 20 56 class2
## 43 20 64 class2
## 44 22 66 class2
## 46 24 70 class2
## 49 24 120 class2
## 50 25 85 class2
relevel(df1$cat,ref="class2")
## [1] class1 class1 class1 class1 class1 class1 class1 class1 class1 class1
## [11] class2 class2 class2 class2 class2 class2 class2 class2 class2 class2
## [21] class2 class2 class2 class2 class2 class2 class2 class2 class2 class2
## [31] class2 class2 class2 class2 class2
## Levels: class2 class1
library(effects)
## Warning: le package 'effects' a été compilé avec la version R 4.5.3
## Le chargement a nécessité le package : carData
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
effect("cat",mo2)
##
## cat effect
## cat
## class1 class2
## 13.16909 15.97237
7.25+0.19*mean(df1$dist)
## [1] 15.36029
library(effsize)
## Warning: le package 'effsize' a été compilé avec la version R 4.5.3
cohen.d(speed~cat,data=df1)#to low effesize
##
## Cohen's d
##
## d estimate: -0.6846472 (medium)
## 95 percent confidence interval:
## lower upper
## -1.46388597 0.09459158
new10=new[new$cat=="class1",]
AAA=sample(10,5,replace=FALSE)
new1000=new10[AAA,]
new1000
## speed dist cat
## 4 7 22 class1
## 22 14 60 class1
## 35 18 84 class1
## 23 14 80 class1
## 41 20 52 class1
new200=new[new$cat=="class2", ]
df2=rbind(new1000,new200)
df2
## speed dist cat
## 4 7 22 class1
## 22 14 60 class1
## 35 18 84 class1
## 23 14 80 class1
## 41 20 52 class1
## 2 4 10 class2
## 3 7 4 class2
## 6 9 10 class2
## 8 10 26 class2
## 11 11 28 class2
## 13 12 20 class2
## 16 13 26 class2
## 17 13 34 class2
## 20 14 26 class2
## 24 15 20 class2
## 28 16 40 class2
## 29 17 32 class2
## 30 17 40 class2
## 32 18 42 class2
## 33 18 56 class2
## 34 18 76 class2
## 36 19 36 class2
## 38 19 68 class2
## 40 20 48 class2
## 42 20 56 class2
## 43 20 64 class2
## 44 22 66 class2
## 46 24 70 class2
## 49 24 120 class2
## 50 25 85 class2
dim(df2)
## [1] 30 3
cohen.d(speed~cat,data=df2)
##
## Cohen's d
##
## d estimate: -0.2998578 (small)
## 95 percent confidence interval:
## lower upper
## -1.3064964 0.7067808
table (df1$cat)
##
## class1 class2
## 10 25
table(df2$cat)
##
## class1 class2
## 5 25
t.test(df2$dist~df2$cat)
##
## Welch Two Sample t-test
##
## data: df2$dist by df2$cat
## t = 1.2498, df = 6.0515, p-value = 0.2575
## alternative hypothesis: true difference in means between group class1 and group class2 is not equal to 0
## 95 percent confidence interval:
## -14.7658 45.7258
## sample estimates:
## mean in group class1 mean in group class2
## 59.60 44.12
mo3=lm(df2$speed~df2$dist+df2$cat)#set factor in 2pos
#2 predictors 4 regressors
summary(mo3)
##
## Call:
## lm(formula = df2$speed ~ df2$dist + df2$cat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.5378 -1.5335 -0.1822 2.1179 6.6612
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.70941 1.85140 2.544 0.0170 *
## df2$dist 0.16595 0.02129 7.794 2.22e-08 ***
## df2$catclass2 4.16890 1.51303 2.755 0.0104 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.014 on 27 degrees of freedom
## Multiple R-squared: 0.6963, Adjusted R-squared: 0.6738
## F-statistic: 30.96 on 2 and 27 DF, p-value: 1.029e-07
anova(mo3)
## Analysis of Variance Table
##
## Response: df2$speed
## Df Sum Sq Mean Sq F value Pr(>F)
## df2$dist 1 493.57 493.57 54.3227 6.284e-08 ***
## df2$cat 1 68.98 68.98 7.5919 0.01037 *
## Residuals 27 245.32 9.09
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(mo1)
## Analysis of Variance Table
##
## Response: new$speed
## Df Sum Sq Mean Sq F value Pr(>F)
## new$cat 1 32.0 32.00 3.2745 0.07676 .
## new$dist 1 878.7 878.70 89.9163 1.73e-12 ***
## Residuals 47 459.3 9.77
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
confint(mo2)
## 2.5 % 97.5 %
## (Intercept) 3.9298981 8.8590599
## dist 0.1197491 0.1976688
## catclass2 0.5025584 5.1040035
confint(mo3)#factor contain zero value mean no diff in groups
## 2.5 % 97.5 %
## (Intercept) 0.9106504 8.508178
## df2$dist 0.1222599 0.209639
## df2$catclass2 1.0644188 7.273376
#However for teh continuous variable the beta coef is unbiased
library(car)
Anova(mo3)
## Anova Table (Type II tests)
##
## Response: df2$speed
## Sum Sq Df F value Pr(>F)
## df2$dist 551.88 1 60.7406 2.218e-08 ***
## df2$cat 68.98 1 7.5919 0.01037 *
## Residuals 245.32 27
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova(mo2)
## Anova Table (Type II tests)
##
## Response: speed
## Sum Sq Df F value Pr(>F)
## dist 622.90 1 68.8529 1.764e-09 ***
## cat 55.73 1 6.1597 0.01851 *
## Residuals 289.50 32
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
mo33=lm(df2$speed~df2$dist+df2$cat-1)#set factor in 2pos
summary(mo33)
##
## Call:
## lm(formula = df2$speed ~ df2$dist + df2$cat - 1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.5378 -1.5335 -0.1822 2.1179 6.6612
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## df2$dist 0.16595 0.02129 7.794 2.22e-08 ***
## df2$catclass1 4.70941 1.85140 2.544 0.017 *
## df2$catclass2 8.87831 1.11624 7.954 1.50e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.014 on 27 degrees of freedom
## Multiple R-squared: 0.9709, Adjusted R-squared: 0.9676
## F-statistic: 300.1 on 3 and 27 DF, p-value: < 2.2e-16
mo22=lm(df1$speed~df1$dist+df1$cat-1)#set factor in 2pos
summary(mo22)
##
## Call:
## lm(formula = df1$speed ~ df1$dist + df1$cat - 1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.7848 -2.2793 0.4539 2.2242 5.3527
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## df1$dist 0.15871 0.01913 8.298 1.76e-09 ***
## df1$catclass1 6.39448 1.20995 5.285 8.67e-06 ***
## df1$catclass2 9.19776 1.03634 8.875 3.86e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.008 on 32 degrees of freedom
## Multiple R-squared: 0.968, Adjusted R-squared: 0.9651
## F-statistic: 323.2 on 3 and 32 DF, p-value: < 2.2e-16
summary(df1)
## speed dist cat
## Min. : 4.00 Min. : 2.00 class1:10
## 1st Qu.:11.50 1st Qu.: 23.00 class2:25
## Median :16.00 Median : 36.00
## Mean :15.17 Mean : 42.69
## 3rd Qu.:19.00 3rd Qu.: 62.00
## Max. :25.00 Max. :120.00
b333=bootCase(mo33,B=100)##boot the difference from group refs included in Intercept + ref base Factor
## Warning in bootCase(mo33, B = 100): 'bootCase' est obsolète.
## Utilisez plutôt ‘Boot’.
## Voir help("Deprecated") et help("car-deprecated").
b333
## df2$dist df2$catclass1 df2$catclass2
## [1,] 0.1794679 2.03547383 7.922805
## [2,] 0.1525650 4.95861111 9.790170
## [3,] 0.1597958 2.89674392 9.377126
## [4,] 0.2316686 2.07990940 5.691982
## [5,] 0.2122395 1.00174398 7.189193
## [6,] 0.1974037 -1.00267948 7.401829
## [7,] 0.2107547 1.12331900 6.933244
## [8,] 0.1741874 6.39387772 8.725137
## [9,] 0.1496360 7.42087092 10.478998
## [10,] 0.1383398 6.26285680 9.492557
## [11,] 0.1740466 3.52245643 8.668740
## [12,] 0.1621869 4.34046383 9.410306
## [13,] 0.1520176 5.25069731 9.694565
## [14,] 0.2017440 0.07792311 7.718985
## [15,] 0.1805615 4.36537644 8.930331
## [16,] 0.1484325 5.43332338 10.292358
## [17,] 0.2103240 2.89489077 7.795451
## [18,] 0.1741322 3.33071729 7.832398
## [19,] 0.1790671 5.01527278 8.047275
## [20,] 0.2386351 3.35232057 5.460759
## [21,] 0.1473025 2.95231426 8.745947
## [22,] 0.1842429 1.38736404 7.748151
## [23,] 0.1663476 4.01914498 8.876395
## [24,] 0.1714748 3.63458255 9.365897
## [25,] 0.1509925 5.92854162 9.573448
## [26,] 0.1482048 6.22497521 9.821105
## [27,] 0.1627985 5.05780032 9.026743
## [28,] 0.2147943 4.97330139 7.505449
## [29,] 0.1640591 5.81659997 8.911381
## [30,] 0.1917272 3.21514677 7.830910
## [31,] 0.1533464 7.82618354 9.634129
## [32,] 0.2069990 3.09085690 7.575439
## [33,] 0.1484405 4.93210023 9.736840
## [34,] 0.1983542 -0.54597276 7.945242
## [35,] 0.1736795 2.07609750 9.035166
## [36,] 0.1442858 6.54428108 11.212204
## [37,] 0.1522285 8.47520380 10.258273
## [38,] 0.1681385 4.57894644 7.903836
## [39,] 0.1947157 1.27640172 7.620605
## [40,] 0.1485393 7.10224826 10.164132
## [41,] 0.1762244 5.16966201 7.511630
## [42,] 0.1690942 4.32468628 8.595320
## [43,] 0.1418308 5.49015287 9.547000
## [44,] 0.1627499 5.90363265 8.736140
## [45,] 0.1781420 3.05100785 8.055174
## [46,] 0.2104260 5.21614505 7.220641
## [47,] 0.2000868 2.92708255 6.885641
## [48,] 0.1916766 0.03828403 8.203846
## [49,] 0.1596314 5.20971765 9.865702
## [50,] 0.1947225 3.96238807 7.038784
## [51,] 0.2401127 -1.97899540 6.265057
## [52,] 0.1698117 6.64526460 8.585646
## [53,] 0.1522932 6.25323237 9.612169
## [54,] 0.1514379 3.88759349 8.731221
## [55,] 0.1853041 3.83622870 7.484249
## [56,] 0.2082741 1.46147147 6.706531
## [57,] 0.2166798 4.09098773 6.837993
## [58,] 0.1876232 4.68269350 7.486792
## [59,] 0.1551044 4.05696223 9.262200
## [60,] 0.1452890 7.41092759 9.237898
## [61,] 0.2525874 3.76695381 5.665249
## [62,] 0.1902535 2.01870539 8.249077
## [63,] 0.1620395 2.91653105 8.791381
## [64,] 0.1980050 4.57173784 6.609403
## [65,] 0.1887779 7.68014139 7.704727
## [66,] 0.2016667 3.38732897 7.845264
## [67,] 0.1703523 5.40198801 9.341245
## [68,] 0.1245849 6.05535734 10.734051
## [69,] 0.1715019 5.92626384 9.636816
## [70,] 0.1889394 6.41478887 8.353025
## [71,] 0.1469563 3.94958480 9.768676
## [72,] 0.1414941 4.81491931 10.494935
## [73,] 0.1500589 6.67618048 9.375178
## [74,] 0.2114311 0.77696136 7.510925
## [75,] 0.2152703 2.39767656 6.997503
## [76,] 0.1971218 0.91298527 6.637885
## [77,] 0.1514869 5.58447425 9.106526
## [78,] 0.2106376 0.30855759 7.149700
## [79,] 0.1513224 5.33348680 9.499346
## [80,] 0.2168309 1.96967499 6.613439
## [81,] 0.1856111 1.64366518 8.788079
## [82,] 0.1521435 4.40223633 9.364883
## [83,] 0.1745550 0.03560392 8.403041
## [84,] 0.1587288 3.09531108 9.849573
## [85,] 0.1118585 6.57150187 11.989201
## [86,] 0.2112594 6.34962595 7.368221
## [87,] 0.1503880 6.65485507 10.398554
## [88,] 0.2091000 5.22127933 7.638556
## [89,] 0.1505082 6.65891120 9.086483
## [90,] 0.1960783 9.80392722 8.025915
## [91,] 0.1402217 7.78780533 10.464775
## [92,] 0.1479972 5.54820840 10.268426
## [93,] 0.1399171 9.48563656 10.507376
## [94,] 0.1853554 1.65747480 9.043277
## [95,] 0.1890063 0.32076168 7.038208
## [96,] 0.2166389 1.92818563 7.022349
## [97,] 0.2236875 -0.05287408 6.540347
## [98,] 0.1795098 1.97313898 7.986795
## [99,] 0.1741087 5.30572589 8.014695
## [100,] 0.1977385 2.66961217 7.579386
## attr(,"class")
## [1] "bootCase" "matrix"
## attr(,"pointEstimate")
## df2$dist df2$catclass1 df2$catclass2
## 0.1659494 4.7094140 8.8783112
hist(b333[,3])
quantile(b333[,3],c(0.05,0.95))
## 5% 95%
## 6.60595 10.47979
Intercept: The intercept shifts to reflect the mean of the larger baseline group.Standard Errors: Small groups get high standard errors. The estimates for rare classes have high uncertainty.Statistical Power: You lose power to find true effects in the small class.Unbiased Slopes: The estimated slope for the effect remains mathematically correct on average.Statistical ConsequencesHigh Variance: Estimates bounce around a lot across different samples.Wide Confidence Intervals: You cannot be precise about the true value for the rare group.P-values: False negatives increase because tests lack the power to cross the significance threshold.Would you like me to explain how to fix this using re-sampling methods, weighted regression, or changing the reference category This can lead to issues where the model might be biased towards predicting the majority class, thus neglecting the minority classes. In such cases, the performance metrics can be misleading, especially if the minority classes are of particular interest.
A better way to see is to set the factor at SUM contrast otherwise the effects is captured by the intercept with ctrst treatment:
contrasts(df2$cat)=contr.sum(2)
df2$cat
## [1] class1 class1 class1 class1 class1 class2 class2 class2 class2 class2
## [11] class2 class2 class2 class2 class2 class2 class2 class2 class2 class2
## [21] class2 class2 class2 class2 class2 class2 class2 class2 class2 class2
## attr(,"contrasts")
## [,1]
## class1 1
## class2 -1
## Levels: class1 class2
mo333=lm(df2$speed~df2$dist+df2$cat)#set factor in 2pos
summary(mo333)#class1 is -075 and class 2 is +0.7534 mean is 7.35
##
## Call:
## lm(formula = df2$speed ~ df2$dist + df2$cat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.5378 -1.5335 -0.1822 2.1179 6.6612
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.79386 1.32836 5.114 2.24e-05 ***
## df2$dist 0.16595 0.02129 7.794 2.22e-08 ***
## df2$cat1 -2.08445 0.75651 -2.755 0.0104 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.014 on 27 degrees of freedom
## Multiple R-squared: 0.6963, Adjusted R-squared: 0.6738
## F-statistic: 30.96 on 2 and 27 DF, p-value: 1.029e-07
contrasts(df1$cat)=contr.sum(2)
df1$cat
## [1] class1 class1 class1 class1 class1 class1 class1 class1 class1 class1
## [11] class2 class2 class2 class2 class2 class2 class2 class2 class2 class2
## [21] class2 class2 class2 class2 class2 class2 class2 class2 class2 class2
## [31] class2 class2 class2 class2 class2
## attr(,"contrasts")
## [,1]
## class1 1
## class2 -1
## Levels: class1 class2
mo222=lm(df1$speed~df1$dist+df1$cat)#set factor in 2pos
head(model.matrix(mo222),18)
## (Intercept) df1$dist df1$cat1
## 1 1 80 1
## 2 1 52 1
## 3 1 60 1
## 4 1 32 1
## 5 1 24 1
## 6 1 18 1
## 7 1 22 1
## 8 1 17 1
## 9 1 2 1
## 10 1 84 1
## 11 1 10 -1
## 12 1 4 -1
## 13 1 10 -1
## 14 1 26 -1
## 15 1 28 -1
## 16 1 20 -1
## 17 1 26 -1
## 18 1 34 -1
summary(mo222)#class1 is -075 and class 2 is +0.7534 from mean distanceis 7.35
##
## Call:
## lm(formula = df1$speed ~ df1$dist + df1$cat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.7848 -2.2793 0.4539 2.2242 5.3527
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.79612 0.97470 7.998 3.94e-09 ***
## df1$dist 0.15871 0.01913 8.298 1.76e-09 ***
## df1$cat1 -1.40164 0.56475 -2.482 0.0185 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.008 on 32 degrees of freedom
## Multiple R-squared: 0.7119, Adjusted R-squared: 0.6939
## F-statistic: 39.54 on 2 and 32 DF, p-value: 2.249e-09
Anova(mo333,type="III")
## Anova Table (Type III tests)
##
## Response: df2$speed
## Sum Sq Df F value Pr(>F)
## (Intercept) 237.67 1 26.1580 2.239e-05 ***
## df2$dist 551.88 1 60.7406 2.218e-08 ***
## df2$cat 68.98 1 7.5919 0.01037 *
## Residuals 245.32 27
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova(mo222,type="III")
## Anova Table (Type III tests)
##
## Response: df1$speed
## Sum Sq Df F value Pr(>F)
## (Intercept) 578.78 1 63.9759 3.942e-09 ***
## df1$dist 622.90 1 68.8529 1.764e-09 ***
## df1$cat 55.73 1 6.1597 0.01851 *
## Residuals 289.50 32
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
INFERENCE TREES
library(partykit)
## Warning: le package 'partykit' a été compilé avec la version R 4.5.3
## Le chargement a nécessité le package : grid
## Le chargement a nécessité le package : libcoin
## Warning: le package 'libcoin' a été compilé avec la version R 4.5.3
## Le chargement a nécessité le package : mvtnorm
X=ctree(df1$speed~df1$dist+df1$cat)
tapply(df1$dist,df1$cat,mean)
## class1 class2
## 39.10 44.12
plot(X)
XX=ctree(df2$speed~df2$dist+df2$cat)
plot(XX)
tapply(df2$dist,df2$cat,mean)
## class1 class2
## 59.60 44.12
##tree should be avoided for too simple regression pattern like ANCOVA
tapply(df1$dist,df1$cat,mean)
## class1 class2
## 39.10 44.12
#trees act a litle bit as seqeuential ANova