In this lesson students will learn …
In R you can use the equals sign =
or the arrow
<-
to do variable assignment.
There are several distributions built into base R, from which you can draw values.
Learn about the runif
function
### HELP
?runif
help(runif)
What are the arguments for the runif
function? What are
the default values?
## YOUR NOTES HERE ##
Generate 100 random numbers from the interval (0,1) and call it
X
### Generate 100 random numbers from the interval (0,1) and call it X
X<-runif(n=100, min=0, max=1)
Create a histogram to look at the data.
### Histogram in Base R
hist(X)
Generate 100 random numbers from the interval (0,1) and call it W
### Generate 100 random numbers from the interval (0,1) and call it W
W<-runif(n=100, min=0, max=1)
Define Y to be a linear combination of X and W, where \[Y=0.15X + 0.85W\]
### Define Y to be a linear combination of X and W
Y<-0.15*X + 0.85*W
Make a scatterplot of (X,W)
### SCATTERPLOT IN BASE R
plot(X, W)
How would you describe the relationship between X and W?
## YOUR NOTES HERE
Make a scatterplots of (X, Y) and (W, Y)
### SCATTERPLOTS OF X,Y and W,Y
plot(X,Y)
plot(W,Y)
Are these plots similar or different? Do you have a hypothesis about why they might look different?
## YOUR NOTES HERE
Consider the following examples of linear models:
Program examples of the models above and in R. Then create a scatter plot to visualize the relationship. Compare and contrast plots.
### SIMPLE LINEAR
X<-runif(n=100)
### PARAMETERS
beta0<-.05
beta1<-.17
### MODEL
Y<-beta0+beta1*X
### PLOT
plot(X, Y)
### POLYNOMIAL LINEAR
X<-runif(n=100)
### PARAMETERS
beta0<-.05
beta1<-.17
beta2<-.14
### MODEL
Y<-beta0+beta1*X+beta2*X^2
### PLOT
plot(X, Y)
### LOG TRANSFORMED
X<-runif(n=100)
### PARAMETERS
beta0<-.05
beta1<-.17
### MODEL
Y<-beta0+beta1*log(X)
### PLOT
plot(X, Y)
plot(log(X), Y)
### MULTIPLE
X1<-runif(n=100)
X2<-rnorm(n=100)
### PARAMETERS
beta0<-.05
beta1<-.17
beta2<-.16
### MODEL
Y<-beta0+beta1*X1+beta2*X2
### PLOT
plot(X1, Y)
plot(X2, Y)