गुरुर्ब्रह्मा गुरुर्विष्णु गुरुर्देवो महेश्वरा गुरुर्साक्षात परब्रह्म तस्मै श्री गुरवे नमः !
QUIZ-2
Q1. It helps to find a numerical value expressing the relationship between variables
Regression
Correlation
Random Variable
Z-Value
Correct Answer : Correlation
Q2. It is used to estimate values of random variable on the basis of the values of fixed variable.
Regression
Correlation
Random Variable
Z-Value
Correct Answer : Regression
Q3. It attempts to model the relationship between two variables by fitting a linear equation to observed data.
Linear Regression
Covariance
Dependent Variable
S.D
Correct Answer : Linear Regression
Q4. __________ prints out the function call stack after an error occurs.
trace()
traceback()
back()
traback()
Correct Answer : traceback()
Q5. Linear regression uses_______ for graphical representation in determining the strength of the relationship between two variables.
Histogram
Pie chart
Scatter diagram
Anova
Correct Answer : Scatter diagram
Q6. Which of the following is primary tool for debugging?
debug()
trace()
browser()
traceback()
Correct Answer : debug()
Q7. Point out the wrong statement?
The primary task of debugging any R code is correctly diagnosing what the problem is
R provides only two tools to help you with debugging your code
print statement can be used for debugging purpose
The traceback() function must be called immediately after an error occurs
Correct Answer : R provides only two tools to help you with debugging your code
Q8. Which function will be used melt.mind2<-_______(mind, id=c(“ID”,”BTW”)) melt.mind2
Cast
Melt
melt
cast
Correct Answer : melt
Q9. Which function combines vectors as columns?
rbind()
cbind()
merge()
none of the above
Correct Answer : cbind()
Q10. The slope of the regression line of Y on X is also referred to as the:
Regression coefficient of X on Y
The correlation coefficient of X on Y
Regression coefficient of Y on X
Correlation coefficient of Y on X.
Correct Answer : Regression coefficient of Y on X
Q11. Which of the assertions below is the least accurate?
When outliers are present in the data series, correlation is a more reliable or relevant measure.
Two variables having a significant nonlinear relation can still have a relatively low correlation.
Correlation among two variables can emerge from their relationship with a third variable rather than a direct relationship between them.
None of the preceding.
Correct Answer : When outliers are present in the data series, correlation is a more reliable or relevant measure.
Q12. The correlation coefficient is?
The square of the coefficient of determination
Can never be negative
The square root of the coefficient of determination.
The same as r square
Correct Answer : The square root of the coefficient of determination.
Q13. The correlation for the values of two variables moving in the same direction is
Perfect positive
Negative
Positive
No correlation.
Correct Answer : Positive
Q14. Who introduced the term ‘regression’?
Karl Pearson
R.A Fischer
Croxton and Cowden
Francis Galton.
Correct Answer : Francis Galton.
Q15. The correlation coefficient describes
Only magnitude
Both magnitude and direction
Only direction
None of the preceding options.
Correct Answer : Both magnitude and direction
Answer :
Start by downloading R and RStudio. Then open RStudio and click on File > New File > R Script.
To install the packages you need for the analysis, run this code (you only need to do this once):
install.packages("ggplot2")
install.packages("dplyr")
install.packages("broom")
install.packages("ggpubr")
Next, load the packages into your R environment by running this code (you need to do this every time you restart R):
library(ggplot2)
library(dplyr)
library(broom)
library(ggpubr)
Follow these four steps for each dataset:
Extract the Downloaded Zip of sample datasets.
In RStudio, go to File > Import dataset > From Text (base).
Choose the data file you have downloaded (income.csv or heart.csv), and an Import Dataset window pops up.
In the Data Frame window, you should see an X (index) column and columns listing the data for each of the variables (income and happiness or biking, smoking, and heart.disease).
Click on the Import button and the file should appear in your Environment tab on the upper right side of the RStudio screen.
After you’ve loaded the data, check that it has been read in correctly using summary().
income.happiness.lm <- lm(happiness ~ income, data = income.data)
summary(income.happiness.lm)
print("Name: BRAINHEALERS ,UID:21BCS____")
The output looks like this:
heart.disease.lm<-lm(heart.disease ~ biking + smoking, data = heart.data)
summary(heart.disease.lm)
print("Name: BRAINHEALERS ,UID:21BCS____")
The output looks like this:
par(mfrow=c(2,2))
plot(income.happiness.lm)
par(mfrow=c(1,1))
print("Name: BRAINHEALERS ,UID:21BCS____")
These are the residual plots produced by the code:
par(mfrow=c(2,2))
plot(heart.disease.lm)
par(mfrow=c(1,1))
print("Name: BRAINHEALERS ,UID:21BCS____")
The output looks like this:
Follow 4 steps to visualize the results of your simple linear regression.
Plot the data points on a graph
income.graph<-ggplot(income.data, aes(x=income, y=happiness))+geom_point()
income.graph
print("Name: BRAINHEALERS ,UID:21BCS____")
Add the linear regression line to the plotted data
income.graph <- income.graph + geom_smooth(method="lm", col="black")
income.graph
print("Name: BRAINHEALERS ,UID:21BCS____")
Add the equation for the regression line.
income.graph <- income.graph +
stat_regline_equation(label.x = 3, label.y = 7)
income.graph
print("Name: BRAINHEALERS ,UID:21BCS____")
Make the graph ready for publication
income.graph +
theme_bw() +
labs(title = "Reported happiness as a function of income",
x = "Income (x$10,000)",
y = "Happiness score (0 to 10)")
print("Name: BRAINHEALERS ,UID:21BCS____")
This produces the finished graph that you can include in your papers:
There are 7 steps to follow.
Create a new dataframe with the information needed to plot the model
plotting.data<-expand.grid(
biking = seq(min(heart.data$biking), max(heart.data$biking), length.out=30),
smoking=c(min(heart.data$smoking), mean(heart.data$smoking), max(heart.data$smoking)))
print("Name: BRAINHEALERS ,UID:21BCS____")
Predict the values of heart disease based on your linear model
plotting.data$predicted.y <- predict.lm(heart.disease.lm, newdata=plotting.data)
print("Name: BRAINHEALERS ,UID:21BCS____")
Round the smoking numbers to two decimals
plotting.data$smoking <- round(plotting.data$smoking, digits = 2)
print("Name: BRAINHEALERS ,UID:21BCS____")
Change the ‘smoking’ variable into a factor
plotting.data$smoking <- as.factor(plotting.data$smoking)
print("Name: BRAINHEALERS ,UID:21BCS____")
Plot the original data
heart.plot <- ggplot(heart.data, aes(x=biking, y=heart.disease)) +
geom_point()
heart.plot
print("Name: BRAINHEALERS ,UID:21BCS____")
Add the regression lines
heart.plot <- heart.plot +
geom_line(data=plotting.data, aes(x=biking, y=predicted.y, color=smoking), size=1.25)
heart.plot
print("Name: BRAINHEALERS ,UID:21BCS____")
Make the graph ready for publication
heart.plot <-
heart.plot +
theme_bw() +
labs(title = "Rates of heart disease (% of population) \n as a function of biking to work and smoking",
x = "Biking to work (% of population)",
y = "Heart disease (% of population)",
color = "Smoking \n (% of population)")
heart.plot
print("Name: BRAINHEALERS ,UID:21BCS____")
heart.plot + annotate(geom="text", x=30, y=1.75, label=" = 15 + (-0.2*biking) + (0.178*smoking)")
print("Name: BRAINHEALERS ,UID:21BCS____")
This is the finished graph that you can include in your papers!
In addition to the graph, include a brief statement explaining the results of the regression model.Reporting the results of simple linear regressionWe found a significant relationship between income and happiness (p < 0.001, R2 = 0.73 ± 0.0193), with a 0.73-unit increase in reported happiness for every $10,000 increase in income.Reporting the results of multiple linear regressionIn our survey of 500 towns, we found significant relationships between the frequency of biking to work and the frequency of heart disease and the frequency of smoking and frequency of heart disease (p < 0 and p < 0.001, respectively).
Specifically we found a 0.2% decrease (± 0.0014) in the frequency of heart disease for every 1% increase in biking, and a 0.178% increase (± 0.0035) in the frequency of heart disease for every 1% increase in smoking.
A more sophisticated debugging method is to put a call to browser() in your code. This will stop execution at that point and open R’s interactive debugger. In the debugger you can run any R command to look at objects in the current environment, modify them and continue executing.
The traceback() function can be used to print a summary of how your program arrived at the error. This is also called a call stack. In R this gives you each call that lead up to the error, which can be very useful for determining what lead to the error.
recover() is not used directly, instead it is used as an error handler, by calling options(error = recover). You can also use other functions, such as browser() as an error handler, which will start the debugger automatically when there is an error.
Solution:
Wide to long:
import pandas as pd
# Create wide dataframe
df = pd.DataFrame({'id': [1, 1, 2, 2], 'time': [1, 2, 1, 2], '*1': [5, 3, 6, 2], '*2': [6, 5, 1, 4]})
# Use pd.melt() to reshape data
df_long = pd.melt(df, id_vars=['id', 'time'], value_vars=['*1', '*2'], var_name='variable', value_name='value')
Long to wide:
# Use pd.pivot_table() to reshape data
df_wide = df_long.pivot_table(index=['id', 'time'], columns='variable', values='value')
# Reset the index
df_wide.reset_index(inplace=True)