[R]:使用R

时间:2019-06-07 17:19:32

标签: r function loops

任务是编写2个函数。 第一个:带有一个参数n的createSeries(),它描述时间序列的长度。预设值(n = 100)。 函数需要返回包含两列的数据帧:时间和值。 时间应从rnorm开始,并按升序排序。第二个变量应来自此等式:xt + 1 = xt + ϵt,其中ϵt是具有正态分布的变量。

(基本上可以)

第二个函数:calculateMeans()应该在给定的时间间隔内计算均值。 函数具有3个参数: 数据-具有上一个功能的data.frame interval-声明计算均值的间隔宽度, 相位-给出的点必须是间隔的边缘。

从技术上讲,该函数应该返回带有timeStart,timeEnd和value列的数据帧,其中包括start,interval的结尾和均值。

我做了第一个功能-它正在运行。

set.seed(20) 
createSeries <- function(n){
  n <- 100
  time <- rnorm(n)
  time1 <- sort(time, F)
  as.numeric(time1)
  E_t <- rnorm(n) 
  as.numeric(E_t)
  x_t <- c(1:100)
 as.numeric(x_t)
  value <- x_t + E_t
  as.numeric(value)
   value1 <-  sort(value, T)
   as.numeric(value1)
  return(data.frame(time1, value1))
}

createSeries(100)  

calculateMeans <- function(data,interval,phase){

  data <- createSeries(100)
  interval <- 1.5
  phase <- 0

    enter code here

  timeStart <- matrix()
    for(i in seq(from = -2.52, to=100, by=1.5)){
        print(i)       
    }

  timeEnd <- for(i in seq(from = -1.02, to=100, by=1.5)){
    print(i)       
  }
  u <- timeStart+timeEnd
  values <- mean(u)
  return(data.frame(timeStart, timeEnd, values))
}

calculateMeans(data, 1.5, 0)

0 个答案:

没有答案