ITEC 621-Homework 1-R Practice

Christian V. Marshall

2018-10-18

Q1

Write a simple R function named Area() that takes 2 values as parameters (representing the two sides of a rectangle) and returns the product of the two values (representing the rectangle’s area. Then use the functions print() and paste() to output this result: “The area of a rectangle of sides 6x4 is 24”, where 24 is calculated with the Area() function you just created

Area <- function(x,y) { A <- x*y print (paste(“The Area of the sides”,x,“X”,y," = “,A)) } Area(6,4)

Q2

Write a simple “for loop” for i from 1 to 10. In each loop pass, compute the area of a rectangle of sides i and i*2 (i.e., all rectangles have one side double the lenght than the other) and for each of the 10 rectangles display “The area of an 1 x 2 rectangle is 2” for i=1, “The area of an 2 x 4 rectangle is 8”, and so on (Hint: it is easier if you use the function you created in Q1 above).

Area.function <- function() { for(i in 1:10) z <- Area(i,i*2) }

Area.function()

Q3

Copy the Credit.csv data file to your working directory:Then read the Credit.csv data table into an object named “Credit”List the top 6 rows of the tableList the first 5 columns of the top 5 rows

Credit.csv <- read.csv(“C:/Users/Christian/Desktop/R/ITEC-620/Credit.csv”, header = TRUE) str(Credit.csv) head(Credit.csv) credit <- Credit.csv credit.1<- credit[1:5,1:5] credit.1

Q4

Create a simple linear model object with the lm() function to fit credit rating as a function of all available predictors and:Save the results in an object called fit.rating;Display the model summary results (summary() function)

fit.rating <- lm(credit) summary(fit.rating)

Q5

Write a simple linear model to predict credit ratings using the most significant predictors: Income, Limit, Cards, Married, Balance; and Display the regression summary results

credit.rating <- lm(X~Income+Limit+Cards+Married+Balance, data = credit) summary(credit.rating

Q6

Display the object class for Gender (i.e., Credit$Gender), Income and Cards

data.frame(credit) class(credit\(Gender) class(credit\)Income) class(credit$Cards)

Q7

Briefly answer: what do each of the classes obtained in Q6 for Gender, Income and Cards mean?

Factor is a yes or no (binary), numeric refers to decimal numbers and integers deal with whole numbers

Q8

Create a vector named “Income.vect” with data from the Income column Display the first 6 values of this vector

income.vect$Income[1:6]

Q9

Compute and display (separtely) the mean, minimum, maximum, standard deviation and variance for all the values in this income vector

mean(income.vect\(Income) min(income.vect\)Income) max(income.vect\(Income) sd(income.vect\)Income) var(income.vect$Income)

Q10

Create a vector called Income.stats with 5 values you computed above andDisplay these Income.stats values

income.stats <- c(mean(income.vect\(Income), max(income.vect\)Income), min(income.vect\(Income), sd(income.vect\)Income), var(income.vect$Income)) income.stats

Q11

Now give these elements these names: “Mean”, “Min”, “Max”, “StDev”, and “Var” and Display these values with their names

income.stats2 <- c(“mean”, “max”, “min”, “sd”, “var”) names(income.stats) <- income.stats2 income.stats

Q12

Suppose that you want to adjust income for inflation by 5%. Multiply the income values in this vector by 1.05 and Display the first 6 records of this computation

1.05*income.vect$Income -> newmoney newmoney[1:6]

Q13

Display a boxplot for the predictor “Income”

boxplot(income.vect$Income)

Q14

Display a boxplot of “Income” by “Gender”. No need to respond, but can you tell if there is gender income inequality in this data set?

boxplot(data = income.vect,Income~Gender)

Q15

Plot Credit Rating (Y axis) against Income (X axis), with respective labels “Income” and “Credit Rating

plot(data = income.vect, xlab = “Income”, ylab = “Credit Rating”, Income~Rating)

Q16

Display a histogram for Credit Rating, with the main title “Credit Rating Histogram” and X label “Rating

hist(data = credit, xlab = “Rating”, main = “Credit Rating Histogram”, credit$Rating)

Q17

Check the qqplot (using the qqnorm() function and qqline() to evaluate the normality of credit rating)

qqnorm(credit\(Rating) qqline(credit\)Rating)

Q18

Briefly answer: Do you think that this data is somewhat normally distributed? Why or why not?

Yes I do think that it is somewhat normally distributed but I believe it also skews to the left of the median

Q19

Install (only if not installed already) and load the {ggplot2} package and draw a histogram with the ggplot() function

ggplot(data = credit) + geom_histogram(aes(x = Rating))

Q20

Then draw a dual line plot with Credit Rating on the Y axis and Income on the X axis, separated by gender (i.e., facet_wrap())

ggplot(credit, aes(x=Income, y = Rating, group = Gender)) + geom_line() + facet_wrap(~ Gender)