How do I start using a trained neural network in R . When I try to put new data I get an error message that "y" is missing. Of course its missing, "y" is the dependent variable I want predicted.
Here is the code I used for training the model
library(neuralnet)
getwd()
d1 = read.csv("mydata.csv")
d1_dummy<- d1 %>% dplyr::rename( y=TOT )
d1_dummy
normalize <- function(x)
{maxval<<-max(x)
minval<<-min(x)
return ((x -minval) / (maxval -minval))}
denormalize <- function(x) {return( x*(maxval-minval) + minval )}
ddnorm <- lapply(d1_dummy,normalize) %>% as_tibble()
f <- as.formula(paste( "y", paste( names(ddnorm)[ -ncol(ddnorm) ], collapse = " + ") ,sep=" ~ " ) )
m_4_f<-neuralnet::neuralnet( f,data=ddnorm, hidden=c(5,4,3), rep=5, err.fct="sse", act.fct= 'logistic' )
class(m_4_f)
m_4_pred<-predict(m_4_f,newdata=ddnorm) %>% sapply(denormalize)
gg_input<-tibble(Predict=m_4_pred,Actual=d1_dummy$y)
with(gg_input, cor(Predict,Actual) )
plot(m_4_pred,col="blue")
points(d1_dummy$y,col="red")
forecast::accuracy(m_4_pred,x=d1_dummy$y)
Answer
- The questioner was satisfied with and accepted the answer, or
- The answer was evaluated as being 100% correct by the judge.
-
I will try it now and let you know if that works, Thank You
-
Sounds good.
-
-
So after ddnorm <- lapply(d1_dummy,normalize) %>% as_tibble() Code should read f <- as.formula(paste( "y", paste( names(ddnorm)[ -ncol(ddnorm) ], collapse = " + ") ,sep=" ~ " ) ) m_4_f<-neuralnet::neuralnet( f,data=ddnorm, hidden=c(5,4,3), rep=5, err.fct="sse", act.fct= 'logistic' ) m_4_comp<-compute(m_4_f,) %>% sapply(denormalize) gg_input<-tibble(Compute=m_4_comp)
-
The compute() function requires input data (excluding y). Add these (add the first line, replace the one you have with the second line): new_data <- ddnorm %>% select(-y) m_4_comp <- compute(m_4_f, new_data)$net.result %>% sapply(denormalize)
-
-
> newdata = read.csv("LIVE2.csv") > d1_dummy<- newdata %>% dplyr::rename( y=TOT ) > normalize <- function(x) + {maxval<<-max(x) + minval<<-min(x) + return ((x -minval) / (maxval -minval)) + } > denormalize <- function(x) {return( x*(maxval-minval) + minval )} > ddnorm <- lapply(d1_dummy,normalize) %>% as_tibble() > f <- as.formula(paste( "y", paste( names(ddnorm)[ -ncol(ddnorm) ], collapse = " + ") ,sep=" ~ " ) ) >> new_data <- ddnorm %>% select(-y)
-
> m_4_comp <- compute(m_4_f, new_data)$net.result %>% sapply(denormalize) > gg_input<-tibble(Compute=m_4_comp) > print(compute) function (x, covariate, rep = 1) { pred <- predict.nn(x, newdata = covariate, rep = rep, all.units = TRUE) for (i in 1:(length(pred) - 1)) { pred[[i]] <- cbind(1, pred[[i]]) } list(neurons = pred[-length(pred)], net.result = pred[[length(pred)]]) } Is the result I got
-
Did it work?
-
-
Sorry it wont let me seperate the lines of code but, I still did it wrong and got the error message at the end............function (x, covariate, rep = 1) { pred <- predict.nn(x, newdata = covariate, rep = rep, all.units = TRUE) for (i in 1:(length(pred) - 1)) { pred[[i]] <- cbind(1, pred[[i]]) } list(neurons = pred[-length(pred)], net.result = pred[[length(pred)]]) }
-
Im new to this and its showing, could you write the code in order, thats giving me the most trouble. I will tip you extra for your time
-
Yes, it is really hard to follow these in a comment. I will write the code in order and add to the solution above (not in the comments).
-
Added. Please check.
-
-
Doing it right now
-
That works, THANK YOU SO MUCH
-
I put in a 20 dollar tip, thanks again
-
I don’t think the tip went through. Please check your account.
-
-
Great! Thank you.
-
For next times, you can ask a direct question. Click on my profile and then click on ask direct question.
-
You should have the tip now I will ask you direct from now for sure You were very helpful
-
Sounds good. For your next questions, to determine appropriate bounty, just remember that the website gets 20% of the bounty and tip as commission.
-
- answered
- 214 views
- $50.00
Related Questions
- Hi Sportspimp here, turns out that neuralnet no longer uses the compute command, just predict. I thought it worked but error message I thought I could research the fix but no luck. Back for more help
- Mathematical modeling
- Two statistics proofs with regressions, any help much appreciated!
- Interpretation of signifcance of continous by continous regression with interaction term
- Help structure linear mixed effects model random effects structure
- Use the Desmos graphing calculator to find slope and y-intercept for the least-squares regression line for the dataset in the table
- How can I calculate incremental growth rate using a logarithm regression analysis? I have count data from 1992-2022 of a species and want to calculate the growth rate on a 3 year moving average.
Could you upload the dataset?
Yes give me a moment
I have attached the training data
Ive also attached the new data (LIVE2.csv)
Did the uploads work?
Rephrase Did you receive the uploads?
I do not see any files. But no need for the files yet. I will tell you what’s not working with your code and how you can fix it.
On the plane now, will submit the solution later tonight.
Thank you very much