Transformations
Scott Gabara
06/11/2020
head(PlantGrowth)
## weight group
## 1 4.17 ctrl
## 2 5.58 ctrl
## 3 5.18 ctrl
## 4 6.11 ctrl
## 5 4.50 ctrl
## 6 4.61 ctrl
head(PlantGrowth)
## weight group
## 1 4.17 ctrl
## 2 5.58 ctrl
## 3 5.18 ctrl
## 4 6.11 ctrl
## 5 4.50 ctrl
## 6 4.61 ctrl
summary(PlantGrowth)
## weight group
## Min. :3.590 ctrl:10
## 1st Qu.:4.550 trt1:10
## Median :5.155 trt2:10
## Mean :5.073
## 3rd Qu.:5.530
## Max. :6.310
str(PlantGrowth)
## 'data.frame': 30 obs. of 2 variables:
## $ weight: num 4.17 5.58 5.18 6.11 4.5 4.61 5.17 4.53 5.33 5.14 ...
## $ group : Factor w/ 3 levels "ctrl","trt1",..: 1 1 1 1 1 1 1 1 1 1 ...
PlantGrowth$weight_trans_sqrt <- sqrt(PlantGrowth$weight) #Sqrt of 4th (weight) column
# can also use the column in the dataframe with [, columnnumber]
PlantGrowth$weight_trans_sqrt <- sqrt(PlantGrowth[,1]) #Sqrt of 4th (weight) column
PlantGrowth$weight_trans_y3 <- (PlantGrowth[,1])^3 #Raises Y to the power of 3
PlantGrowth$weight_trans_9RT <- (PlantGrowth[,1])^(1/9) #Takes the ninth root of Y
PlantGrowth$weight_trans_log <- log(PlantGrowth[,1]) #Takes the natural logarithm (ln) of Y
PlantGrowth$weight_trans_log10 <- log10(PlantGrowth[,1]) #Takes the base-10 logarithm of Y
PlantGrowth$weight_trans_exp <- exp(PlantGrowth[,1]) #Raises the constant e to the power of Y
PlantGrowth$weight_trans_abs <- abs(PlantGrowth[,1]) #Finds the absolute value of Y
PlantGrowth$weight_trans_sin <- sin(PlantGrowth[,1]) #Calculates the sine of Y)
# Test for homogeneity of variance
library(car) # load package to run leveneTest
## Loading required package: carData
leveneTest(weight ~ group, PlantGrowth) #Concentration variance among species
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 1.1192 0.3412
## 27
leveneTest(weight_trans_sqrt ~ group, PlantGrowth) #Concentration variance among species
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 1.4932 0.2426
## 27
leveneTest(weight_trans_y3 ~ group, PlantGrowth) #Concentration variance among species
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 0.2207 0.8034
## 27
leveneTest(weight_trans_9RT ~ group, PlantGrowth) #Concentration variance among species
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 1.8203 0.1813
## 27
leveneTest(weight_trans_log ~ group, PlantGrowth) #Concentration variance among species
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 1.9185 0.1663
## 27
leveneTest(weight_trans_log10 ~ group, PlantGrowth) #Concentration variance among species
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 1.9185 0.1663
## 27
leveneTest(weight_trans_exp ~ group, PlantGrowth) #Concentration variance among species
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 0.0843 0.9194
## 27
leveneTest(weight_trans_abs ~ group, PlantGrowth) #Concentration variance among species
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 1.1192 0.3412
## 27
leveneTest(weight_trans_sin ~ group, PlantGrowth) #Concentration variance among species
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 0.6002 0.5559
## 27
head(PlantGrowth)
## weight group weight_trans_sqrt weight_trans_y3 weight_trans_9RT
## 1 4.17 ctrl 2.042058 72.51171 1.171936
## 2 5.58 ctrl 2.362202 173.74111 1.210485
## 3 5.18 ctrl 2.275961 138.99183 1.200522
## 4 6.11 ctrl 2.471841 228.09913 1.222751
## 5 4.50 ctrl 2.121320 91.12500 1.181896
## 6 4.61 ctrl 2.147091 97.97218 1.185071
## weight_trans_log weight_trans_log10 weight_trans_exp weight_trans_abs
## 1 1.427916 0.6201361 64.71545 4.17
## 2 1.719189 0.7466342 265.07161 5.58
## 3 1.644805 0.7143298 177.68281 5.18
## 4 1.809927 0.7860412 450.33872 6.11
## 5 1.504077 0.6532125 90.01713 4.50
## 6 1.528228 0.6637009 100.48415 4.61
## weight_trans_sin
## 1 -0.8564780
## 2 -0.6466507
## 3 -0.8926477
## 4 -0.1723209
## 5 -0.9775301
## 6 -0.9947628
leveneTest(weight ~ group, PlantGrowth)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 1.1192 0.3412
## 27
model1<-lm(weight ~ group, PlantGrowth)
anova(model1)
## Analysis of Variance Table
##
## Response: weight
## Df Sum Sq Mean Sq F value Pr(>F)
## group 2 3.7663 1.8832 4.8461 0.01591 *
## Residuals 27 10.4921 0.3886
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
model1$resids <- residuals(model1)
shapiro.test(model1$resids)
##
## Shapiro-Wilk normality test
##
## data: model1$resids
## W = 0.96607, p-value = 0.4379
plot(model1$resids)