fire.txt :(数据出自何晓群--应用回归阐明)x y3.4 26.21.8 17.84.6 31.32.3 23.13.1 27.55.5 360.7 14.13 22.32.6 19.64.3 31.32.1 241.1 17.36.1 43.24.8 36.43.8 26.1
#-------------------------------------------------------------#数据筹备fire <- read.table('D:/fire.txt', head = T)#-------------------------------------------------------------#回归阐明plot(fire$y ~ fire$x)fire.reg <- lm(fire$y ~ fire$x, data = fire) #回归拟合summary(fire.reg) #回归阐明表anova(fire.reg) #方差阐明表abline(fire.reg, col = 2, lty = 2) #拟合直线#-------------------------------------------------------------#残差阐明fire.res <- residuals(fire.reg) #残差fire.sre <- rstandard(fire.reg) #学生化残差plot(fire.sre)abline(h = 0)text(11, fire.sre[11], label = 11, adj = (-0.3), col = 2) #标注点#-------------------------------------------------------------#预测与节制attach(fire) #毗连fire.reg <- lm(y ~ x) #这种回归拟合简朴fire.points <- data.frame(x = c(3.5, 4))fire.pred <- predict(fire.reg, fire.points, interval = 'prediction', level = 0.95) #预测:置信区间fire.preddetach(fire) #打消毗连
--------------------------------------------------------------------------------------------------#附自编的进程措施:(R较大的长处是可以本身编想要的措施和函数,尤其没有内置函数的时候)fire <- read.table('D:/fire.txt', head = T)attach(fire)--------------------------------------------lxy <- function(x){sum <- 0sum0 <- 0 for(i in 1:length(x)){sum0 <- (x[i] - mean(x)) * (y[i]-mean(y))sum <- sum + sum0}sum}
---------------------------------------------------------------------------------#用这个就不需要轮回了lxy <- function(x){mid <- (x - mean(x)) * (y-mean(y))sum <- sum(mid)sum}#对付数据框、列表等数据工具要善用apply()函数。---------------------------------------------------------------------------------
lxx <- function(x){sum <- 0sum0 <- 0 for(i in 1:length(x)){sum0 <- (x[i] - mean(x))^2sum <- sum + sum0}sum}

Lxx <- lxx(x)Lyy <- lxx(y)Lxy <- lxy(x)
b1 <- Lxy / Lxx; b1 #回归系数斜率b0 <- mean(y) - b1 * mean(x); b0 #回归系数截距residu <- y - (b0 + b1*x); residu #残差r <- Lxy / sqrt(Lxx * Lyy); r #相干系数rsqure <- r^2; rsqure #抉择系数adrsqure <- 1 - ((length(x)-1)/(length(x)-2))*(1-r^2) #调解后的抉择系数----------------------------------------------------------------------------------esrequre <- function(x){ #求尺度差平方预计值sum <- 0sum0 <- 0 for(i in 1:length(x)){sum0 <- residu[i]^2sum <- sum + sum0}residusqure <- sum/(length(x)-2)residusqure}

esterreq <- esrequre(x); esterreq #尺度差平方预计值(MSE)ester <- sqrt(esrequre(x)); ester #尺度差预计值(回归阐明表给出的尺度误差)val_t <- b1*sqrt(Lxx) / ester; val_t #检讨回归系数斜率b1的t值

SSe <- function(x){ #求残差平方和sum <- 0sum0 <- 0 for(i in 1:length(x)){sum0 <- residu[i]^2sum <- sum + sum0}sum}
SSE <- SSe(x); SSE #残差平方和MSE <- SSE/(length(x)-2); MSE #残差均方和
SSr <- function(x){sum <- 0sum0 <- 0 for(i in 1:length(x)){sum0 <- ((b0 + b1*x[i]) - mean(y))^2sum <- sum + sum0}sum}
SSR <- SSr(x); SSR #回归平方和MSR <- SSR/1; MSR #回归均方和
val_F <- SSR / MSE; val_F #检讨回归方程F值
hi <- 1/length(x) + (x-mean(x))^2/Lxx #杠杆值ZRE <- residu / ester; ZRE #尺度化残差SRE <- residu/(ester*sqrt(1-hi)); SRE #学生化残差
Y <- function(x){b0 + b1 * x} #点预计Y(3.5)

其他教程

2017-12-04


fire.txt :(数据出自何晓群--应用回归阐明)x y3.4 26.21.8 17.84.6 31.32.3 23.13.1 27.55.5 360.7 14.13 22.32.6 19.64.3 31.32.1 241.1 17.36.1 43.24.8 36.43.8 26.1#--------------------------------------- ...