如何在R中创建公式化表? [英] How to create a formulated table in R?

查看:75
本文介绍了如何在R中创建公式化表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我可复制的示例:

#http://gekkoquant.com/2012/05/26/neural-networks-with-r-simple-example/

library("neuralnet")
require(ggplot2)

traininginput <-  as.data.frame(runif(50, min=0, max=100))
trainingoutput <- sqrt(traininginput)
trainingdata <- cbind(traininginput,trainingoutput)
colnames(trainingdata) <- c("Input","Output")

Hidden_Layer_1 <- 1       # value is randomly assigned 
Hidden_Layer_2 <- 1       # value is randomly assigned
Threshold_Level <- 0.1    # value is randomly assigned

net.sqrt <- neuralnet(Output~Input,trainingdata, hidden=c(Hidden_Layer_1, Hidden_Layer_2), threshold = Threshold_Level)

#Test the neural network on some test data
testdata <- as.data.frame((1:13)^2)              #Generate some squared numbers
net.results <- predict(net.sqrt, testdata)       #Run them through the neural network

cleanoutput <- cbind(testdata,sqrt(testdata),
                     as.data.frame(net.results))

colnames(cleanoutput) <- c("Input","ExpectedOutput","NeuralNetOutput")

ggplot(data = cleanoutput, aes(x= ExpectedOutput, y= NeuralNetOutput)) + geom_point() +
  geom_abline(intercept = 0, slope = 1
              , color="brown", size=0.5)

rmse <- sqrt(sum((sqrt(testdata)- net.results)^2)/length(net.results))

print(rmse)

在这里,当我 Hidden_​​Layer_1 1 Hidden_​​Layer_2 2 ,而 Threshold_Level 0.1 ,我的 rmse 生成的是 0.6717354

At here, when my Hidden_Layer_1 is 1, Hidden_Layer_2 is 2, and the Threshold_Level is 0.1, my rmse generated is 0.6717354.

让我们尝试另一个示例

当我的 Hidden_​​Layer_1 2 Hidden_​​Layer_2 3 ,而 Threshold_Level 0.2 ,我的 rmse 0.8355925

when my Hidden_Layer_1 is 2, Hidden_Layer_2 is 3, and the Threshold_Level is 0.2, my rmse generated is 0.8355925.

如何创建一个表,该表将自动计算当用户向 Hidden_​​Layer_1 Hidden_​​Layer_2 赋值时, rmse ,和阈值水平。 (我知道如何在Excel中执行此操作,但不是在 r 哈哈中执行的操作)

How can I create a table that will automatically calculate the value of rmse when user assign value to the Hidden_Layer_1, Hidden_Layer_2, and Threshold_Level. ( I know how to do it in Excel but not in r haha )

所需的表应类似于

我希望我试用版 Hidden_​​Layer_1 Hidden_​​Layer_2 Threshold_Level rmse 在我的列中,通过输入一些 actionButton可以无限生成行数(如果可能),表示用户可以继续尝试,直到获得所需的 rmse

I wish that I have Trial(s), Hidden_Layer_1, Hidden_Layer_2, Threshold_Level, and rmse in my column, and the number of rows can be generated infinitely by entering some actionButton (if possible), means user can keep on trying until they got the rmse they desired.

我该怎么做?谁能帮我?我肯定会从此课程中学到东西,因为我对 r 还是很陌生。
非常感谢任何愿意帮助我的人。

How can I do that? Can anyone help me? I will definitely learn from this lesson as I am quite new to r. Thank you very much for anyone who willing to give a helping hand to me.

推荐答案

创建可以与数据框查看器一起显示的值表。

Here is a way to create the table of values that can be displayed with the data frame viewer.

# initialize an object where we can store the parameters as a data frame
data <- NULL

# function to receive a row of parameters and add them to the
# df argument
addModelElements <- function(df,trial,layer1,layer2,threshold,rmse){
     newRow <- data.frame(trial = trial,
                          Hidden_Layer_1 = layer1,
                          Hidden_Layer_2 = layer2,
                          Threshold = threshold,
                          RMSE = rmse)
     rbind(df,newRow)
}

# once a model has been run, call addModelElements() with the 
# model parameters 
data <- addModelElements(data,1,1,2,0.1,0.671735)
data <- addModelElements(data,2,2,3,0.2,0.835593)

...以及输出:

View(data)

请注意,如果您将要创建分数或数百行参数&在将RMSE结果显示给最终用户之前,应更改代码以提高 rbind()的效率。在这种情况下,我们建立了一组参数列表,将它们转换为数据帧,并使用 do.call()执行 rbind() 仅一次。

Note that if you're going to create scores or hundreds of rows of parameters & RMSE results before displaying any of them to the end user, the code should be altered to improve the efficiency of rbind(). In this scenario, we build a list of sets of parameters, convert them into data frames, and use do.call() to execute rbind() only once.

# version that improves efficiency of `rbind()
addModelElements <- function(trial,layer1,layer2,threshold,rmse){
     # return row as data frame
     data.frame(trial = trial,
                Hidden_Layer_1 = layer1,
                Hidden_Layer_2 = layer2,
                Threshold = threshold,
                RMSE = rmse)
}

# generate list of data frames and rbind() once

inputParms <- list(c(1,1,2,0.1,0.671735),
                   c(1,1,2,0.3,0.681935),
                   c(2,2,3,0.2,0.835593))

parmList <- lapply(inputParms,function(x){
     addModelElements(x[1],x[2],x[3],x[4],x[5])
})
# bind to single data frame
data <- do.call(rbind,parmList)
View(data)

...和输出:

这篇关于如何在R中创建公式化表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