[R] Bias and variance tradeoff
varin sacha
v@r|n@@ch@ @end|ng |rom y@hoo@|r
Wed Apr 26 22:31:45 CEST 2023
Dear R-experts,
Here below a toy example to calculate the MSE (mean squared error).
Starting from this equation : MSE = bias^2 + variance + irreducible error
I am trying to get the bias and the variance in addition to the MSE. How to get them both?
Many thanks for your help.
#########################################################
#Data
y=c(23,34,54,34,23,34,43,45,56,65,45,42,32,35,37,41,54,56,53,45)
x1=c(2,4,3,5,4,6,4,3,2,1,2,3,4,3,5,6,7,6,4,3)
x2=c(11,12,13,21,14,13,15,17,18,16,21,21,23,19,11,10,19,17,16,11)
x3=c(45,46,34,24,47,48,50,40,41,39,37,34,50,56,53,51,37,48,49,53)
#Fit the model
model=lm(y~x1+x2+x3)
#Calculate MSE
model_summ=summary(model)
mean(model_summ$residuals^2)
###########################################################
More information about the R-help
mailing list