I am in the beginning stages of machine learning in R and I find it hard to believe that there are no packages to solving the cost function for different types of regression algorithms. For example, if I want to solve the cost function for a logistic regression, the manual way would be below:
https://www.r-bloggers.com/logistic-regression-with-r-step-by-step-implementation-part-2/
# Implement Sigmoid function
sigmoid <- function(z)
{
g <- 1/(1+exp(-z))
return(g)
}
#Cost Function
cost <- function(theta)
{
m <- nrow(X)
g <- sigmoid(X%*%theta)
J <- (1/m)*sum((-Y*log(g)) - ((1-Y)*log(1-g)))
return(J)
}
##Intial theta
initial_theta <- rep(0,ncol(X))
#Cost at inital theta
cost(initial_theta)
In the glm function is there a way to automatically do this? Or for each algorithm that I apply, do I need to manually do it like this?
glmnet
function from packageglmnet
i.e.library(glmnet);x=matrix(rnorm(100*20),100,20);g2=sample(1:2,100,replace=TRUE);fit2=glmnet(x,g2,family="binomial")
– Rhizomorphous