# Untitled

unknown
plain_text
7 months ago
3.3 kB
6
Indexable
Never
```## Question 4

Besides economic and cultural threat, many scholars also argue that gender is an important predictor of immigration attitudes. While there is some evidence that women are slightly less opposed to immigration than men, it may also be true that gender conditions the very effect of other factors such as cultural threat. To see if it is indeed the case, fit a linear regression of H-1B support on the interaction between gender and implicit prejudice. Then, create a plot with the predicted level of H-1B support (y-axis) across the range of implicit bias (x-axis) by gender (Hint: you can use the `predict()` function. Check the help file via `?predict.lm` in RStudio). Considering the results, would you agree that gender alters the relationship between cultural threat and immigration attitudes?

```{r}
model_interaction <- lm(h1bvis.supp ~ female * impl.prejud, data = data)
```

```{r}
# Predicciones para hombres (female = 0) y mujeres (female = 1) en los valores de impl.prejud
predictions_male <- predict(model_interaction, newdata = data.frame(female = 0, impl.prejud = seq(0, 1, 0.25)))
predictions_female <- predict(model_interaction, newdata = data.frame(female = 1, impl.prejud = seq(0, 1, 0.25)))

# Crear un dataframe para las predicciones
predictions_df <- data.frame(
Gender = rep(c("Male", "Female"), each = length(predictions_male)),
ImplicitBias = rep(seq(0, 1, 0.25), times = 2),
PredictedSupport = c(predictions_male, predictions_female)
)

# Crear el gráfico de barras apiladas con colores por defecto
ggplot(predictions_df, aes(x = ImplicitBias, y = PredictedSupport, fill = Gender)) +
geom_bar(stat = "identity") +
labs(title = "Predicted H-1B Support by Implicit Bias and Gender", x = "Implicit Bias", y = "Predicted Support")

```

Age is another important covariate. Fit two regression models in which H-1B support is either a linear or quadratic function of age. Compare the results by plotting the predicted levels of support (y-axis) across the whole age range (x-axis). Would you say that people become more opposed to immigration with age?

```{r}
# Ajustar un modelo de regresión lineal
model_linear_age <- lm(h1bvis.supp ~ age, data = data)

# Ajustar un modelo de regresión cuadrática
model_quadratic_age <- lm(h1bvis.supp ~ age + I(age^2), data = data)

# Filtrar los valores de edad válidos
valid_age_data <- na.omit(data\$age)  # Elimina los valores faltantes (NA)

# Crear un rango de edades válidas para predecir
age_range <- seq(min(valid_age_data), max(valid_age_data), length.out = 100)

# Predicciones del modelo lineal
predictions_linear <- predict(model_linear_age, newdata = data.frame(age = age_range))

# Crear un dataframe para las predicciones
predictions_df <- data.frame(
Age = age_range,
PredictedSupportLinear = predictions_linear,