R Markdown
# --- Distribución Normal ---
x <- seq(-4, 4, length = 200)
plot(x, dnorm(x), type = "l", lwd = 2,
col = "#276DC2", main = "Distribución Normal N(0,1)")

# P(Z < 1.96)
pnorm(1.96) # ≈ 0.975
## [1] 0.9750021
# --- Generación de muestras ---
set.seed(42)
grupo_A <- rnorm(30, mean = 75, sd = 10)
grupo_B <- rnorm(30, mean = 80, sd = 12)
# --- Prueba t de Student (dos muestras) ---
resultado <- t.test(grupo_A, grupo_B,
alternative = "two.sided",
var.equal = FALSE)
print(resultado)
##
## Welch Two Sample t-test
##
## data: grupo_A and grupo_B
## t = -0.87812, df = 57.999, p-value = 0.3835
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -9.350771 3.648312
## sample estimates:
## mean of x mean of y
## 75.68587 78.53710
# Interpreta: p-value < 0.05 → rechazar H0
# --- Regresión Lineal Simple ---
modelo <- lm(mpg ~ wt + hp, data = mtcars)
summary(modelo) # R², coeficientes, F-stat
##
## Call:
## lm(formula = mpg ~ wt + hp, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.941 -1.600 -0.182 1.050 5.854
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.22727 1.59879 23.285 < 2e-16 ***
## wt -3.87783 0.63273 -6.129 1.12e-06 ***
## hp -0.03177 0.00903 -3.519 0.00145 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.593 on 29 degrees of freedom
## Multiple R-squared: 0.8268, Adjusted R-squared: 0.8148
## F-statistic: 69.21 on 2 and 29 DF, p-value: 9.109e-12
plot(modelo) # diagnóstico de residuos




confint(modelo) # intervalos de confianza
## 2.5 % 97.5 %
## (Intercept) 33.95738245 40.49715778
## wt -5.17191604 -2.58374544
## hp -0.05024078 -0.01330512
# --- Correlación ---
cor.test(mtcars$wt, mtcars$mpg, method = "pearson")
##
## Pearson's product-moment correlation
##
## data: mtcars$wt and mtcars$mpg
## t = -9.559, df = 30, p-value = 1.294e-10
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.9338264 -0.7440872
## sample estimates:
## cor
## -0.8676594
# ============================================
# MÓDULO 1: Fundamentos de R
# Universidad Autónoma de Chiriquí — UNACHI
# ============================================
# --- 1. Tipos de variables ---
num_val <- 3.14 # numeric
int_val <- 10L # integer
char_val <- "UNACHI" # character
bool_val <- TRUE # logical
fact_val <- factor(c("A", "B", "A", "C"))
class(num_val) # "numeric"
## [1] "numeric"
typeof(int_val) # "integer"
## [1] "integer"
# --- 2. Vectores ---
notas <- c(85, 92, 78, 95, 88)
seq1 <- seq(1, 10, by = 2) # 1 3 5 7 9
mean(notas) # 87.6
## [1] 87.6
sd(notas) # desviación estándar
## [1] 6.580274
# --- 3. Matrices ---
M <- matrix(1:9, nrow = 3, ncol = 3)
t(M) # transpuesta
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
M %*% M # producto matricial
## [,1] [,2] [,3]
## [1,] 30 66 102
## [2,] 36 81 126
## [3,] 42 96 150
# --- 4. Data Frame ---
df <- data.frame(
nombre = c("Ana", "Luis", "María"),
nota = c(90, 85, 92),
aprobó = c(TRUE, TRUE, TRUE)
)
str(df)
## 'data.frame': 3 obs. of 3 variables:
## $ nombre: chr "Ana" "Luis" "María"
## $ nota : num 90 85 92
## $ aprobó: logi TRUE TRUE TRUE
summary(df)
## nombre nota aprobó
## Length:3 Min. :85.0 Mode:logical
## Class :character 1st Qu.:87.5 TRUE:3
## Mode :character Median :90.0
## Mean :89.0
## 3rd Qu.:91.0
## Max. :92.0
df[df$nota > 88, ] # filtrar filas
## nombre nota aprobó
## 1 Ana 90 TRUE
## 3 María 92 TRUE
df
## nombre nota aprobó
## 1 Ana 90 TRUE
## 2 Luis 85 TRUE
## 3 María 92 TRUE