Posts

Showing posts from March, 2022

Ridge regression in R step by step

  R Programming Code library(glmnet) #define predictor and response variables y <- mtcars$hp x <- data.matrix(mtcars[, c('mpg', 'wt', 'drat', 'qsec')]) #fit ridge regression model model <- glmnet(x, y, alpha = 0) summary(model) #perform k-fold cross-validation to find optimal lambda value cv_model <- cv.glmnet(x, y, alpha = 0) plot(cv_model) #find optimal lambda value that minimizes test MSE best_lambda <- cv_model$lambda.min best_lambda #produce Ridge trace plot plot(model, xvar = "lambda") #find coefficients of best model best_model <- glmnet(x, y, alpha = 0, lambda = best_lambda) coef(best_model) #calculate R-squared of model on training data y_predicted <- predict(model, s = best_lambda, newx = x) #find SST and SSE sst <- sum((y - mean(y))^2) sse <- sum((y_predicted - y)^2) #find R-Squared rsq <- 1 - sse / sst rsq

Lasso Regression in R (Step-by-Step)

  Lasso regression  is a method we can use to fit a regression model when  multicollinearity  is present in the data. In a nutshell, least squares regression tries to find coefficient estimates that minimize the sum of squared residuals (RSS): RSS = Σ(y i  – ŷ i )2 where: Σ : A greek symbol that means  sum y i : The actual response value for the i th  observation ŷ i : The predicted response value based on the multiple linear regression model Conversely, lasso regression seeks to minimize the following: RSS + λΣ|β j | where  j  ranges from 1 to  p  predictor variables and λ ≥ 0. This second term in the equation is known as a  shrinkage penalty . In lasso regression, we select a value for λ that produces the lowest possible test MSE (mean squared error). This tutorial provides a step-by-step example of how to perform lasso regression in R. Step 1: Load the Data For this example, we’ll use the ...

how to create simple scatter 3D chart

 library("car") data(iris) head(iris) library(rgl) sep.l <- iris$Sepal.Length sep.w <- iris$Sepal.Width pet.l <- iris$Petal.Length scatter3d(x = sep.l, y = pet.l, z = sep.w) scatter3d(x = sep.l, y = pet.l, z = sep.w,           point.col = "blue", surface=FALSE) scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species) scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species,           grid = FALSE) scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species,           grid = FALSE, fit = "quadratic") scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species,           grid = FALSE, surface = FALSE) scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species,           surface=FALSE, ellipsoid = TRUE) scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species,           surface=FALSE, gri...

How to create Animated 3d chart with R.

  The rgl package is the best option to build 3d charts in R. Please see this post for an introduction to 3d scatterplots using it. It also provides the plot3d() and play3d() functions that allow to animate the 3d chart, and eventually to export the result at a .gif format. Here is an application to the famous iris dataset, with a nice animated 3d scatterplot chart. Arguments x, y, z vectors of points to be plotted. Any reasonable way of defining the coordinates is acceptable. See the function  xyz.coords  for details. xlab, ylab, zlab labels for the coordinates. type For the default method, a single character indicating the type of item to plot. Supported types are: 'p' for points, 's' for spheres, 'l' for lines, 'h' for line segments from z = 0, and 'n' for nothing. For the mesh3d method, one of 'shade', 'wire', or 'dots'. Partial matching is used. col the color to be used for plotted...