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:

library (tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
ggplot(iris, aes(x = Petal.Width, y = Petal.Length)) +
  geom_point() +               
  xlab("Petal Width (cm)") +
  ylab("Petal Length (cm)") + 
  ggtitle("Relationship between petal width and petal length in iris data")

fit = lm(Petal.Length~ Petal.Width, data = iris)
summary(fit)
## 
## Call:
## lm(formula = Petal.Length ~ Petal.Width, data = iris)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.33542 -0.30347 -0.02955  0.25776  1.39453 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.08356    0.07297   14.85   <2e-16 ***
## Petal.Width  2.22994    0.05140   43.39   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4782 on 148 degrees of freedom
## Multiple R-squared:  0.9271, Adjusted R-squared:  0.9266 
## F-statistic:  1882 on 1 and 148 DF,  p-value: < 2.2e-16
ggplot(iris, aes(x = Petal.Width, y = Petal.Length)) +   
geom_point() +                   
geom_smooth(method = "lm") + 
xlab("Petal Width (cm)") +   
ylab("Petal Length (cm)") +    
ggtitle("Relationship between petal width and petal length in iris data")
## `geom_smooth()` using formula = 'y ~ x'

cor(iris$Petal.Width,iris$Petal.Length)
## [1] 0.9628654
plot(fit, which = 1)

ggplot(fit, aes(x = .fitted, y = .resid)) +
  geom_point() +
  geom_hline(yintercept = 0, linetype = "dashed", colour = "red") +
  ggtitle("Resid vs Fitted")

unname(fit$coeff[1]) + unname(fit$coeff[2])*2
## [1] 5.543439
AQIJuly2015 <- read.csv("~/data1001 demo/AQIJuly2015.csv")
view((AQIJuly2015))
ggplot(AQIJuly2015, aes(x = SydneyCEAQI, y = SydneyNWAQI)) +   
geom_point() +                   
geom_smooth(method = "lm") + 
xlab("Sydney CEAQI") +   
ylab("Sydney NWAQI") 
## `geom_smooth()` using formula = 'y ~ x'

Olympics100m <- read.csv("C:/Users/frida/Downloads/Olympics100m.csv")
View(Olympics100m)
Men <- Olympics100m$Time[Olympics100m$Gender == "male"] 
max(Men)
## [1] 12
Women <- Olympics100m$Time[Olympics100m$Gender == "female"] 
min(Women)
## [1] 10.54
ggplot(Olympics100m, aes(Country)) +   
geom_bar(fill = "black") 

year <- Olympics100m[Olympics100m$Gender == "male", ]    # note the comma for filtering [row, column] 
fit <- lm(Time ~ Year, year) 
cor(Olympics100m$Time, Olympics100m$Year) 
## [1] -0.4950511
ggplot(Olympics100m, aes(Year, Time)) +
geom_point() +
geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'