0% found this document useful (0 votes)
6 views8 pages

R-Programming Module 4 Notes

This document is a comprehensive guide on R programming, covering topics such as sampling distributions, factors, hypothesis testing, and various statistical tests like T-tests and ANOVA. It also discusses regression analysis, customization of plots, and advanced graphics techniques including point-and-click interactions and higher-dimensional plotting. Each section includes definitions, explanations, syntax, and examples to facilitate understanding of R programming concepts.

Uploaded by

shaheenbegumb5
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

R-Programming Module 4 Notes

This document is a comprehensive guide on R programming, covering topics such as sampling distributions, factors, hypothesis testing, and various statistical tests like T-tests and ANOVA. It also discusses regression analysis, customization of plots, and advanced graphics techniques including point-and-click interactions and higher-dimensional plotting. Each section includes definitions, explanations, syntax, and examples to facilitate understanding of R programming concepts.

Uploaded by

shaheenbegumb5
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

R-PROGRAMMING 4TH MODULE PROF.

AJAY K V

R-PROGRAMMING MODULE-4
1. Define Sampling Distributions in R.
A Sampling distribution is a theoretical probability distribution that describes the
behavior of a static based on repeated random sampling from a population.
Syntax: hist(v, main, xlab, ylab, col)

2. Define factors in R.
Factors in R a variable used to categorize and store the data, having a limited number
of different values. It stores the data as a vector of integer values.
Types: categorical, continuous,

3. Define Hypothesis Testing. Write four-stop process of hypothesis testing.


Hypothesis testing is a fundamental statistical method used to make inferences about
population parameters based on sample data.
• State the hypotheses
• Set the significance level
• Calculate the test statistic and P- value/Analyze Sample Data
• Make a Decision/Interpret Decision

4. Explain different types of testing in R-Programming.


• One-sample T-Testing: one- sample t-test is used to compare the mean of the
single sample to a known or hypothesized population mean. Using [Link]().
Syntax: [Link](x, mu)
Example: x=rnorm(100)
[Link](x, mu=5)

• Two-Sample T-test: [Link](): A two-sample t-test is used to compare the means


of two independent groups to determine whether they are significantly different
from each other.
Syntax: [Link](x, y)
Example: group1=c(25,27,26,28,30,29,31,32,30,28)
group2=c(22,24,26,27,28,25,29,31,30,28)
result=[Link](group1, group2)
print(result)

• Paired T-Test: the paired t-test is used when you have two related groups or
samples. The data should be paired because the measurements in each group
are not independent, and each observation in one group has a unique
corresponding measurement in the other group.

Example: before=c(23,21,29,18,25)
after=c(19,20,28,17,22)

1
R-PROGRAMMING 4TH MODULE [Link] K V

result=[Link](before, after, paired= TURE)


print(result)

5. Define Analysis of Variance( ANOVA). Write key properties of anova.


• Anova stand for Analysis of Varience.
• Anova enables us to test for significance of difference amoung more than two
sample means.
• It is Extension of t-Test.
• ANOVA tests whether there is a difference in the means of the groups at each
level of the independent variable.

• key properties of anova: Decomposition of Variance, F-Test, Assumptions,


one-way and two- way anova, Post hoc Tests.

6. Explain one-way anova in detail.(very IMP)


• Anova is a statistical method used to compare means of three or more groups to
determine whether there are any statically significant differences among the
means.
• It is called one-way because there is only one because there is only one
independent variable affecting the dependent variable. This independent
variable typically represents the different groups or levels being compared.
Syntax: model=aov(reponse_variable~group_variable, data=your
_data_frame)
Example: group1= c(18,20,22,24,19)
group2= c(15,17,16,19,18)
group3= c(21,23,25,24,22)
#Combine the data into a data frame
data=[Link](value= c(group1,group2,group3), group=
factor(rep(1:3, each=5)))
model= aov(value~group, data=data)
print(model)

7. Explain two-way ANOVA in detail.(very IMP)


• The two-way ANOVA is used to analyze the effects of two categorical
independent variables on a continuous depend variable.
• A sample dataset with two categorical independent variables(group1 and
group2) and one continuous dependent variable(response).
• The gl() finction is used to generate factor levels for the groups.
• The aov() function is used to perform the two-way ANIVA.

Example: group1= gl(2,4,lables=c(“A”,”B”))

2
R-PROGRAMMING 4TH MODULE [Link] K V

group2=gl(2,2,8,lables=c(“X”,”Y”))
response=rnorm(8, mean=10, sd=2)
#Combining the data into a data frame
data=[Link](group1, group2, response)
#performing two-way ANOVA
model=aov(response~group1+group2+group1:group2, data=data)
Print(model)

