RPubs will retire in June 2027. Your existing documents will stay accessible through December 31, 2031
and Connect Cloud is the recommended home for new publishing. Read the blog post
gravatar

ShaikRafi

Shaik Rafi

Recently Published

Plot
# Install packages if not already install.packages("ggplot2") install.packages("gridExtra") library(ggplot2) library(gridExtra) # Create sample plots representing dashboard icons/metrics plot1 <- ggplot(mtcars, aes(x=hp)) + geom_histogram(fill="steelblue", color="white") + labs(title="Horsepower Distribution") plot2 <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(color="darkgreen") + labs(title="MPG vs Weight") plot3 <- ggplot(mtcars, aes(x=factor(cyl))) + geom_bar(fill="orange") + labs(title="Cylinder Count") # Combine in a dashboard-style layout grid.arrange(plot1, plot2, plot3, ncol=2)
Machine Learning Algorithm (Linear Regression)
## Machine Learning Algorithm (Linear Regression) ## COde # Install required packages if not already installed install.packages("tidyverse") # Load the library library(tidyverse) # Load the dataset data(mtcars) # View the first few rows head(mtcars) # Fit a linear regression model model <- lm(mpg ~ wt, data = mtcars) # Summary of the model summary(model) # Visualize the regression line ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(color = "blue") + geom_smooth(method = "lm", color = "red") + labs(title = "Linear Regression: MPG vs Weight", x = "Weight of Car", y = "Miles Per Gallon") summary(cars$speed) summary(pressure) plot(pressure) ### Ouptput > # Load the library > 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.1 ✔ tibble 3.2.1 ✔ lubridate 1.9.4 ✔ tidyr 1.3.1 ✔ purrr 1.0.4 ── Conflicts ─────────────────────────────────────────────────── tidyverse_conflicts() ── ✖ dplyr::filter() masks stats::filter() ✖ dplyr::lag() masks stats::lag() ℹ Use the conflicted package to force all conflicts to become errors Warning messages: 1: package ‘tidyverse’ was built under R version 4.4.3 2: package ‘ggplot2’ was built under R version 4.4.3 > # Load the dataset > data(mtcars) > # View the first few rows > head(mtcars) mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 > # Fit a linear regression model > model <- lm(mpg ~ wt, data = mtcars) > # Summary of the model > summary(model) Call: lm(formula = mpg ~ wt, data = mtcars) Residuals: Min 1Q Median 3Q Max -4.5432 -2.3647 -0.1252 1.4096 6.8727 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 37.2851 1.8776 19.858 < 2e-16 *** wt -5.3445 0.5591 -9.559 1.29e-10 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 3.046 on 30 degrees of freedom Multiple R-squared: 0.7528, Adjusted R-squared: 0.7446 F-statistic: 91.38 on 1 and 30 DF, p-value: 1.294e-10 > # Visualize the regression line > ggplot(mtcars, aes(x = wt, y = mpg)) + + geom_point(color = "blue") + + geom_smooth(method = "lm", color = "red") + + labs(title = "Linear Regression: MPG vs Weight", + x = "Weight of Car", + y = "Miles Per Gallon") `geom_smooth()` using formula = 'y ~ x' > summary(cars$speed) Min. 1st Qu. Median Mean 3rd Qu. Max. 4.0 12.0 15.0 15.4 19.0 25.0 > summary(pressure) temperature pressure Min. : 0 Min. : 0.0002 1st Qu.: 90 1st Qu.: 0.1800 Median :180 Median : 8.8000 Mean :180 Mean :124.3367 3rd Qu.:270 3rd Qu.:126.5000 Max. :360 Max. :806.0000 > plot(pressure) >