Prescriptive analytics support business decision-making with data and statistical models. A flexible statistical software package that allows analytic researchers to apply and create new models is essential. R is an integrated suite of software routines that facilitates advanced analytics.

In this first homework, we will be learning some of the basics of the R command language to:

  1. Conduct basic calculations
  2. Visualize your data
  3. Summarize information from a survey dataset (Ford)

Compile your answers into a .html file and upload it to CarmenCanvas. The instructor will provide on how to compile a .html file. Insert an R code chunk when necessary to answer questions. Hide the results of the code when necessary.

0. We will explore different types of variables and data structure handled in R.

a. Variables of different types: numeric (integer, single/double precision) character, logical, complex etc
b. Data structures: Vector, Matrix, data frame
## Warning in cbind(V1, V4): number of rows of result is not a multiple of vector
## length (arg 2)

1. The following questions explore the use of arithmetic expressions utilizing operators in R for calculation.

a. Create two variables a and b, where a = 7 and b = 2 in the code chuck (below). What are the values of the following expressions? Compute and explain each in command lines (#).
  1. a+b
  2. a-b
  3. a*b
  4. a/b
  5. a%%b
  6. a%/%b
a = 7; b = 2
a + b # it sums vales in variable a and b.
a - b 
a*b
a/b
a%%b
a%/%b
b. Create a variable x comprised of the numbers 1, 2, 3, 4, 5, 6 using x <- c(1,2,3,4,5,6). Is x a vector? Explain the purpose or the result of each command in the command lines.
  1. x + 3
  2. x - 3
  3. x * 3
  4. x / 3
x <- c(1,2,3,4,5,6)
x + 3 # every different x is being added to three.
## [1] 4 5 6 7 8 9
x - 3 # every different x is being subtracted from three. 
## [1] -2 -1  0  1  2  3
x * 3 # every different x is being multiplied by three.  
## [1]  3  6  9 12 15 18
x / 3 # every different x is being divided by three. 
## [1] 0.3333333 0.6666667 1.0000000 1.3333333 1.6666667 2.0000000
c. What does sum(x) do?

Answer: returns the sum of all values present in its arguments.

’’’{r, echo=FALSE, results=’hide} sum(x)

?sum(x)
d. Create a variable y <- c(10,11,12,13,14,15) and evaluate the followings. Explain the purpose or the result of each command in the command lines.
  1. x+y
  2. x-y
  3. x*y
  4. x/y
  5. (x+3)*y
y <- c(10,11,12,13,14,15)
x + y # each x is being added to each y.
## [1] 11 13 15 17 19 21
x - y # each x is being subtracted from each y.
## [1] -9 -9 -9 -9 -9 -9
x * y # each x is being multiplied by each y.
## [1] 10 22 36 52 70 90
x / y # each x is being divided by each y.
## [1] 0.1000000 0.1818182 0.2500000 0.3076923 0.3571429 0.4000000
(x + 3) * y
## [1]  40  55  72  91 112 135
e. Compute \(\sum_{i=1}^6 x_i y_i\) using R commands used in previous parts.

\[\sum_{i=1}^6 x_i y_i \]

sum(x*y)
## [1] 280
f. Create a variable r that contains 6 random numbers sampled from the standard normal distribution.
set.seed(3887) # to keep numbers consistent, each number will has its own specific sample
r = rnorm(6) # number of of observations, vector of means, vector of sd

# r = rnorm(1000, mean = 2, sd = .2)
# hist(r)
g. Create two variables t1 and t2 that contain 6 random numbers generated from a t-distribution with degrees of freedom of your choice.
set.seed(1234)
t1 = rt(6,3)
t2 = rt(6,10) # it generates 6 random numbers from a t distribution with df = 10

t3= rt(1000,100) # it generates 1000 random numbers from a t distribution with df = 100
hist(t3)

h. Run a multiple linear regression using the following model:

\[r = \beta_0 + \beta_1 t_1 + \beta_2 t_2 + \varepsilon.\]

Report the model fit.

lm.fit= lm( r~t1+t2 )
# lm - linear model 
# syntax, dependent variable ~ independent variables
# To fit this model, as an example, y = beta0 + beta 1 * x + epsilon, y = dv, x = iv
# lm (y ~ x)
# y = beta 0 + beta 1 * x1 + beta 2 * x2 + epsilon 
# y = dv, x1, x2 = ivs 
# lm( y ~ x1 + x2)

summary (lm.fit)
## 
## Call:
## lm(formula = r ~ t1 + t2)
## 
## Residuals:
##       1       2       3       4       5       6 
##  0.2852  0.8997 -0.6218 -0.3165 -0.4808  0.2342 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   0.3922     0.3486   1.125    0.342
## t1            0.4464     0.3837   1.163    0.329
## t2            0.5544     0.6804   0.815    0.475
## 
## Residual standard error: 0.7447 on 3 degrees of freedom
## Multiple R-squared:  0.311,  Adjusted R-squared:  -0.1483 
## F-statistic: 0.6771 on 2 and 3 DF,  p-value: 0.5719

In-class comment. \[r = \beta_0 + \beta_1 t_1 + \beta_2 t_2 + \varepsilon.\]
is an example of a simple linear regression model.

i. Provide general interpretations for each estimate in Part h.
j. Create a vector that contains 100 elements with integers from 1~100. (Hint: use ?seq for help). Use the option results = ’hide” in the code chuck to hide the result.
V1<- c(1:100)
V1
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100
k. Create a vector that contains 100 elements with value of 1. (Hint: use ?rep for help). Use the option results = ’hide” in the code chuck to hide the result.
V1 <- rep_len(1:1, 100)
V1
##   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [75] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

2. The following questions explore the calculation of matrices.

a. Is variable x, y, and r created in question 1 a matrix, respectively? (Hint: use ?is.matrix for help)

False # is.matrix(x) # is matrix(y) # is matrix(r)

b. Convert x into a matrix with dimension of \(1\times 6\).
x.matrix.version = matrix(x, nrow = 1, ncol = 6)
c. Convert y into a matrix with dimension of \(1\times 6\).
y.matrix.version = matrix(y, nrow =1, ncol = 6)
y <- matrix(y, nrow = 1, ncol = 6)
d. Can you calculate x %*% y? If not, why?
# here the inner dimensions don't match. So we can't compute the matrix multiplication. 
e. Let z be the transpose of matrix y. Now, can you calculate x %*% z? If yes, why?**
z <- t(y)
# x (1 by 6) times z (6 by 1)
# since the inner dimensions match, we can compute x %*% z 
x %*% z
##      [,1]
## [1,]  280
f-i. Convert x into a matrix with dimension of \(2 \times 3\) with byrow logic set to be TRUE.
x <- matrix(x , nrow = 2, ncol = 3, byrow = TRUE)
f-ii. Convert x into a matrix with dimension of \(2 \times 3\) with byrow logic set to be FALSE.
x <- matrix(x , nrow = 2, ncol = 3, byrow = FALSE)
f-iii. What is the difference between your output from i. and ii. ?

In i, the matrix is is filled by columns and in ii, the matrix is filled by rows.

3. This question explores visualization of your result using R command.

a. Create a variables h containing 20 random numbers generated from a normal distribution.
set.seed(1234)
h <- rnorm(20, mean = 1, sd = 2)
b. Generate a histogram of h using the hist() function.
hist(h)

c. Let v be equal to sin(h) and create a line graph using the plot() function. Set the horizontal axis as h and the vertical axis as v.
h <- sort(h)
v <- sin(h)
plot(h, v, pch = 16, col = 'red', cex = 0.5, xlim = c(-6,6), ylim=c(-2, 2) , asp = 1, 'l' , lty =3)

v2 <- cos(h)
points(h, v2, pch = 15, cex = 0.3)

# lines (h, v2, lty = 3)
# pch = point characters, changes shape (any number 1-16) 
# col = color of points
# cex = sixe of points, 1 is normal

4. The following questions explore some R commands using the Ford dataset.

The Ford data set (Ford_Data.csv) provides information on a cross-section of n = 547 respondents within the automobile industry. The corresponding survey instrument is in file: FordSurveyInstrument.docx which includes i) a consent script, ii) screening questions (with screening logic), iii) general questions, iv) glossary, v) conjoint exercises (i.e., Premium, Safety Warnings, Driver Assistance package), and vi) demographic questions.

