[R-sig-teaching] [jones maybe from reed.edu: Re: (no subject)]

User Hayden bob @ending from @t@tl@nd@org
Sun Jul 29 13:42:54 CEST 2018


To be specific, this list is about using R for teaching.  Appropriate
content might include things like sharing R tutorials for beginners,
ideas for assignments or grading policies, educational simulations or
demos using R, etc.  How-To questions about using R should go to
r-help.  The bulk of posts here are not suitable for this list and I
wonder how people end up here.  When a person joins, do they get an
initial email discribing what is appropriate content for this list,
with suggestions of where to go for other sorts of questions?  This
might reduce the traffic in inappropriate questions, and notifications
of same, and help those seeking help get to a place where they might
actually get it;-)


----- Forwarded message from Albyn Jones <jones using reed.edu> -----

Date: Sat, 28 Jul 2018 22:12:31 -0700
From: Albyn Jones <jones using reed.edu>
To: Baojun Sun <bsun1 using students.towson.edu>
Cc: R-sig-teaching <r-sig-teaching using r-project.org>
Subject: Re: [R-sig-teaching] (no subject)

Is this a question about using R to teach statistical learning, or a
question for r-help, or homework?

On Sat, Jul 28, 2018 at 7:46 AM, Baojun Sun <bsun1 using students.towson.edu>
wrote:

> The book "Introduction to Statistical Learning" gives R scripts for its
> labs. I found a script for ridge regression that works on the dataset the
> book uses but is unusable on other datasets I own unless I clean the data.
>
>
> I'm trying to understand the syntax for I need for data cleaning and am
> stuck. I want to learn to do ridge regression. I tried using my own data
> set on this script rather than the book example but get errors. If you use
> your own data set rather than the Hitters dataset, then you'll get errors
> unless you format your code. How do I change this script or clean any
> dataset so that this script for ridge regression useable for all datasets?
>
>
>     library(ISLR)
>
>     fix(Hitters)
>
>     names(Hitters)
>
>     dim(Hitters)
>
>     sum(is.na(Hitters$Salary))
>
>     Hitters=na.omit(Hitters)
>
>     dim(Hitters)
>
>     sum(is.na(Hitters))
>
>     library(leaps)
>
>
>
>     x=model.matrix(Salary~.,Hitters)[,-1]
>
>     y=Hitters$Salary
>
>
>
>     # Ridge Regression
>
>
>
>     library(glmnet)
>
>     grid=10^seq(10,-2,length=100)
>
>     ridge.mod=glmnet(x,y,alpha=0,lambda=grid)
>
>     dim(coef(ridge.mod))
>
>     ridge.mod$lambda[50]
>
>     coef(ridge.mod)[,50]
>
>     sqrt(sum(coef(ridge.mod)[-1,50]^2))
>
>     ridge.mod$lambda[60]
>
>     coef(ridge.mod)[,60]
>
>     sqrt(sum(coef(ridge.mod)[-1,60]^2))
>
>     predict(ridge.mod,s=50,type="coefficients")[1:20,]
>
>     set.seed(1)
>
>     train=sample(1:nrow(x), nrow(x)/2)
>
>     test=(-train)
>
>     y.test=y[test]
>
>     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(y~x, subset=train)
>
>     predict(ridge.mod,s=0,exact=T,type="coefficients")[1:20,]
>
>     set.seed(1)
>
>     cv.out=cv.glmnet(x[train,],y[train],alpha=0)
>
>     plot(cv.out)
>
>     bestlam=cv.out$lambda.min
>
>     bestlam
>
>     ridge.pred=predict(ridge.mod,s=bestlam,newx=x[test,])
>
>     mean((ridge.pred-y.test)^2)
>
>     out=glmnet(x,y,alpha=0)
>
>     predict(out,type="coefficients",s=bestlam)[1:20,]
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-teaching using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
>

	[[alternative HTML version deleted]]

_______________________________________________
R-sig-teaching using r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-teaching

----- End forwarded message -----

-- 

      _
     | |          Robert W. Hayden
     | |          5 Howard Street, Apartment 206
    /  |          Wilton, New Hampshire 03086  USA
   |   |          
   |   |          email: bob@ the site below
  /    |          website: http://statland.org
 |   x /          
 ''''''



More information about the R-sig-teaching mailing list