r语言计算均方误差怎么判断

Python017

r语言计算均方误差怎么判断,第1张

1、RMSE(均方根误差)即标准误差

假如数据在A1:Z1

标准方差用函数=STDEV(A1:Z1)

方差用函数=VARA(A1:Z1)

2、MRE(平均相对误差)

Excel/函数/统计/STDEV(Sd)

计算出标准偏差Sd值,然后除以平均数再×100%就可以了。

为了找到均方根误差,我们首先需要找到残差(也称为误差,我们需要对这些值均方根),然后需要计算这些残差的均方根。因此,如果我们有一个线性回归模型对象说M,则均方根误差可以找到为sqrt(mean(M $residuals ^ 2))。

示例

x1<-rnorm(500,50,5)

y1<-rnorm(500,50,2)

M1<-lm(y1~x1)

summary(M1)

输出结果

Call:

lm(formula = y1 ~ x1)

Residuals:

Min 1QMedian3QMax

-5.6621 -1.2257 -0.0272 1.4151 6.6421

Coefficients:

EstimateStd.Errort value Pr(>|t|)

(Intercept) 50.178943 0.915473 54.812 <2e-16 ***

x1 -0.002153 0.018241 -0.118 0.906

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.966 on 498 degrees of freedom

Multiple R-squared: 2.798e-05, Adjusted R-squared: -0.00198

F-statistic: 0.01393 on 1 and 498 DF, p-value: 0.9061

从模型M1中找到均方根误差-

示例

sqrt(mean(M1$residuals^2))

输出结果

[1] 1.961622

示例

x2<-rnorm(5000,125,21)

y2<-rnorm(5000,137,10)

M2<-lm(y2~x2)

summary(M2)

输出结果

Call:

lm(formula = y2 ~ x2)

Residuals:

Min 1QMedian3QMax

-37.425 -7.005 -0.231 6.836 36.627

Coefficients:

Estimate Std.Error t value Pr(>|t|)

(Intercept) 138.683501 0.851247 162.918 <2e-16 ***

x2 -0.014386 0.006735 -2.136 0.0327 *

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 10.06 on 4998 degrees of freedom

Multiple R-squared: 0.0009121, Adjusted R-squared: 0.0007122

F-statistic: 4.563 on 1 and 4998 DF, p-value: 0.03272

从模型M2中找到均方根误差:

示例

sqrt(mean(M2$residuals^2))

输出结果

[1] 10.05584

示例

x37<-rpois(500,5)

y3<-rpois(500,10)

M3<-lm(y3~x3)

summary(M3)

输出结果

Call:

lm(formula = y3 ~ x3)

Residuals:

Min 1QMedian3QMax

-7.9004 -1.9928 -0.2155 2.1921 9.3770

Coefficients:

EstimateStd.Error t value Pr(>|t|)

(Intercept) 10.17770 0.3233031.481<2e-16 ***

x3 -0.09244 0.06145-1.5040.133

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.027 on 498 degrees of freedom

Multiple R-squared: 0.004524, Adjusted R-squared: 0.002525

F-statistic: 2.263 on 1 and 498 DF, p-value: 0.1331

从模型M3查找均方根误差-

示例

sqrt(mean(M3$residuals^2))

输出结果

[1] 3.020734

示例

x4<-runif(50000,5,10)

y4<-runif(50000,2,10)

M4<-lm(y4~x4)

summary(M4)

输出结果

Call:

lm(formula = y4 ~ x4)

Residuals:

Min1Q Median 3QMax

-4.0007 -1.9934 -0.0063 1.9956 3.9995

Coefficients:

EstimateStd.Error t value Pr(>|t|)

(Intercept) 5.9994268 0.0546751 109.729 <2e-16 ***

x40.0001572 0.0071579 0.0220.982

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.309 on 49998 degrees of freedom

Multiple R-squared: 9.646e-09, Adjusted R-squared: -1.999e-05

F-statistic: 0.0004823 on 1 and 49998 DF, p-value: 0.9825

从模型M4找到均方根误差-

示例

sqrt(mean(M4$residuals^2))

输出结果

[1] 2.308586

示例

x5<-sample(5001:9999,100000,replace=TRUE)

y5<-sample(1000:9999,100000,replace=TRUE)

M5<-lm(y5~x5)

summary(M5)

输出结果

Call:

lm(formula = y5 ~ x5)

Residuals:

Min 1QMedian 3Q Max

-4495 -2242-42230 4512

Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) 5.504e+03 4.342e+01 126.765 <2e-16 ***

x5-1.891e-03 5.688e-03 -0.333 0.74

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2594 on 99998 degrees of freedom

Multiple R-squared: 1.106e-06, Adjusted R-squared: -8.895e-06

F-statistic: 0.1106 on 1 and 99998 DF, p-value: 0.7395

从模型M5中找到均方根误差<

示例

sqrt(mean(M5$residuals^2))

输出结果

[1] 2593.709

用的最多的,是求均值的mean()函数,当然这里也要提到,像sum()这种求和函数,

还有sd(x) 标准差函数,var(x) 方差函数。min()求最小值,max()求最大值。

我们来具体试试,这里使用一个向量:

test<-c(2,4,5,23,199,25,78,90,12)

求最大值

>max(test)

[1] 19

求最小值

>min(test)

求和

>sum(test)

[1] 43

求标准差,求方差

>sd(test)

[1] 65.01154

>var(test)

[1] 4226.

在来试试最重要的均值

>mean(test)

[1] 48.66667

另外中位数计算。使用median()函数

>median(test)

[1] 23

如果给定一种概率分布,通常会有四类计算问题:

计算其概率密度density (d)计算其概率分布probability(p)计算其百分位数quantile (q)随机数模拟random (r)上面四类计算对应的英文首字母,就是R语言类率分布函数的开头字母。

比如说,正态分布是norm的化,那密度函数就是dnorm(),分布函数就是pnorm(),

更有用的是用相应分布生成随机数,比如rnorm(),就会生成服从正态分布的随机数。

比如我们生成100个服从正态分布的随机数

rnorm(100)

[1] -9.064408e-01 1.026560e+00 -1.097470e+00 1.055395e+00 9.377175e-01

[6] -2.080103e-01 -3.092396e-01 -8.739942e-01 -1.242774e+00 1.102486e+00

[11] 1.082092e+00 -1.695528e+00 -5.930809e-01 -2.100800e-01 8.253859e-01

[16] -1.112551e+00 -3.960474e-01 -9.354820e-01 7.291608e-01 -3.773510e-01

[21] -3.438082e-01 -7.378688e-02 -9.047609e-01 -1.036344e+00 9.485103e-01

[26] -3.437985e-01 -2.145275e-02 1.350098e+00 -1.283633e+00 3.767240e-01

[31] 1.169566e+00 -4.325399e-01 -9.215626e-02 3.839357e-01 3.045491e-01

......

我们再用相应的频率分布直方图来看一下,这些生成的随机数:

hist(rnorm(100))

R就画出了这些随机数的频率分布图