R Viva Ques
R Viva Ques
R
CopyEdit
install.packages("ggplot2") # To install
library(ggplot2) # To load
● List: Heterogeneous, can contain different types (e.g., numbers, strings, vectors)
6. What are data frames and how are they different from matrices?
Answer:
R
CopyEdit
vec <- c(1, 2, 3, 4)
R
CopyEdit
mat <- matrix(1:6, nrow=2, ncol=3)
R
CopyEdit
lst <- list(name="Alex", age=25, scores=c(90, 80, 85))
R
CopyEdit
factor_var <- factor(c("low", "medium", "high"))
R
CopyEdit
x <- 5
if (x > 0) {
print("Positive")
} else {
print("Non-positive")
}
R
CopyEdit
for (i in 1:5) {
print(i^2)
}
R
CopyEdit
square <- function(x) {
return(x^2)
}
square(4)
R
CopyEdit
apply(matrix(1:9, nrow=3), 1, sum) # Row-wise sum
R
CopyEdit
x <- list(a=1:3, b=4:6)
lapply(x, sum) # List of sums
sapply(x, sum) # Vector of sums
R
CopyEdit
ages <- c(21, 25, 19, 23)
gender <- factor(c("M", "F", "M", "F"))
tapply(ages, gender, mean) # Mean age by gender
R
CopyEdit
str(data)
R
CopyEdit
summary(data)
● Numeric
● Integer
● Character
● Logical
● Complex
● Factor
UNIT 4: Descriptive Statistics Using R (6 Hours)
Topics: Data Import, Data Visualization, Measures of Central Tendency, Measures
of Dispersion, Covariance, Correlation, Coefficient of Determination
● Median = 4
● Mode = 5
● 0 = no linear relationship
Explanation: Gives min, 1st quartile, median, mean, 3rd quartile, and max for each column.
7. What is heteroscedasticity?
Answer:
It occurs when the variance of residuals is not constant across all levels of the independent
variable(s). It violates a key regression assumption and affects the accuracy of coefficient
estimates.
8. What is multicollinearity?
Answer:
Multicollinearity occurs when independent variables are highly correlated with each other,
making it difficult to isolate their individual effects.
13. How do you extract the R-squared and Adjusted R-squared values in R?
R
CopyEdit
summary(model)$r.squared
summary(model)$adj.r.squared
R
CopyEdit
library(lmtest)
bptest(model) # Breusch-Pagan Test
# Prediction interval
predict(model, newdata = data.frame(x = 50), interval = "prediction")
● Coefficients
● R-squared values
● F-statistic
2. What are the advantages of R over other languages like Python or Excel?
Answer:
4. What are the primary data structures in R and where are they used?
Structure Description Use Case
● A list can contain different types (e.g., numeric, character, vectors, even data frames)
● R² ranges from 0 to 1
● β₀: intercept
● β₁: slope
● ε: error term
Importing Excel/CSV files readr, For reading .csv and .xlsx files
readxl
Data wrangling dplyr, For data manipulation and better data frame
tibble handling
Viewing data types and str, Built-in base functions (no need to install)
structure summary
✅ Installation code:
r
CopyEdit
install.packages(c("readr", "readxl", "dplyr", "tibble"))
✅ Installation code:
r
CopyEdit
install.packages(c("ggplot2", "psych"))
✅ Installation code:
r
CopyEdit
install.packages(c("car", "GGally", "lmtest"))
r
CopyEdit
install.packages(c("readr", "readxl", "dplyr", "tibble", "ggplot2",
"psych", "car", "GGally", "lmtest"))