R code for some examples in the videos:
Code for recurrent neural network (RNN). Note that this code is for educational purposes only and is therefore not intended to be used to predict the stock market.
library(rnn) rm(list=ls()) y = c(9, 7, 6, 10, 8, 7, 11, 9, 6, 12, 10, 7, 11, 9, 7) # Time series data yn = (y - min(y)) / (max(y) - min(y)) plot(yn,type="l", las=1, xlab="Time", ylab="Normalized data") points(yn,pch=16) step=5 # Time steps train_size=9 # Numb of time points to train # Prepare training data X_train = NULL Y_train = NULL i=1 for (k in step:train_size){ X_train[[i]]=yn[(k-step+1):k] Y_train[[i]]=yn[(k-step+2):(k+1)] i=i+1 } X_train=do.call(rbind, X_train) Y_train=do.call(rbind, Y_train) XM=array(X_train,dim=c(dim(X_train)[1],step,1)) # Data, samples, time, variables YM=array(Y_train,dim=c(dim(Y_train)[1],step,1)) # Data, samples, time, variables set.seed(3) model = trainr(Y = YM,X = XM, learningrate = 0.1, hidden_dim = 10, numepochs = 15000, use_bias = TRUE) # Predict training data and plot train=yn[1:train_size] # Train data xp=array(train,dim=c(1,length(train),1)) # Prepare traning data to predict Yp_train = as.vector(predictr(model, xp)) # Predict traning data t2=2:(length(Yp_train)+1) # Time steps for plot lines(t2, Yp_train, type = "l", col = "red") points(t2, Yp_train,pch=16,col="red") # Predict validation data and plot X_valid=yn[(train_size+1):(length(yn)-1)]# Validation data xpv=array(X_valid,dim=c(1,length(X_valid),1)) # Prepare validation data to predict Yp_valid = as.vector(predictr(model, xpv)) t3=(t2[length(t2)]+1):length(yn) lines(t3, Yp_valid, type = "l", col = "green") points(t3, Yp_valid,pch=16,col="green") #Calculate SSE of validation data Y_valid=yn[(train_size+2):(length(yn))]# Validation data sum((Yp_valid-Y_valid)^2)
Statistical power – Parametric vs Nonparametric test
k=100000 # Number of simulations n=5 # Sample size p_values=rep(0,k) # Initialize vector p_valuesWMW=rep(0,k) # Initialize vector for (i in 1:k){ X1=rnorm(n,0,1) # Group A X2=rnorm(n,0.3,1) # Group B p_values[i]=t.test(X1,X2,var.equal=TRUE)$p.val p_valuesWMW[i]=wilcox.test(X1,X2)$p.val } power_t_test=sum(p_values<0.05)/k round(power_t_test,2)*100 power_WMW_test=sum(p_valuesWMW<0.05)/k round(power_WMW_test,2)*100
How to solve ordinary differential equations (ODEs) in R (deSolve)
library(deSolve) rm(list=ls()) # Clear the memory ############# ODE function ######### my_ode=function(t,state,parms){ with(as.list(state),{ dndt=rep(0,length(state)) #-------My Equations---------------- dndt[1] = -k*N #------------------------------------- return(list(dndt)) # Return }) } ############ END of function ########## N=70 # Initial value = 70 mg init=c(N=N) # Create a vector with initial values k=0.2 # Exponential decay constant (/h) t=seq(0,30,1) # Run for 30 time steps out = ode(y =init, times = t, func = my_ode, parms = NULL) plot(out,type="l",xlab="Time (h)",ylab="Caffeine (mg)")
Neural networks with continuous output | ANN vs Regression
rm(list=ls()) normalize <- function(x) { return ((x - min(x)) / (max(x) - min(x))) } Age=c(1,2,2,3,4,5,6,7,8,9,10,10,11) Price=c(29,25,21,18,15,15,12,10,7,5,6,4,4) df=data.frame(Age,Price,Price_n=normalize(Price),Age_n=normalize(Age)) plot(Age, Price, ylab="Price",xlab="Age",col="blue",cex=1.2,ylim=c(0,35),xlim=c(0,12)) set.seed(378) # To get the same numbers as in my example library(neuralnet) nn <- neuralnet(Price_n ~ Age_n, data=df, hidden=c(2), linear.output=TRUE, threshold=1e-4,rep=20,act.fct = "logistic") i=which.min(nn$result.matrix[1,]) # Select network with the lowest error x=seq(0,1,0.01) df2=data.frame(Age=x) yy=predict(nn,df2, rep = i)* abs(diff(range(Price))) + min(Price)# Change back to original scale tt=x*abs(diff(range(Age))) + min(Age)# Change back to original scale lines(tt,yy) yhat=predict(nn,data.frame(Age=normalize(Age)), rep = i)* abs(diff(range(Price))) + min(Price) (SSE=sum((Price-yhat)^2)) plot(nn,rep=i)