-
时间序列函数ts()
ts()的参数
data 观察值的向量或矩阵 start 第一个观察值的时间 end 最后一个观察值的时间 frequency 观察值的时间频率 deltat 表示观察值时间频次的分数,比如1/12表示月度 ts.eps 时间序列表示比较容限 class 结果类的设定 names 设定多元时间序列变量名称的字符型向量 header TRUE/FALSE,原始数据第一列是否为变量名称 -
列表
-
列表的应用
#列表 > x=rnorm(20)#生成正态随机20个数据 > e=rnorm(20,mean = 0,sd=0.25)#sd表示标准差 > y=0.15*x+e > output=lm(y~x)#lm(y~x)表示关于x的线性回归函数 > names(output) [1] "coefficients" "residuals" [3] "effects" "rank" [5] "fitted.values" "assign" [7] "qr" "df.residual" [9] "xlevels" "call" [11] "terms" "model" > output$coefficients#output中存放的回归系数 (Intercept) x 0.08810978 0.19939250 > output$residuals #残差值 1 2 3 4 -0.09282232 -0.13877388 0.24089096 0.07408545 5 6 7 8 0.31357607 -0.07479624 -0.02548409 -0.10585975 9 10 11 12 -0.13589863 -0.10550118 -0.13213445 0.08072300 13 14 15 16 -0.25224860 0.06710868 -0.09243627 0.26019371 17 18 19 20 -0.29309579 0.32606966 0.17197668 -0.08557302 > output$model#原始数据y和x y x 1 -0.018958396 -0.07144633 2 0.360034812 2.05975108 3 0.165100767 -0.82199670 4 0.202606823 0.20267360 5 0.704361980 1.51799156 6 0.185344148 0.86277372 7 0.246775090 0.92355229 8 -0.176371069 -0.79552193 9 -0.283548596 -1.18239030 10 -0.571401633 -2.77849091 11 -0.096402343 -0.26268629 12 0.017628165 -0.75832654 13 -0.235598842 -0.35838875 14 0.005605898 -0.75034201 15 0.084909517 0.44753942 16 0.199469362 -0.74643800 17 -0.104335161 0.50478752 18 0.472742224 0.29370607 19 0.053073577 -1.03821803 20 0.028147960 0.12844616
-
函数summary(),可以获取对象的详细信息。
output_more=summary(output) > output_more Call: lm(formula = y ~ x) Residuals: Min 1Q Median 3Q Max -0.29310 -0.11243 -0.08018 0.10354 0.32607 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.08811 0.04230 2.083 0.051805 x 0.19939 0.04020 4.960 0.000101 (Intercept) . x *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.1877 on 18 degrees of freedom Multiple R-squared: 0.5775, Adjusted R-squared: 0.554 F-statistic: 24.6 on 1 and 18 DF, p-value: 0.0001013 > names(output_more) [1] "call" "terms" [3] "residuals" "coefficients" [5] "aliased" "sigma" [7] "df" "r.squared" [9] "adj.r.squared" "fstatistic" [11] "cov.unscaled"
-
函数list()
x1=1:15 > x2=LETTERS[1:18]#生成前18个大写字母 > x3=sample(c(TRUE,FALSE),size=20,replace=TRUE) > L=list(num=x1,ABC=x2,TF=x3) > #显示L中的第一列 > L$num;**L[[1]]**;L[[""]] [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 NULL > L$num;L[[1]];L[["num"]] [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 > #显示第一个子列表 > **L[1]** $num [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #注意L[1]和L[[1]]的区别
-
-
-
常规数据对象处理
-
向量的处理
1、利用正整数取值筛取数据
#利用正整数取值筛选数据 > x=seq(1,10,2) > x[5] [1] 9 > x[length(x)] [1] 9 > length(x) [1] 5 > x[2:5] [1] 3 5 7 9 > x[c(2,3,4,5)] [1] 3 5 7 9 > x[3] [1] 5
2、利用逻辑条件筛取数据
#利用逻辑条件筛选数据 > x=c(-23,24,25,30,-69) > index=x>20 > index [1] FALSE TRUE TRUE TRUE FALSE > x[c(FALSE,TRUE)] [1] 24 30 > x[x>20]=0 > x [1] -23 0 0 0 -69
3、利用负整数筛取数据
#利用负整数取值筛除数据 > x=c(1.5,2.6,3.7,4.8,5.9,6) > x[-(1:2)] [1] 3.7 4.8 5.9 6.0 > x[-(5:6)] [1] 1.5 2.6 3.7 4.8 > x[-6] [1] 1.5 2.6 3.7 4.8 5.9 > x[-7] [1] 1.5 2.6 3.7 4.8 5.9 6.0 > x[-8] [1] 1.5 2.6 3.7 4.8 5.9 6.0 > x[1:8] [1] 1.5 2.6 3.7 4.8 5.9 6.0 NA NA
4、常用向量运算函数
#常用的向量运算函数 > x=1:20 > length(x) [1] 20 > max(x) [1] 20 > min(x) [1] 1 > range(x) [1] 1 20 > prod(x) [1] 2.432902e+18 > sum(x) [1] 210 > sum(x) [1] 210 > any(x>10) [1] TRUE > all(x>10) [1] FALSE > summary(x) Min. 1st Qu. Median Mean 3rd Qu. Max. 1.00 5.75 10.50 10.50 15.25 20.00 > cumsum(x) [1] 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 > cummin(x) [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 > cummax(x) [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
-
R语言学习day_2
最新推荐文章于 2024-08-15 21:26:55 发布