R:格式化循环输出 - 没有重复的列名?

时间:2017-09-04 15:24:32

标签: r loops for-loop format cbind

我使用以下脚本输出序列相关的结果:

serial = function(x,y,z){
  for (i in 1:4 ) {
   table_serial <- data.frame(i,
   serial.test(VAR(cbind(x,y),p=i,type="const"),lags.pt=4, type=z)$serial$statistic[[1]],                   
   serial.test(VAR(cbind(x,y),p=i,type="const"),lags.pt=4,    type=z)$serial$p.value[[1]], digits=3))
   colnames(output) <- c("Lag", "Chi", "p")
   print(data.frame(serial))
   }
}

lags.pt=4是我测试的滞后数,因为数据是季度数据。功能

serial(data[1],data[2], "PT.asymptotic")

返回

  Lag  Chi    p
1   1  41.46  0.581
  Lag  Chi    p
1   2  50.032 0.133
  Lag  Chi    p
1   3  40.097 0.293
  Lag  Chi    p
1   4  40.582 0.142

有没有办法避免重新打印列标题和行? 我想要的输出:

Lag Chi    p
1   41.46  0.581
2   50.032 0.133
3   40.097 0.293
4   40.582 0.142

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

以下是您想要的。

# Defining network architecture
def build_model(layers):
    model = Sequential()

    model.add(LSTM(
        input_dim=layers[0],
        output_dim=layers[1],
        return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(
        layers[2],
        return_sequences=False))
    model.add(Dropout(0.2))

    model.add(Dense(
        output_dim=layers[3]))
    model.add(Activation("sigmoid"))

    start = time.time()
    model.compile(loss="binary_crossentropy", optimizer="rmsprop",metrics=['accuracy'],class_mode="binary")
    print("> Compilation Time : ", time.time() - start)
    return model

# Compiling model
model = build_model([1, 100, 500, 1])