R语言实现描述性统计
作者:互联网
# 01分布
a<- runif(20)
a
- 0.0534173720516264
- 0.0381318787112832
- 0.253885793266818
- 0.851638266118243
- 0.356620342005044
- 0.175922254333273
- 0.270358079113066
- 0.421792675741017
- 0.675487545551732
- 0.139561568852514
- 0.649348761420697
- 0.0383495420683175
- 0.673801982775331
- 0.131142142694443
- 0.241756724659353
- 0.205821343231946
- 0.826634412631392
- 0.827650502324104
- 0.48426380334422
- 0.385196640854701
# 算术平均数
mean(a)
0.385039081587456
# 几何平均数
exp(mean(log(a)))
0.269715541826603
# 中位数
median(a)
0.313489210559055
b <- sort(a)
(b[10]+b[11])/2
0.313489210559055
# 产生0到10的20个数round取整
c <- round(runif(20,0,10))
c
- 6
- 1
- 10
- 9
- 6
- 9
- 3
- 10
- 6
- 3
- 4
- 4
- 7
- 2
- 3
- 3
- 5
- 9
- 2
- 0
# 众数 table 统计出现的次数
x <- table(c)
x
c
0 1 2 3 4 5 6 7 9 10
1 1 2 4 2 1 3 1 3 2
# 取出次数最多的
names(x)[x==max(x)]
‘3’
a <- round(runif(100,1,10))
table(a)
a
1 2 3 4 5 6 7 8 9 10
4 8 11 7 16 10 11 8 14 11
stem(a)
The decimal point is at the |
1 | 0000
2 | 00000000
3 | 00000000000
4 | 0000000
5 | 0000000000000000
6 | 0000000000
7 | 00000000000
8 | 00000000
9 | 00000000000000
10 | 00000000000
hist(a)
hist(a,breaks=50)
a<- rnorm(100,0,1)
hist(a)
# 协方差与相关系数
x<-runif(20)
y<-runif(20)
cov(x,y)
0.00322176575114672
e<- runif(20,0,0.1)
z<- x*3+e
cov(x,z)
0.191163714793349
#与相关系数
cor(x,z)
0.999193595914862
# 一元线性回归|
x<- runif(10)
y<- 3*x +5+ runif(10,0,0.5)
plot(x,y)
# 线性模型
lm(y~x)
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
5.349 2.780
plot(x,y)
abline(lm(y~x),col="red")
# 多元回归
x1<- runif(10)
x2 <- runif(10)
y <- 3*x1+5*x2+2+runif(10,0,0.1)
lc<- lm(y~x1+x2)
summary(lc)
Call:
lm(formula = y ~ x1 + x2)
Residuals:
Min 1Q Median 3Q Max
-0.04436 -0.02495 0.00357 0.02298 0.04432
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.08064 0.03958 52.56 2.36e-10 ***
x1 2.97190 0.03884 76.51 1.71e-11 ***
x2 4.97719 0.04625 107.61 1.58e-12 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.03303 on 7 degrees of freedom
Multiple R-squared: 0.9995, Adjusted R-squared: 0.9993
F-statistic: 6916 on 2 and 7 DF, p-value: 2.91e-12
标签:11,10,00000000000,语言,lm,hist,描述性,x1,统计 来源: https://blog.csdn.net/weixin_44510615/article/details/97902429