### DATA IMPORT
### Read the data of the 'cake example' into your R-workspace
### These data contains the variables Volume, Firmness and Weight
### for N=1000 pieces of the production. Let these 1000 cakes be
### the production of one day, thus they can be considered the population.
setwd("G:\\tierzucht\\AG_bioinf\\teaching\\Master FPPE\\DataExamples")
library(xlsx)
X = read.xlsx("CakeCharacteristics_V02.xlsx", 1)
dim(X)
head(X)
N = nrow(X)
### SAMPLE FOR QUALITY CONTROL
### Usually, you will not be able to study the whole population.
### Therefore, you want to analyse only a sample of size n.
set.seed(123)
n = 50
S = sample(1:N, n, replace=FALSE)
Y = X[S,]
### GRAPHICAL ANALYSIS
### Make pairwise scatterplots of the variables in the populatin.
### Add the coordinated of the sample data using the R-function points.
plot(X$Volume, X$Firmness, xlab="Volume", ylab="Firmness", cex.lab=1.5, cex.axis=1.5)
points(Y$Volume, Y$Firmness, col=2, pch=16)
### SIMPLE LINEAR REGRESSION
### Fit a linear regression model to the data of the population,
### in order to model the firmness in dependence of the volume.
### Add a regression line to scatterplot using the function abline.
### Do the same based on the sample data.
### Use the function lm to fit the models.
### Apply the summary-function on the fitted model of the sample
### to see whether Volume has a significant effect on Firmness.
### CONFIDENCE BAND
### Use the predict function to generate a 95%-confidence band
### to the plot.
NEW = Y[,1:2]
P = predict(M.sam, new=NEW, interval="confidence")
head(P)
P2 = data.frame(Volume=NEW$Volume, P)
head(P2)
o = order(P2$Volume)
P2 = P2[o,]
points(P2$Volume, P2$lwr, type="l", col=4, lwd=2)
points(P2$Volume, P2$upr, type="l", col=4, lwd=2)
### SAMPLE SIZE
### Repeate the above analyses with larger sample sizes, e.g. n=100.
### Discuss the effect of the sample size on the precission the results.
### MULTIPLE REGRESSION
### Fit and analyse a regression model with volume and weight as
### independent variables and firmness as dependent variable.