R Misc Flashcards

1
Q

What is the package that allows you to do ridge regression and lasso regression

A

library(‘glmnet’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to implement CV when you are doing linear or logistics regression?

A

cv.glmnet() is a function that will do CV for you.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Does glmnet library have predict function? What is glmnet for?

A

yes, it does however the regsubsets (for best subset, forward and backward LR) function does not have one.
Also glmnet is for lasso and ridge regression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to conduct lasso regression

A
library(glmnet)
# Do CV to get best tuning paramter
# Use alpha = 1 for lasso 
cv.out = cv.glmnet(x[train, ], y[train], alpha = 1)
bestlam = cv.out$lambda.min
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Print out character array one letter at at a time by looping

A

x = c(‘a’, ‘b’, ‘c’)
for(letter in x) {
print(letter)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

if control structure R

A
if() {
## do something
} else if() {
## do something different
} else {
## do something different
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

easiest to initiate an infinite loop (on purpose)

A

repeat{

if(condition) {
break
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to skip an interation in a loop

A
Use next
for(i in 1:100) {
if(i <= 20) {
## Skip the first 20 iterations
next
}
## Do something here
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

while loops

A

while(count < 10) {
print(count)
count <- count + 1
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to make a function

A
f ) {
## Do something interesting
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

get list of arguments for a function

A

args()

ex: args(lm)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

describe the lazy evaluations of functions

A

Arguments to functions are evaluated lazily, so they are evaluated only as needed.
f <- function(a, b) {
a^2
}
f(2)
This function never actually uses the argument b, so calling f(2) will not produce an
error because the 2 gets positionally matched to a.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does … mean in a function defintion

A
  1. when extending another function, but don’t want to copy entire argument list
  2. … so that extra arguments can be passed to methods
  3. … is necessary when the number of arguments cannot be known in advance, such as paste() and cat.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Display the list of environment variables and packages R will iterate through to find a variable, in the order they will be searched

A

search()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When user loads a library, where does it go on the search list?

A

it goes into second place, everything else goes down 1. the global environment of the users workspace is always number 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what are free variables?

A

f <- function(x, y) {
x^2 + y / z
}
This function has 2 formal arguments x and y. In the body of the function there is
another symbol z. In this case z is called a free variable.

17
Q

how do free variables get values?

A

they are not local arguments, can be defined outside the function. R will search for the free variable starting in the global environment

18
Q

Does R let you define a function inside another function?

A

Yes, lots of languages don’t let you do this

19
Q

Example of defining a function inside another function

A
make.power <- function(x) {
x^n
}
pow
}
20
Q

lapply vs sapply

A

lapply will apply a function over a list, whereas sapply will do the same thing, except simplify the output if possible

21
Q

apply -

A

str(apply)
function (X, MARGIN, FUN, …)
X is an Array
MARGIN is what margin should be retained - 1 is row, 2 is columns
FUN is what function do you want to apply
… is for other arguments to be passed to FUN

22
Q

shortcut functions equiavalent to apply

A
rowSums = apply(x, 1, sum)
rowMeans = apply(x, 1, mean)
colSums = apply(x, 2, sum)
colMeans = apply(x, 2, mean)
23
Q

Example of apply using the … arguments

A

apply(x, 1, quantile, probs = c(0.25, 0.75))

24
Q

tapply

A

used to apply a function over subset of a vector, like a group by statement, can apply a function over a factor

25
Q

split

A

can use to split a dataframe into pieces

s <- split(airquality, airquality$Month)

26
Q

return only complete cases of data frame

A

DataFrame[complete.cases(DataFrame), ]

na.omit(DataFrame)

27
Q

Add column to data frame

A

Carseats = data.frame(Carseats, High)

28
Q

convert confusion matrix table into percentages

A

prop.table(table(trainClass))

29
Q

Steps to follow when using the caret package

A
  1. partition data *createDataPartition()
  2. test for low variance variables *nearZeroVar()
  3. test for multicolinearity *findcorrelation()
  4. preprocess - center/scale and derive pca *preprocess()
  5. Build and tune model *train()
30
Q

count occurences of each factor in a column

A

table(dataframe$columnname)

31
Q

profile data

A

use describe() in psych package

32
Q

install packages using command

A

install.packages(‘psych’)

33
Q

return predicted values for linear regression

A

fitted(modelobject)

34
Q

get the 95% confidence interval for regression coeeficient

A

confint(modelobject)