8. Define Regression, explain linear regression with example.


• Regression is a statistical tool to estimate the relationship between two or more
variables.
• There is always one response variable and one or more predictor variables.
Linear Regression:
• The Linear Regression model is one of the most widely used three of the
regression types.
• Linear regression is a statistical technique used to model the relationship
between a dependent variable(often denoted a Y) and one or more independent
variables(often denoted as X).
• It assumes a linear relationship between the dependent and independent
variables. lm() function is used to create linear regression model.
Syntax: lm(formula, data)
Example: heights=c(65,71,69,68,72,66,77,73,74,60)
weights=c(120,150,140,130,160,125,180,170,175,110)
#Create a data frame from the data
data=[Link](heights, weights)
#perform the linear regression
model=lm(weights~heights, data=data)
summary(model)
• Advantages of linear regression: Easy to implement, easy to interpret, useful
for prediction.
• Disadvantaes of linear regression: Assumes linear relationship, sensitive to
outliers, Assumes independence of observations.

9. Explain Multiple regression with example.


• Multiple regression is a statistical technique used to model the relationship
between a dependent variable and multiple indepemdent variables.
• It an extension of simple linear regression, where you have more than one
predictor variable. Formula= y=a+b1x1+b2x2+….bnxn
Syntax: lm(formula, data)
Example: x1=c(1,2,3,4,5)
x2=c(3,4,5,6,7)

3
R-PROGRAMMING 4TH MODULE [Link] K V

y=c(3, 4.8, 6.9, 9.2, 10.9)


#Create a data frame
data=[Link](x1, x2, y)
model=lm(y~x1+x2, data=data)
print(model)

[Link] the customizing plots in advance graphics.


customizing plots in R allows us to create visually appealing and informative graphics
tailored to the specific needs. We can adjust various aspects of the plot, such as the title,
axes, labels, colors, and annotations.
Basic Plot Customization:
• Adjusting the plot title using the 'main' parameter.
• Customizing axis labels using the xlab and ylab parameters.
• Modifying axis limits using the xlim and ylim parameters.
• Changing axis ticks and labels using functions like 'axis' and 'at'.

Color Customization:
• Setting colors for points, lines, or bars using the 'col' parameter.
• Creating color palettes with functions like 'rainbow', '[Link]', and
'colorRampPalette".
Text Customization:
• Modifying text properties such as font size, font family, and font style using the
"cex" and family parameters.
• Adding annotations and text labels using functions like text and 'mtext'.
Legend Customization:
• Modifying legend labels and position using the legend function and x and y
parameters.
• Changing legend titles and text properties using the 'title' and '[Link]'
parameters.
Layout Customization:
• Adjusting the layout of multiple plots using functions like 'par' and 'layout'.
• Creating multi-panel plots using packages like 'ggplot2' and 'gridExtra".
Line and Marker Customization:
• Modifying line types and widths using the 'Ity' and 'lwd' parameters.
• Changing point shapes, sizes, and colors using the 'pch' and 'cex' parameters.
Background Customization:
o Adjusting the background color using the 'bg' parameter.
o Adding grid lines using functions like 'abline' and grid.

[Link] specialized Text Notation in advanced graphics.

4
R-PROGRAMMING 4TH MODULE [Link] K V

Specialized text notation in R refers to using specific symbols, formatting, or characters


to represent special characters, mathematical equations, or the other notations within
text strings.
1. Greek Letters
Greek letters can be included in text strings using the expression function. For
example, expression(alpha) represents the Greek letter alpha.
Example: plot(1:10, main expression("Scatterplot with-alpha- Symbol"))

2. Subscripts and Superscripts


Subscripts and superscripts can be added to text using the substitute and paste
functions.
Example: plot(1:10, main substitute(paste("H"^2, "O"), list()))