In this question, we will be exploring the given dataset, employing functions for summarization, and utilizing functions for visualization. Once the data (Ford_Data.csv) has been downloaded from Carmen, set the working directory and proceed to answer the following questions.

a. Read the Ford dataset (Ford_Data.csv) into your R workspace using the following command.
Forddata = read.csv("Ford_Data.csv",sep=",",header = TRUE)
is.data.frame(Forddata)
## [1] TRUE
b. How many variables are in the data?
c. What are the variable names? To answer, print the first ten names.
names(Forddata) [1:10]
##  [1] "No"                 "ID"                 "RespondentID"      
##  [4] "StartTime"          "EndTime"            "ILength"           
##  [7] "SampleRespondentID" "Status"             "S0"                
## [10] "S1_1"
# first 10 variable names 
d. How many columns and rows are there in this dataset? What do those numbers represent? (Hint: use ?str and ?dim for help)

DONE

str(Forddata)
## 'data.frame':    547 obs. of  248 variables:
##  $ No                    : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ ID                    : int  1 2 3 5 7 8 9 11 14 16 ...
##  $ RespondentID          : int  28966548 28966673 28966708 28966768 28966799 28966802 28966805 28966867 28966893 28966905 ...
##  $ StartTime             : chr  "4/6/20 19:01" "4/6/20 21:19" "4/6/20 21:54" "4/6/20 23:02" ...
##  $ EndTime               : chr  "4/6/20 19:27" "4/6/20 21:51" "4/6/20 22:07" "4/6/20 23:16" ...
##  $ ILength               : int  1567 1897 765 813 932 489 1334 1200 1159 667 ...
##  $ SampleRespondentID    : int  1504513160 1501023933 1613146851 1727236699 1637965116 1650480152 1502482343 1311911912 1436839890 1585183295 ...
##  $ Status                : int  3 3 3 3 3 3 3 3 3 3 ...
##  $ S0                    : int  3 3 3 3 3 3 3 3 3 3 ...
##  $ S1_1                  : int  2 1 1 1 1 3 1 3 1 2 ...
##  $ S1_2                  : int  3 3 3 3 3 3 1 3 1 2 ...
##  $ S1_3                  : int  2 2 2 2 1 2 1 3 1 1 ...
##  $ S1_4                  : int  1 1 2 1 3 2 1 3 2 2 ...
##  $ S1_5                  : int  1 2 1 2 3 3 1 3 2 3 ...
##  $ S1_6                  : int  1 1 3 3 3 2 2 3 3 3 ...
##  $ S1_7                  : int  3 3 3 2 3 3 3 3 3 3 ...
##  $ S2                    : int  4 3 2 1 1 3 2 1 2 3 ...
##  $ S3                    : int  3 2 1 1 2 1 1 1 2 1 ...
##  $ S4_1                  : int  0 1 0 0 0 0 0 0 0 1 ...
##  $ S4_2                  : int  0 0 1 0 0 0 0 0 0 1 ...
##  $ S4_3                  : int  0 0 1 0 0 0 0 0 0 1 ...
##  $ S4_4                  : int  0 0 0 0 0 0 0 0 1 0 ...
##  $ S4_5                  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ S4_6                  : int  1 1 1 0 0 0 0 0 0 0 ...
##  $ S4_7                  : int  0 0 1 0 1 1 1 1 1 1 ...
##  $ S4_8                  : int  0 0 0 1 0 0 0 0 0 0 ...
##  $ S4_9                  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ S4_10                 : int  0 0 0 0 0 0 0 0 1 0 ...
##  $ S4_11                 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ S4_12                 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ D1_1                  : int  5 5 3 3 5 4 3 5 5 5 ...
##  $ D1_2                  : int  5 5 4 3 5 5 3 5 5 4 ...
##  $ D1_3                  : int  5 5 4 5 5 4 5 5 5 5 ...
##  $ D1_4                  : int  5 4 3 4 5 5 3 5 5 4 ...
##  $ D2                    : int  11 9 2 3 3 3 11 7 9 3 ...
##  $ D3                    : int  1 2 2 1 2 2 1 1 2 2 ...
##  $ D4                    : int  1 1 2 1 1 2 2 2 1 1 ...
##  $ D5                    : int  2 2 2 2 4 1 1 4 2 3 ...
##  $ D6                    : int  3 1 1 1 1 1 3 1 2 1 ...
##  $ D7                    : int  2 4 3 3 3 2 2 2 2 2 ...
##  $ D8                    : int  5 5 2 2 5 5 2 2 2 3 ...
##  $ D9                    : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ D10                   : int  1 3 1 1 5 3 2 2 1 5 ...
##  $ S5_Year               : int  32 26 24 25 28 27 16 27 30 31 ...
##  $ S5_Price              : int  29000 38000 10000 23000 65000 67000 45000 35000 38000 40000 ...
##  $ Q1                    : int  30000 55000 15000 34000 70000 70000 30000 35000 46000 50000 ...
##  $ Q2_1                  : int  0 0 0 0 1 0 0 0 0 0 ...
##  $ Q2_2                  : int  0 1 0 0 1 0 1 0 0 0 ...
##  $ Q2_3                  : int  0 0 0 0 0 0 0 0 0 1 ...
##  $ Q2_4                  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Q2_5                  : int  0 0 0 0 1 0 1 0 1 0 ...
##  $ Q2_6                  : int  0 1 1 1 0 0 1 1 1 0 ...
##  $ Q2_7                  : int  0 1 0 0 1 1 1 0 1 0 ...
##  $ Q2_8                  : int  0 0 1 1 0 0 0 0 0 0 ...
##  $ Q2_9                  : int  0 1 0 0 0 0 0 0 0 0 ...
##  $ Q2_10                 : int  0 0 1 0 0 0 0 0 1 0 ...
##  $ Q2_11                 : int  0 1 1 0 1 0 1 1 1 0 ...
##  $ Q2_12                 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Q2_13                 : int  0 1 1 0 1 0 1 0 1 0 ...
##  $ Q2_14                 : int  0 1 1 1 1 0 1 0 1 0 ...
##  $ Q2_15                 : int  0 0 0 0 0 0 1 0 0 0 ...
##  $ Q2_16                 : int  1 1 1 1 1 1 1 1 1 0 ...
##  $ Q2_17                 : int  0 1 0 0 1 0 1 0 1 0 ...
##  $ Q2_99                 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Q2_97                 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Q3_1                  : int  0 1 1 0 1 0 1 0 0 0 ...
##  $ Q3_2                  : int  0 0 0 0 1 0 1 0 0 0 ...
##  $ Q3_3                  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Q3_4                  : int  0 1 0 1 0 0 0 0 0 0 ...
##  $ Q3_5                  : int  0 1 0 0 0 0 0 1 1 0 ...
##  $ Q3_6                  : int  0 1 0 1 1 0 0 1 0 0 ...
##  $ Q3_7                  : int  0 0 1 0 0 0 1 0 1 0 ...
##  $ Q3_8                  : int  0 0 0 1 0 0 0 0 0 0 ...
##  $ Q3_9                  : int  0 0 0 0 0 1 0 1 0 0 ...
##  $ Q3_10                 : int  0 0 0 0 0 0 0 0 0 1 ...
##  $ Q3_11                 : int  0 1 0 0 0 0 0 0 0 0 ...
##  $ Q3_12                 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Q3_99                 : int  1 0 0 0 0 0 0 0 0 0 ...
##  $ Q3_97                 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ HP_DCVersion1         : int  1 4 2 6 9 8 10 7 1 9 ...
##  $ Q4_1                  : int  1 6 3 5 5 3 6 4 1 2 ...
##  $ Q5_1                  : int  1 0 1 1 1 1 0 1 1 1 ...
##  $ Q4_2                  : int  5 1 4 6 4 4 6 3 4 3 ...
##  $ Q5_2                  : int  1 1 1 0 1 1 0 1 1 1 ...
##  $ Q4_3                  : int  2 4 4 4 1 4 5 5 2 5 ...
##  $ Q5_3                  : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ Q4_4                  : int  4 6 1 4 1 4 1 4 2 1 ...
##  $ Q5_4                  : int  1 0 1 1 1 1 1 1 1 1 ...
##  $ Q4_5                  : int  4 6 1 1 4 2 3 2 3 4 ...
##  $ Q5_5                  : int  1 0 1 1 1 1 1 1 1 1 ...
##  $ Q4_6                  : int  3 3 2 2 1 3 4 5 3 1 ...
##  $ Q5_6                  : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ Q4_7                  : int  4 6 2 6 4 5 1 3 2 2 ...
##  $ Q5_7                  : int  1 0 1 0 1 1 1 1 1 1 ...
##  $ Q4_8                  : int  2 6 2 6 5 4 6 5 3 3 ...
##  $ Q5_8                  : int  1 0 1 0 1 1 0 1 1 1 ...
##  $ Q4_9                  : int  5 6 2 6 6 2 6 1 5 2 ...
##  $ Q5_9                  : int  1 0 1 0 0 1 0 1 1 1 ...
##  $ Q4_10                 : int  1 4 1 1 3 2 2 1 2 1 ...
##   [list output truncated]
# gives you names and type of data
e. What are the meanings of Forddata[1,], Forddata[,6], and Forddata[1, 6]?
# Forddata[1,] is respondent one
# Forddata[,6] is answer six 
# Forddata[1, 6] is respondednt one and answer six 
f. Create a variable respt that contains the total response time (ILength) of all respondents using command $. What is the average response time?
respt <- Forddata$ILength
mean(respt) # mean response time, 1535.201 seconds 
## [1] 1535.201
g. Create a data frame time that contains the StartTime and EndTime of all respondents.
time <- data.frame( Forddata$StartTime, Forddata$EndTime)
dim(time)
## [1] 547   2
names(time) <- c( "StartTime", "EndTime")
names(time)
## [1] "StartTime" "EndTime"
h. Create a subset budget that contain the Purchase Price (S5_Price) and Purchase Budget (Q1) of all respondents. What is the average Purchase Budget and Purchase price of the respondents?
budget <- data.frame( Forddata$S5_Price, Forddata$Q1)
names(budget) <- c("Price", "Budget")
names(budget)
## [1] "Price"  "Budget"
mean(budget$Price); mean(budget$Budget)
## [1] 30966.61
## [1] 41428.88
i. Find the number of respondents in the data set who have shopped for a laptop computer in the past 6 or 7 months. A good way to describe discrete data is with frequency counts. The table() function counts the observed prevalence of each value that occurs in a variable. Use the function table() to find the number of respondents who have shopped for a laptop computer in the past 6 or 7 months. Refer to FordSurveyInstrument.docx when necessary.
typeof(Forddata$S1_7)
## [1] "integer"
table(Forddata$S1_7)
## 
##   1   2   3 
##  25  49 473