Answer To: Question 1 In maximum likelihood estimation, the estimator is obtained by maximizing the log...
Sanchi answered on Feb 15 2021
#Question 1
func <- function(x) {(2*(x^2)-1)/(1+(x^3))}
derivfunc <- function(x) {2*(2(x^6)-6(x^4)-14(x^3)+3*x+2)/(1+(x^3))^3}
install.packages("animation")
library(animation)
x0=seq(0,2,,100)
plot(x0,func(x0),type="n")
lines(x0,func(x0))
abline(h=0,lty=2)
ani.options(interval = 0.5)
newton.method(function(x) (2*(x^2)-1)/(1+(x^3)),1,c(0,2))
#Question2
install.packages("ISLR")
library(ISLR)
library(glmnet)
library(dplyr)
library(tidyr)
data <- read.csv("C:\\Users\\sanchi.kalra\\Desktop\\Greynodes\\AS11\\data_16.csv")
grid = 10^seq(10, -2, length = 100)
#RIDGE
set.seed(1)
train = data %>%
sample_frac(0.5)
test = data %>%
setdiff(train)
x_train = model.matrix(sales~., train)[,-1]
x_test = model.matrix(sales~., test)[,-1]
y_train = train %>%
select(sales) %>%
unlist() %>%
as.numeric()
y_test = test %>%
select(sales) %>%
unlist() %>%
as.numeric()
ridge_mod = glmnet(x_train, y_train, alpha=0, lambda = grid, thresh = 1e-12)
ridge_pred = predict(ridge_mod, s = 4, newx = x_test)
mean((ridge_pred - y_test)^2)
mean((mean(y_train) - y_test)^2)
ridge_pred = predict(ridge_mod, s = 1e10, newx = x_test)
mean((ridge_pred - y_test)^2)
ridge_pred = predict(ridge_mod, s = 0, newx = x_test, exact = T)
mean((ridge_pred - y_test)^2)
lm(sales~., data = train)
predict(ridge_mod, s = 0, type="coefficients")[1:3,]
set.seed(1)
cv.out =...