3. Special Characters
Special characters, such as degree symbol (") or copyright symbol (©), can be
included using their Unicode code points.
Example: plot(1:10, main "Temperature (C) vs. Time")

4. Mathematical Equations
Mathematical equations can be included within text using LaTeX notation. For
example, $\alpha + \beta \gammas represents a mathematical equation.
Example: plot(1:10, main "Linear Regression Model: Sy \\alpha + \\beta x +
\\epsilonS")

5. Scientific Notation
Scientific notation can be used to format numbers with exponents.
Example:
plot(1:10, xlab= "Time (s)", ylab "Distance (m)", main "Experimental Data: $2.5
\\times 10^{-3}$ kg")

12. Explain specialized Label Notation in advanced graphics.


• Specialized label notation to formate and customize the appearance of text
labels in your plots.
• subscripts, perscripts, and other formatting to your text annotations. The
expression and bquote functions e commonly used for creating specialized
labels in R. plots.
• The expression function is used to create a label with mathematical notation.
The label "f(x) a + beta^ * x^ ^ 2^ prime prime includes Greek letters (a, B) and
superscripts (").

• #Create a plot with a specialized label plot(1:5, 1:5, type "n", xlab="", ylab =")
text(3, 3, expression(f(x) alpha + beta x^2))

5
R-PROGRAMMING 4TH MODULE [Link] K V

• The bquote function allows you to create dynamic labels that include the values
of variables, in this case, x_val and y_val.

• #Create a plot with a dynamic label using bquote x\ v al < - 2 y\ v al < - 8


plot(1:10, 1:10, type = "n", xlab="", ylab = "") text(5, 5, bquote("A point at ("-
.(x\ v al ) sim"," sim.(y\ val) sim")"))

[Link] Plotting Region and Plotting Margin in advanced graphics.


Plotting Region:
The “plotting region” specifies the size and location of the plot within a graphical
device such as window or file. This is typically done using function like par.

Common parameters for defining the plotting region include:


• mfrow or mfcol: Specifies the number of rows and columns for multiple plots
in a grid.
• mar: Sets the margins around the plotting region.
• oma: Specifies the outer margins of the entire plot.
• plt: Defines the location of the plotting region within the graphical device
Example

Define a 2 x 2 grid for multiple plots


par(mfrow = c(2, 2))
#Create individual plots within the plotting region
plot(1:10, main = "Plot 1")
plot(11:20, main= "Plot 2")
plot(21:30, main = "Plot 3")
plot(31:40, main= "Plot 4")

Plotting Margins:
Plotting margins using the 'par" function. The par function is used to set various
graphical parameters for plotting. To change the margin sizes, you can modify the 'mar'
parameter, which represents the number of lines of margin to be specified on the four
sides of the plot (bottom, left top, right).
Example:
Program to adjust the plotting margins in R
#Create example data x1:10 y-x^2 #Set the plotting margins using the par function
par(marc(5, 4, 4, 2) +0.1) #Adjust the margins for the plot
#Create the plot with adjusted margins
plot(x, y, type "I", col "blue", main "Plot with Adjusted Margins")

[Link] Point-and-click in advanced graphics.

6
R-PROGRAMMING 4TH MODULE [Link] K V

A point-and-click coordinate interaction in an R plot to use the locator function. The


locator function is to interactively click on a plot, and it records the coordinates of the
points where you click. The locator function interactively selects points or coordinates
by clicking on a plot.
Example: Program to use the locator function to interactively click on a plot and
retrieve the coordinates:
# Create an example scatterplot
x=1:10
y=x^2
plot(x, y, type "p", col="blue", pch 16, main "Interactive Point Selection")
#Use locator to interactively click on the plot
points <- locator(1)
#Print the coordinates of the clicked points
cat("Clicked Coordinates:\n")
print(points)

[Link] Plotting in Higher Dimensions with Example.


Plotting in higher dimensions is a challenging task, as visualizing data beyond three
dimensions directly on a 2D plot is not feasible. However, there are several techniques
in R that can help visualize and analyze high-dimensional data:

Scatterplot Matrices: Use the pairs function to create a matrix of scatterplots, where
each variable is plotted against every other variable. This provides an overview of
pairwise relationships in the data.
Parallel Coordinate Plots: Represent each observation as a line that traverses across
a set of parallel axes, with each axis representing a different variable. This allows for
the visualization of multivariate data points.
Heatmaps: Use the heatmap or geom tile function in R to create heatmaps that display
numerical data in a matrix format, where colors represent the values of the data points.
Interactive 3D Visualization: Packages like rgl and plotly enable the creation of
interactive 3D plots, allowing for the exploration of data in three dimensions.
Hierarchical Clustering Dendrogram: Use the heatmap.2 function in the gplots
package to create a dendrogram that displays hierarchical relationships within the data.

Example:
Program to demonstrate a 3D scatter plot
#Install and load the required package
#[Link]("plot3D")
library(plot3D)
#Create example data
x <- morm(100)
z<- morm(100)

7
R-PROGRAMMING 4TH MODULE [Link] K V

y <- rnorm(100) scatter3D(x, y, z, colvarz, col="blue", pch 16, theta 30, phi = 30,
#Create a 3D scatter plot
xlab= "X-axis", ylab = "Y-axis", zlab = "Z-axis", main = "3D Scatter Plot")

NOTE:

Students study imp questions only

1. Anova and anova types.


2. Linear regression and multiple regression .
3. Plotting region and margines.
4. Define hypothesis testing.
5. Speciali9zed Text Notation.

Study these important question for 4th module.

You might also like