#1. Import data

This function allows R to import and read the data from plant growth & soil moisture CSV. By adding “Data <-”, I am able to set this data-frame as a variable. This makes the code easier to read. The output is present in the environment tab in R, and it shows the variable “Data” alongside the table containing the data.

#2 Package Import

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)

The above input functions allow this session of R to load the listed packages and their provided functions. The output is not visual in here, but is present in the code below. Below, functions from these packages are used, and if we did not load these packages prior, then the code would not work.

#2. Confirm data’s import & presence

head(Data)
##   soil.moisture.content plant.growth.rate
## 1             0.4696876          21.31695
## 2             0.5413106          27.03072
## 3             1.6979915          38.98937
## 4             0.8255799          30.19529
## 5             0.8568173          37.06547
## 6             1.6082405          43.24982

The head function displays the first 6 rows of data from the specified dataset (“Data” here). The output is a table containing this data.

#3. Question 1: Data simulation & ANOVA + Summary analysis

simulate_plant_growth <- function(n = 500,
                                  alpha = 2,
                                  beta = 20,
                                  rse = 8,
                                  moisture_range = c(0.1, 5.0)) {

  soil_moisture <- runif(n, 
                         min = moisture_range[1], 
                         max = moisture_range[2])

  error <- rnorm(n, mean = 8, sd = rse)

  growth_rate <- alpha + beta * soil_moisture + error

  data.frame(
    soil_moisture = soil_moisture,
    growth_rate = growth_rate
  )
}

By setting the seed we set the randomization of the newly created data frame to be reproducible. The ouput is the specific numbers and orders of soil_moisture & growth_rate in data frame. By changing the seed # we would also change the values & order of the randomized data.

The first part of this code creates a new function, “simulate_plant_growth”. This function generates simulated plant growth data, as required for this project. The output is the availability of this function for further use and its presence in the “Environment” window in the top right of R. The second part of this code actually generates the new simulated data. There is a defined max & min moisture range for this data. The output is the creation of n = 500 simulated soil mosture data points. The error code accounts for the random error and variation that you would see within gathered data. This ensures that the simulated data is not all the same. The growth_rate input sets the growth rate of the simulated data to be in-line with the linear regression model. This output will be present in the values of the simulated data. The last part of the creates the new data frame (later named Data_Single) with the new simulated data assigned to specific columns.

Data_Simulated <- simulate_plant_growth()

This executes our new function, “simulate_plant_growth” and gives the simulated data a home in the Data_Single data frame.

lm.model <- lm(growth_rate ~ soil_moisture, data = Data_Simulated)

This function actually comlpetes the linear regression analysis and stores the findings in the “lm.model” object. In here we specified the relationship that we are looking into (growth_rate ~ soil_moisture) and created the object “lm.model” that holds this linear regression data.

summary(lm.model)
## 
## Call:
## lm(formula = growth_rate ~ soil_moisture, data = Data_Simulated)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -21.0949  -4.9714  -0.0081   5.1029  22.7611 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    11.5603     0.6864   16.84   <2e-16 ***
## soil_moisture  19.5720     0.2353   83.18   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.503 on 498 degrees of freedom
## Multiple R-squared:  0.9329, Adjusted R-squared:  0.9327 
## F-statistic:  6918 on 1 and 498 DF,  p-value: < 2.2e-16

This function provides a statistical summary of the data imputed within it. Here, this data is our linear regression data, so the summary provided gives a statistical summary of the numeric and categorical variables present.

anova(lm.model)
## Analysis of Variance Table
## 
## Response: growth_rate
##                Df Sum Sq Mean Sq F value    Pr(>F)    
## soil_moisture   1 389471  389471  6918.2 < 2.2e-16 ***
## Residuals     498  28035      56                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

This function creates an ANOVA statistical summary of the data specified.

#5. Question 9: Graph creation

Data_Simulated %>%
  ggplot(aes(x = soil_moisture, y = growth_rate)) +
  geom_point() +
  geom_smooth(method = "lm", se = TRUE) +
  xlab("Soil Moisture") +
  ylab("Plant Growth Rate")+
  theme_bw()
## `geom_smooth()` using formula = 'y ~ x'

Above is a ggplot2 code that creates a scatter plot graph from the information provided. By using the geom_smooth(method = lm, se = TRUE) addition to it, we add a linear regression line and confidence interval to our graph. Because the confidence interval is so small, we see that our simulated data fits well.