简体   繁体   中英

Calculate and plot residuals variance of a regression, per year, using R

I calculated a linear model, with the lm() function. I know how to extract the residuals, using the resid() function, but I would like to calculate the values of the residuals variance by year and plot it.

You could use a data frame out of the residuals and the year, aggreagte it and make a barplot .

fdf <- data.frame(resid=fit$residuals, year=fit$model$year)
res <- aggregate(resid ~ year, fdf, var)
barplot(resid ~ year, res)

在此处输入图像描述


Toy data

set.seed(42)
dat <- transform(cbind.data.frame(year=1:5, x=rnorm(500)), y=.5 * x + rnorm(500))
fit <- lm(y ~ x + year, dat)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM