Question
R code explain please.

Please explain what “factor”, “tapply”, “residuals” means.

Please explain the code in the photo precisely.

MAST20005/MAST90058: Week 9 Lab Goals: (i) Analysis of variance, (i) Study the distribution of the test statistics and p-values through simulations; (ii) Compare tests by simulating power curves. Data for Section 1: Corn data (corn.txt). The yield obtained, in bushels per acre, of 4 test plots for each of 4 different varieties of corn. The data file can be obtained from the shared folder in the computer labs, or from the LMS. 1 Analysis of variance (ANOVA) Consider the corn dataset. Let g, be the average yield of variety i of corn, i 1,2,3,4. Test Fa the hypothesis Ho: μ.-AT AT μ4 at the 5% level of significance. Variety Yield 168.82 76.9974.30 78.73 286.84 75.69 77.87 76.18 390.16 78.84 80.65 83.58 61.58 73.51 74.57 70.75 1. Assuming the data file is in your working directory, the following commands will load the data into R, covert Corn to a Eictor (categorical variable) and plot the data data <-read.table (corn. trt, header TRUE) Corn 3- factor (datal. n) Yield KH data2 Corn table(Corn) Yield tapply(Yield, list (Corn), nean) # group means boxplot (Yield n Corn) 2. Do an ANOVA: ni 1(Yield Corn) ggnormCresiduals (ni)) summary (at) anova (n1) Hence complete the following table: Source SSMSFp-value Treatiment Error Total 12
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Page No.: uV Date 此三 eadax TRUE unblie 4.1 Gun tabla build tion s facto 一キ斗ーGives us. o sbiecttield Peint 6 each eld and Quob

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

Add a comment
Know the answer?
Add Answer to:
R code explain please. Please explain what “factor”, “tapply”, “residuals” means. Please explain the code in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT