栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > R语言

R语言for循环

R语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

R语言for循环

基本语法:for (name in expr_1) expr_2

实例操作:

1.构造矩阵
x=array(0,dim=c(4,4)) # 构造四阶矩阵 数值全为0
for (i in 1:4){
    for (j in 1:4){
        x[i,j]=1/(i+j+1)
  }
    }
print(x)
         [,1]      [,2]      [,3]      [,4]
[1,] 0.3333333 0.2500000 0.2000000 0.1666667
[2,] 0.2500000 0.2000000 0.1666667 0.1428571
[3,] 0.2000000 0.1666667 0.1428571 0.1250000
[4,] 0.1666667 0.1428571 0.1250000 0.1111111
2.利用循序进行单位根检验
nrow=20
ncol=5
A=matrix(nrow=nrow,ncol=ncol,data=NA)
for (i in 1:ncol) { A[,i]= rnorm(20, mean=0, sd=1) #构造正太分布,产生20个随机数,服从正太分布 }
library(tseries) #导入所需要的函数包 for (i in 1:5) {print(adf.test(A[,i]))} #AD

 结果如下,很方便.

Augmented Dickey-Fuller Test data: A[, i] Dickey-Fuller = -2.4773, Lag order = 2, p-value = 0.3905 alternative hypothesis: stationary Augmented Dickey-Fuller Test data: A[, i] Dickey-Fuller = -1.8836, Lag order = 2, p-value = 0.6167 alternative hypothesis: stationary Augmented Dickey-Fuller Test data: A[, i] Dickey-Fuller = -3.5647, Lag order = 2, p-value = 0.05491 alternative hypothesis: stationary Augmented Dickey-Fuller Test data: A[, i] Dickey-Fuller = -2.2957, Lag order = 2, p-value = 0.4597 alternative hypothesis: stationary Augmented Dickey-Fuller Test data: A[, i] Dickey-Fuller = -2.2784, Lag order = 2, p-value = 0.4663 alternative hypothesis: stationary

  

F单位根检验

  

 

转载于:https://www.cnblogs.com/jin-liang/p/8922832.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/855800.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号