R code

R code for some examples in the videos:

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)