How to create a formulated table in R?

吃可爱长大的小学妹 提交于 2020-05-14 11:57:57

问题


This is my reproducible example :

#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)

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.

Let's say we try for the other example,

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.

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 )

The desired table should be looked like this :

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.

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.


回答1:


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)

...and the output:

View(data)

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)

...and the output:



来源:https://stackoverflow.com/questions/61405882/how-to-create-a-formulated-table-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!