R Programming Student Lab Manual-52-63-3-12
R Programming Student Lab Manual-52-63-3-12
Solution
> center <- 12
> sd <- 3
> n <- 30
> E <- qnorm(0.975)*sd/sqrt(n)
>E
[1] 1.073516
> lower_bound <- center – E
>lower_bound
[1] 10.92648
>upper_bound <- center + E
>upper_bound
53
[1] 13.07352
Therefore lower_bound is 10.92648 and upper_bound is 13.07352
Thus the range in this case is between 10.9 and 13.1 (rounding outwards).
Note the p-value of 0.1336. The p-value reports how likely we are to see this
data or worse assuming the null hypothesis. The notion of worse, is implied by
the alternative hypothesis. In this example, the alternative is two sided as too
small a value or too large a value or the test statistic is consistent with HA. In
particular, the p-value is the probability of 42 or fewer or 58 or more answer
54
“yes” when the chance a person will answer “yes” is fifty-fifty.
Now the p-value is tiny (that’s 0.0000004956!) and the null hypothesis is not
supported. That is, we “reject” the null hypothesis. This illustrates the the p
value depends not just on the ratio, but also n. In particular, it is because the
standard error of the sample average gets smaller as n gets larger.
Linear regression is one of the simplest and most common supervised machine
learning algorithms that data scientists use for predictive modeling. In this post,
we’ll use linear regression to build a model that predicts cherry tree volume
from metrics that are much easier for folks who study trees to measure.
Collect some data relevant to the problem (more is almost always better).
Clean, augment, and preprocess the data into a convenient form, if
needed.
Conduct an exploratory analysis of the data to get a better sense of it.
Using what you find as a guide, construct a model of some aspect of the
data.
Use the model to answer the question you started with, and validate your
results.
55
The Simple Linear Regression is handled by the inbuilt function ‘lm’ in R.
print(relation)
Call:
lm(formula = y ~ x)
56
Get the Summary of the Relationship
x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
# Apply the lm() function.
relation <- lm(y~x)
predict() Function
Syntax
The basic syntax for predict() in linear regression is −
predict(object, newdata)
Following is the description of the parameters used −
object is the formula which is already created using the lm() function.
newdata is the vector containing the new value for predictor variable.
57
relation <- lm(y~x)
58
Experiment 11
The most used plotting function in R programming is the plot() function. It
is a generic function, meaning, it has many methods which are called according
to the type of object passed to plot().
In the simplest case, we can pass in a vector and we will get a scatter plot of
magnitude vs index. But generally, we pass in two vectors and a scatter plot of
these points are plotted.
For example, the command plot(c(1,2),c(3,5)) would plot the points (1,3) and
(2,5).
Here is a more concrete example where we plot a sine function form range
-pi to pi.
x <- seq(-pi,pi,0.1)
plot(x, sin(x))
59
Adding Titles and Labelling Axes
We can add a title to our plot with the parameter main. Similarly, xlab and ylab
can be used to label the x-axis and y-axis respectively.
plot(x, sin(x),
main="The Sine Function", ylab="sin(x)")
plot(x, sin(x),
main="The Sine Function", ylab="sin(x)",
type="l", col="blue")
60
R 3D PLOTS
There are many functions in R programming for creating 3D plots. In this
section, we will discuss on the persp() function which can be used to create 3D
surfaces in perspective view.
This function mainly takes in three variables, x, y and z where x and y are
vectors defining the location along x- and y-axis. The height of the surface (z-
axis) will be in the matrix z. As an example,
Let’s plot a cone. A simple right circular cone can be obtained with the
following function.
61
Adding Titles and Labelling Axes to Plot
We can add a title to our plot with the parameter main. Similarly, xlab, ylab and
zlab can be used to label the three axes.
Rotational angles
We can define the viewing direction using parameters theta and phi.
By default theta, azimuthal direction, is 0 and phi, colatitude direction, is 15.
Colouring and Shading Plot
Colouring of the plot is done with parameter col. Similarly, we can add shading
with the parameter shade.
persp(x, y, z,
main="Perspective Plot of a Cone", zlab = "Height",
theta = 30, phi = 15,
col = "springgreen", shade = 0.5)
62