R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# Define the parameters
population_mean <- 60
population_sd <- 20
sample_size <- 25
num_simulations <- 1000

# Create a vector to store the sample means
sample_means <- numeric(num_simulations)
sample_standard_deviations<-numeric(num_simulations)

# Simulate sampling and calculate sample means
for (i in 1:num_simulations) {
  samples <- rnorm(sample_size, population_mean, population_sd)
  sample_means[i] <- mean(samples)
  sample_standard_deviations[i]<-sd(samples)
}
#sample_means
#sample_standard_deviations
# Calculate the mean, standard deviation, and standard error of the sample means
mean_of_sample_means <- mean(sample_means)
mean_of_sample_standard_deviations<-mean(sample_standard_deviations)
sd_of_sample_means <- sd(sample_means)
se_of_sample_means <- sd_of_sample_means / sqrt(sample_size-1)
SEotE<-sqrt((mean_of_sample_means-population_mean)^2/(sample_size-1))

# Output the results
mean_of_sample_means
## [1] 59.96027
mean_of_sample_standard_deviations
## [1] 19.88741
sd_of_sample_means
## [1] 4.032389
se_of_sample_means
## [1] 0.8231079
SEotE
## [1] 0.008109077

Including Plots

You can also embed plots, for example:

library(arm)
## Loading required package: MASS
## Loading required package: Matrix
## Loading required package: lme4
## 
## arm (Version 1.13-1, built: 2022-8-25)
## Working directory is C:/Users/brand/Desktop/UCR/PhD CCN/PSYC213_Spring2023/Lab/Midterm
crash.data<-read.csv("crash.csv")
m1<-lm(Rate~AlcCons+PerMiles,data=crash.data)
m2<-lm(Rate~KillRest+KillUnre+KillRest*KillUnre,data=crash.data)
display(m1)
## lm(formula = Rate ~ AlcCons + PerMiles, data = crash.data)
##             coef.est coef.se
## (Intercept) -1.64     3.87  
## AlcCons     -1.93     0.90  
## PerMiles    17.96     1.44  
## ---
## n = 51, k = 3
## residual sd = 4.38, R-Squared = 0.79
display(m2)
## lm(formula = Rate ~ KillRest + KillUnre + KillRest * KillUnre, 
##     data = crash.data)
##                   coef.est coef.se
## (Intercept)       -64.89    33.78 
## KillRest            1.36     0.79 
## KillUnre            1.27     0.54 
## KillRest:KillUnre  -0.02     0.01 
## ---
## n = 51, k = 4
## residual sd = 8.35, R-Squared = 0.24
summary(m1)
## 
## Call:
## lm(formula = Rate ~ AlcCons + PerMiles, data = crash.data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -11.0272  -2.8275   0.0915   2.7110  12.7452 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -1.6361     3.8668  -0.423   0.6741    
## AlcCons      -1.9281     0.9032  -2.135   0.0379 *  
## PerMiles     17.9630     1.4376  12.495   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.376 on 48 degrees of freedom
## Multiple R-squared:  0.7869, Adjusted R-squared:  0.778 
## F-statistic: 88.61 on 2 and 48 DF,  p-value: < 2.2e-16
summary(m2)
## 
## Call:
## lm(formula = Rate ~ KillRest + KillUnre + KillRest * KillUnre, 
##     data = crash.data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -22.1232  -5.1672  -0.0482   5.2242  17.0232 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)  
## (Intercept)       -64.88945   33.78050  -1.921   0.0608 .
## KillRest            1.35982    0.79103   1.719   0.0922 .
## KillUnre            1.27468    0.54039   2.359   0.0225 *
## KillRest:KillUnre  -0.01521    0.01378  -1.104   0.2753  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 8.355 on 47 degrees of freedom
## Multiple R-squared:  0.2394, Adjusted R-squared:  0.1908 
## F-statistic:  4.93 on 3 and 47 DF,  p-value: 0.004655
# Load the ggplot2 library
library(ggplot2)

