installLoadPkg("car")
installLoadPkg("blorr")
installLoadPkg("pscl")
installLoadPkg("survey")
installLoadPkg("pROC")
installLoadPkg("ROCR")
#see https://developers.google.com/machine-learning/crash-course/classification/roc-and-auc#:~:text=An%20ROC%20curve%20(receiver%20operating,False%20Positive%20Rate

source("http://ceresanalytics.com/R_for_IBF_BootCamp/installLoadPkg.r")
installLoadPkg("rgl")

#1. get launch data 
#   follow instructions at that refer you to "https://github.com/stedy/Machine-Learning-with-R-datasets/blob/master/challenger.csv"
#   and suggest clicking "raw" for this link:
launch<-read.csv("https://raw.githubusercontent.com/stedy/Machine-Learning-with-R-datasets/master/challenger.csv")
#	more detailed code at http://cox.csueastbay.edu/~esuess/classes/Statistics_452/Presentations/ml10a/MLwR_v2_06.r

#	a. print to check data
print(launch)
#	b. examine your structure
print(str(launch))

#2. Look at what you have--distress vs. temperature 
plot(x=launch$temperature,y=launch$distress_ct)
#	a. change your plotting character to a filled-in circle
plot(x=launch$temperature,y=launch$distress_ct,pch=19,main="Number of failures vs. temperature")

#3. for a binary outcome, change to "had any distress"
launch$hadAnyDistress<-ifelse(launch$distress_ct>0,1,0)

#	a. see it... 
par(mfrow=c(1,2))
	plot(x=launch$temperature,y=launch$distress_ct,pch=19,main="Distress Count vs. Temperature",col="blue",ylim=c(0,2))
	plot(x=launch$temperature,y=launch$hadAnyDistress,pch=19,main="Distress Yes/No vs. Temperature",col="red",ylim=c(0,2))
#	b. fit it 
myModel<-glm(
	formula=hadAnyDistress ~ temperature,
	data=launch,
	family=binomial(link="logit")
)
{
print(summary(myModel))
#pseudo-Rsquare
print("McFadden is pseudo-R-square:")
pR2(myModel)  # look for 'McFadden
}

#	c. plot it 
plot(launch$temperature,launch$hadAnyDistress,
		main="Initial Model",
		col="red",
		pch=19)
curve(expr=
	predict(
		object=myModel,
		newdata=data.frame(temperature=x),
		type="resp"
	),
add=TRUE,
col="blue" 
)

#4. make ROC curve and AUC 

prob <- predict(myModel, newdata=launch, type="response")
pred <- prediction(prob, launch$hadAnyDistress)
perf <- performance(pred, measure = "tpr", x.measure = "fpr") #true positive vs. false positive 
auc <- performance(pred, measure = "auc")
auc <- auc@y.values[[1]]
auc
plot(perf,col="darkgreen",lwd=3,main=paste0("ROC Curve for Challenger Data\nAUC=",round(auc,4)),sub=("Note: All Data used for Training"))
## [1] 0.6540625


#5. EXPLORE impacts:  Intercept  

#	a. intercept:  make EVERY temperature be 10 degrees higher 
#		i. copy dataset 
launch_1<-launch
#		ii. in new dataset, simply pretend that temperature was 30 degrees higher.  
launch_1$temperature<-launch_1$temperature + 10


	#	c. make new model with 30-degree addend to temperature.  Note that slope and its precision are the same!
myModel_int<-glm(
	formula=hadAnyDistress ~ temperature,
	data=launch_1,
	family=binomial(link="logit")
)
print(summary(myModel_int))

#	b. compare "before" and "after" plots for your 10-degree change 
par(mfrow=c(1,2))
	#before 
	plot(
		x=launch$temperature,
		y=launch$hadAnyDistress,
		pch=19,
		main="Original\nDistress Yes/No vs. Temperature",
		col="red",
		xlim=c(min(launch$temperature),max(launch_1$temperature))
	)
	curve(expr=
		predict(
			object=myModel,
			newdata=data.frame(temperature=x),
			type="resp"
		),
		add=TRUE,
		col="blue" 
	)
	plot(
		x=launch_1$temperature,
		y=launch_1$hadAnyDistress,
		pch=19,
		main="Increase Temp by 10\nDistress Yes/No vs. Temperature",
		col="red4",
		xlim=c(min(launch$temperature),max(launch_1$temperature))
	)
	curve(expr=
		predict(
			object=myModel_int,
			newdata=data.frame(temperature=x),
			type="resp"
		),
		add=TRUE,
		col="blue4" 
	)

