First, let me give you the output;s generated at each step and then i will explain what is each function does at each step.
R help is great way to understand your codes and functions.
1: Factor:The function factor is used to encode a vector as a factor (the terms ‘category’ and ‘enumerated type’ are also used for factors). If argument ordered is TRUE, the factor levels are assumed to be ordered. For compatibility with S there is also a function ordered.
corn<-factor(data[,1])
output:
> corn
[1] 1 2 3 4
Levels: 1 2 3 4
yield<-data[,2]
Basically,Yield contains the second column.
> yield
[1] 68.82 86.84 90.16 61.58
2: tapply: Apply a function to each cell of a ragged array, that is to each (non-empty) group of values given by a unique combination of the levels of certain factors.
Code: tapply(yield,list(corn),mean)
boxplot(yield~corn)
output:
> tapply(yield,list(corn),mean)
1 2 3 4
68.82 86.84 90.16 61.58
Code Explanation in image attached.
Part 2: Code:
1: m1<-lm(yield~corn)
# lm is used to fit linear models. lm gives us coefficient of
linear regression model between yield and crop.
2: qqnorm(residuals(m1))
#residuals is a generic function which extracts model residuals from objects returned by modeling functions. QQNorm is used to plot normal QQ plot.
3: summary(m1)
# summary function gives us summary statistics of a model i.e.
coefficients , residual standard error , R square , F statistics
etc.
4: anova(m1)
# Anova computes analysis of variance table for one or more fitted models.
Outputs of part 2:
> m1<-lm(yield~corn)
> qqnorm(residuals(m1))
> summary(m1)
>anova(m1)
Call:
lm(formula = yield ~ corn)
Residuals:
ALL 4 residuals are 0: no residual degrees of freedom!
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 68.82 NA NA NA
corn2 18.02 NA NA NA
corn3 21.34 NA NA NA
corn4 -7.24 NA NA NA
Residual standard error: NaN on 0 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: NaN
F-statistic: NaN on 3 and 0 DF, p-value: NA
> anova(m1)
Analysis of Variance Table
Response: yield
Df Sum Sq Mean Sq F value Pr(>F)
corn 3 574.61 191.54
Residuals 0 0.00
Warning message:
In anova.lm(m1) :
ANOVA F-tests on an essentially perfect fit are unreliable
R code explain please. Please explain what “factor”, “tapply”, “residuals” means. Please explain the code in...