# Create a data frame combining the residuals and fitted values
residualsm1<-residuals(m1)
fitted.values.m1<-fitted.values(m1)

residuals_df <- data.frame(Residuals = residualsm1, Fitted_Values = fitted.values.m1)

# Create a scatter plot of residuals versus fitted values
ggplot(data = residuals_df, aes(x = Fitted_Values, y = Residuals)) +
  geom_point() +
  labs(x = "Fitted Values", y = "Residuals") +
  ggtitle("Residuals versus Fitted Values")

residualsm2<-residuals(m2)
fitted.values.m2<-fitted.values(m2)

residuals_df2 <- data.frame(Residuals = residualsm2, Fitted_Values = fitted.values.m2)

# Create a scatter plot of residuals versus fitted values
ggplot(data = residuals_df2, aes(x = Fitted_Values, y = Residuals)) +
  geom_point() +
  labs(x = "Fitted Values", y = "Residuals") +
  ggtitle("Residuals versus Fitted Values")

AlcCons<-mean(crash.data$AlcCons,na.rm=TRUE)+c(-1,0,1)*sd(crash.data$AlcCons,na.rm=TRUE)
KillUnre<-mean(crash.data$KillUnre,na.rm=TRUE)+c(-1,0,1)*sd(crash.data$KillUnre,na.rm=TRUE)
combs <- expand.grid(AlcCons, KillUnre)
combs
##       Var1     Var2
## 1 1.933266 47.69781
## 2 2.631176 47.69781
## 3 3.329087 47.69781
## 4 1.933266 56.99412
## 5 2.631176 56.99412
## 6 3.329087 56.99412
## 7 1.933266 66.29043
## 8 2.631176 66.29043
## 9 3.329087 66.29043
predict(m1)
##         1         2         3         4         5         6         7         8 
## 34.199759 29.299236 38.994118 38.312938 19.292685 25.144320 12.724483 21.069702 
##         9        10        11        12        13        14        15        16 
## 19.703932 32.155983 24.196382 21.532444 31.593663 23.733640 21.162932 30.337230 
##        17        18        19        20        21        22        23        24 
## 27.053129 32.519146 34.993450 20.372416 19.986798  8.804112 25.742028 18.576118 
##        25        26        27        28        29        30        31        32 
## 46.503911 27.923944 34.395742 22.708578 28.106994  9.408168 16.355640 34.472865 
##        33        34        35        36        37        38        39        40 
## 20.391696 28.328843 13.495719 19.308792 25.411079 27.596168 22.804983 10.793219 
##        41        42        43        44        45        46        47        48 
## 34.858483 31.767191 34.103354 25.626343 25.854539 22.769595 16.837663 16.587011 
##        49        50        51 
## 34.334725 17.419264 36.384848
predict(m2)
##        1        2        3        4        5        6        7        8 
## 28.94527 23.31288 22.40798 25.49018 20.45396 28.62837 24.67650 29.84510 
##        9       10       11       12       13       14       15       16 
## 21.02478 28.14584 23.34414 15.57539 28.24756 20.17495 24.62817 22.42529 
##       17       18       19       20       21       22       23       24 
## 22.95595 30.52749 20.10626 30.15355 24.17188 15.95254 22.81514 18.41057 
##       25       26       27       28       29       30       31       32 
## 36.30676 26.43972 28.69453 20.58384 24.60661 27.51389 22.21172 27.71945 
##       33       34       35       36       37       38       39       40 
## 20.37956 19.94188 21.65511 28.91058 34.35016 26.24364 20.03751 32.03320 
##       41       42       43       44       45       46       47       48 
## 29.53627 25.89958 30.86095 27.83199 25.27377 26.67510 24.83539 27.83655 
##       49       50       51 
## 29.04444 24.71952 33.48852

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.