Skip to content

Instantly share code, notes, and snippets.

Selva Prabhakaran selva86

View GitHub Profile
View challenge_measures_of_goodness_of_fit.R
# Which amongst the following models is better in terms of:
# R-Squared, Adj R-Squared, AIC and BIC
model1 <- lm(mpg ~ wt + hp, data=mtcars)
model2 <- lm(mpg ~ wt + cyl, data=mtcars)
model3 <- lm(mpg ~ qsec + hp, data=mtcars)
@selva86
selva86 / ks_plot.R
Last active Jun 3, 2020
Function to create KS Plot using InformationValue R package
View ks_plot.R
require(ggplot2)
ks_table <- InformationValue:::ks_table
ks_plot <- function (actuals, predictedScores) {
rank <- 0:10
ks_table_out <- ks_table(actuals = actuals, predictedScores = predictedScores)
perc_events <- c(0, ks_table_out$cum_perc_responders) * 100
perc_nonevents <- c(0, ks_table_out$cum_perc_non_responders) * 100
random_prediction <- seq(0, 100, 10)
View facet_grid_challenge.R
# Input
library(ggplot2)
data(mpg, package="ggplot2")
mpg <- read.csv("http://goo.gl/uEeRGu")
g <- ggplot(mpg, aes(x=displ, y=hwy)) +
geom_point() +
geom_smooth(method="lm") +
theme_bw()
plot(g)
View legend challenge.R
# Modify the following code to remove the legend title. Then, place the legend in top left position
gg <- ggplot(midwest, aes(x=area, y=poptotal)) + # Define Data
geom_point(aes(col=state, size=popdensity)) + # Add scatterplot
geom_smooth(method="lm", col="firebrick", se=F) + # Add best fit line
coord_cartesian(xlim=c(0, 0.1), ylim=c(0, 250000)) + # Limit X and Y axis
labs(title="Area Vs Population", y="Population", x="Area") # Labels
plot(gg)
View annotations_challenge.R
# Run the below code to get the graph. The graph represents speed of car
# vs distance it travels before stopping (when brakes are applied)
# In this graph, write an insight stating that "The faster the car,
# the longer it takes to stop"
gg <- ggplot(data=cars, aes(x=speed, y=dist, size=dist)) +
geom_point() +
geom_smooth() +
labs(title="Cars", x="Speed", y="Dist")
print(gg)
View fast loops with set.R
set.seed(100)
M <- matrix(round(runif(100*100), 2), nrow=100, ncol=100)
df <- as.data.frame(M)
df[1:5, 1:5]
# 1. Replace the diagonal of dataframe `df` to the respective row number
# using the set function.
# 2. Set the values of column V1 to values of column V2, for all rows where V2 is
View dplyr_join_challenges.R
set.seed(100)
df1 <- data.frame(a=rep(1:3, each=4), b=round(runif(12, 10, 20)))
df2 <- data.frame(a=0:1, c=10:11)
# You have two dataframe df1 and df2 from the code below. df1 and df2 have column a in common.
# The challenge for this video is as follows:
# 1. From df1, remove the rows that are present in df2, based on the common column a.
# 2. Create another dataframe df3 that contains all the rows and columns from both datasets.
View error_handling_challenge_2.R
# Add error handling to `largest_hypotenuse()` so it ignores incorrect cases and captures largest
# hypotenuse for eligible cases.
source('https://bit.ly/2w2WA9k')
# OR
hypotenuse <- function(side1, side2){
side1 <- as.numeric(side1); side2 <- as.numeric(side2)
sqrt(side1^2 + side2^2)
View Debug_challenge.R
print_1_to_10 <- function(){
for(i in 1:10){
print(i)
}
}
View 8_3_Challenge_R_Course.R
# Input
set.seed(100)
first <- sample(10:20, 15, replace = T) # first sides of RHT
second <- sample(10:20, 15, replace = T) # second sides of RHT
# Final Result updates here
largest_val <- 0
# Calc Hypotenuse
hypotenuse <- function(side1, side2){
You can’t perform that action at this time.