r语言sqrt函数用法(简述函数sqrt()的功能,并举例说明)

本文目录
简述函数sqrt()的功能,并举例说明
求一个非负数的算术平方根。
如 sqrt(0)=0,sqrt(4)=2,sqrt(9)=3,sqrt(8)=2√2 。
用R语言求根号3的值
首先order是直接返回位置的可以用which来取到满足条件的下标://定义a和向量ma=2.5m=c(1,2,3,4,5)//求差取绝对值k=abs(m-a)which.min(k)//只返回满足条件一个下标which(k==min(k))//可以返回所有的最小数的下标m//返回所有满足条件的数不好意思,我直接回答了。分不给也不要紧,难道LZ在考试?
R语言-数据转化(log和根号)
在我们实际建模拟合时,若数据中某个变量过度离散,则需要做转化,常见的有做log和根号两种方式。
install.packages(’gridExtra’)
library(gridExtra)
#先画一个柱状图,观察friend_count这个变量的整体趋势,发现过度离散
p1《-ggplot(aes(x=friend_count),data=pf)+geom_histogram()
#log转化
p2《-p1+scale_x_log10()
#根号转化
p3《-p1+scale_x_sqrt()
#将三张图放在一个面板里
grid.arrange(p1,p2,p3,ncol=1)
最后我们发现,做了log转化的数据更接近正态分布。
sqrt用的时候结果为0
sqrt()函数的详解和用法
1.sqrt函数运行的结果是算术平方根
即不能运算出负数值,也不能输出虚数结果。 如需要得到算术平方根中的负数值,则应该在sqrt函数前添加负号。 2.sqrt函数的参数必须是非负数值,如果是负数值,则输出会报错,即数值无效;如果参数是未定义的非数字变量,则按回车后也会报错,即变量名有误。 3.sqrt函数的计算精度与C语言设置的精度有关,可以通过设置更改精度。
查看更多
r语言计算均方误差怎么判断
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 1Q Median 3Q Max
-5.6621 -1.2257 -0.0272 1.4151 6.6421
Coefficients:
Estimate Std. Error t 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.961622
示例
x2《-rnorm(5000,125,21)
y2《-rnorm(5000,137,10)
M2《-lm(y2~x2)
summary(M2)
输出结果
Call:
lm(formula = y2 ~ x2)
Residuals:
Min 1Q Median 3Q Max
-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))
输出结果
10.05584
示例
x37《-rpois(500,5)
y3《-rpois(500,10)
M3《-lm(y3~x3)
summary(M3)
输出结果
Call:
lm(formula = y3 ~ x3)
Residuals:
Min 1Q Median 3Q Max
-7.9004 -1.9928 -0.2155 2.1921 9.3770
Coefficients:
Estimate Std. Error t value Pr(》|t|)
(Intercept) 10.17770 0.32330 31.481 《2e-16 ***
x3 -0.09244 0.06145 -1.504 0.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))
输出结果
3.020734
示例
x4《-runif(50000,5,10)
y4《-runif(50000,2,10)
M4《-lm(y4~x4)
summary(M4)
输出结果
Call:
lm(formula = y4 ~ x4)
Residuals:
Min 1Q Median 3Q Max
-4.0007 -1.9934 -0.0063 1.9956 3.9995
Coefficients:
Estimate Std. Error t value Pr(》|t|)
(Intercept) 5.9994268 0.0546751 109.729 《2e-16 ***
x4 0.0001572 0.0071579 0.022 0.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))
输出结果
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 1Q Median 3Q Max
-4495 -2242 -4 2230 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))
输出结果
2593.709

更多文章:
vscode离线python环境搭建(VScode配置Python环境“配置任务运行程序”遇到问题)
2026年5月23日 19:15
javabus怎么上不去了(为什么JAV连续几天都登不上去)
2026年3月30日 04:30
函数static修饰(C++类的线程函数为什么要加static修饰)
2026年1月3日 20:45
周杰伦最新专辑发布时间(周杰伦新专辑确切消息!于今年12月12日发布!!)
2026年8月4日 04:00
drawimage 缩放(如何在固定的canvas画布内缩放)
2026年7月14日 16:30
canva手机版教程(超市宣传海报怎么制作图片-手机怎么做超市海报)
2025年9月12日 09:00
一i一一人口人一一 我(目前我国人口目前我国人口最多的少数民族是哪个民族)
2025年5月29日 06:45
matlab怎么画多个函数图像(如何用Matlab画函数的图像)
2025年10月17日 02:00
completefuture使用场景(completefuture无法手动停止)
2026年1月15日 20:00
css怎么把文字放在图片中间(div+css怎么让图片在两边文字在中间)
2025年6月16日 21:30











